Auto index & optimize imports. And switch to java5 for-each in place of some iterators, generics issues.

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1598141 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
David Wayne Smiley 2014-05-28 21:15:43 +00:00
parent 36c9276bba
commit d61e0f7527
1 changed files with 88 additions and 91 deletions

View File

@ -17,19 +17,23 @@
package org.apache.solr.handler.component;
import com.carrotsearch.hppc.IntObjectMap;
import com.carrotsearch.hppc.IntObjectOpenHashMap;
import com.carrotsearch.hppc.IntOpenHashSet;
import com.carrotsearch.hppc.cursors.IntObjectCursor;
import com.carrotsearch.hppc.cursors.ObjectCursor;
import org.apache.lucene.index.AtomicReader;
import org.apache.lucene.index.AtomicReaderContext;
import org.apache.lucene.index.DocValues;
import org.apache.lucene.index.SortedDocValues;
import org.apache.lucene.search.LeafCollector;
import org.apache.lucene.search.Collector;
import org.apache.lucene.search.DocIdSetIterator;
import org.apache.lucene.search.LeafCollector;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.Scorer;
import org.apache.lucene.search.SimpleCollector;
import org.apache.lucene.search.Sort;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.Collector;
import org.apache.lucene.search.TopDocsCollector;
import org.apache.lucene.search.TopFieldCollector;
import org.apache.lucene.search.TopScoreDocCollector;
@ -37,28 +41,23 @@ import org.apache.lucene.util.BytesRef;
import org.apache.lucene.util.CharsRef;
import org.apache.lucene.util.FixedBitSet;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.params.ExpandParams;
import org.apache.solr.common.params.ShardParams;
import org.apache.solr.common.params.SolrParams;
import org.apache.solr.common.util.NamedList;
import org.apache.solr.core.PluginInfo;
import org.apache.solr.core.SolrCore;
import org.apache.solr.request.SolrQueryRequest;
import org.apache.solr.schema.FieldType;
import org.apache.solr.search.CollapsingQParserPlugin;
import org.apache.solr.search.DocIterator;
import org.apache.solr.search.DocList;
import org.apache.solr.search.DocSlice;
import org.apache.solr.search.QParser;
import org.apache.solr.search.QueryParsing;
import org.apache.solr.schema.FieldType;
import org.apache.solr.common.params.SolrParams;
import org.apache.solr.common.params.ExpandParams;
import org.apache.solr.common.util.NamedList;
import org.apache.solr.request.SolrQueryRequest;
import org.apache.solr.search.DocSlice;
import org.apache.solr.search.SolrIndexSearcher;
import org.apache.solr.util.plugin.PluginInfoInitialized;
import org.apache.solr.util.plugin.SolrCoreAware;
import org.apache.solr.core.PluginInfo;
import org.apache.solr.core.SolrCore;
import com.carrotsearch.hppc.IntObjectMap;
import com.carrotsearch.hppc.IntObjectOpenHashMap;
import com.carrotsearch.hppc.IntOpenHashSet;
import com.carrotsearch.hppc.cursors.IntObjectCursor;
import com.carrotsearch.hppc.cursors.ObjectCursor;
import java.io.IOException;
import java.net.MalformedURLException;
@ -66,49 +65,48 @@ import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Iterator;
import java.util.Map;
/**
* The ExpandComponent is designed to work with the CollapsingPostFilter.
* The CollapsingPostFilter collapses a result set on a field.
* <p/>
* The ExpandComponent expands the collapsed groups for a single page.
* <p/>
* http parameters:
* <p/>
* expand=true <br/>
* expand.rows=5 <br/>
* expand.sort=field asc|desc<br/>
* expand.q=*:* (optional, overrides the main query)<br/>
* expand.fq=type:child (optional, overrides the main filter queries)<br/>
* expand.field=field (mandatory if the not used with the CollapsingQParserPlugin)<br/>
**/
* The ExpandComponent is designed to work with the CollapsingPostFilter.
* The CollapsingPostFilter collapses a result set on a field.
* <p/>
* The ExpandComponent expands the collapsed groups for a single page.
* <p/>
* http parameters:
* <p/>
* expand=true <br/>
* expand.rows=5 <br/>
* expand.sort=field asc|desc<br/>
* expand.q=*:* (optional, overrides the main query)<br/>
* expand.fq=type:child (optional, overrides the main filter queries)<br/>
* expand.field=field (mandatory if the not used with the CollapsingQParserPlugin)<br/>
*/
public class ExpandComponent extends SearchComponent implements PluginInfoInitialized, SolrCoreAware {
public static final String COMPONENT_NAME = "expand";
private PluginInfo info = PluginInfo.EMPTY_INFO;
@Override
@Override
public void init(PluginInfo info) {
this.info = info;
this.info = info;
}
@Override
@Override
public void prepare(ResponseBuilder rb) throws IOException {
if (rb.req.getParams().getBool(ExpandParams.EXPAND,false)) {
if (rb.req.getParams().getBool(ExpandParams.EXPAND, false)) {
rb.doExpand = true;
}
}
@Override
@Override
public void inform(SolrCore core) {
}
@Override
@Override
public void process(ResponseBuilder rb) throws IOException {
if(!rb.doExpand) {
if (!rb.doExpand) {
return;
}
@ -118,24 +116,24 @@ public class ExpandComponent extends SearchComponent implements PluginInfoInitia
boolean isShard = params.getBool(ShardParams.IS_SHARD, false);
String ids = params.get(ShardParams.IDS);
if(ids == null && isShard) {
if (ids == null && isShard) {
return;
}
String field = params.get(ExpandParams.EXPAND_FIELD);
if(field == null) {
if (field == null) {
List<Query> filters = rb.getFilters();
if(filters != null) {
for(Query q : filters) {
if(q instanceof CollapsingQParserPlugin.CollapsingPostFilter) {
CollapsingQParserPlugin.CollapsingPostFilter cp = (CollapsingQParserPlugin.CollapsingPostFilter)q;
field = cp.getField();
if (filters != null) {
for (Query q : filters) {
if (q instanceof CollapsingQParserPlugin.CollapsingPostFilter) {
CollapsingQParserPlugin.CollapsingPostFilter cp = (CollapsingQParserPlugin.CollapsingPostFilter) q;
field = cp.getField();
}
}
}
}
if(field == null) {
if (field == null) {
throw new IOException("Expand field is null.");
}
@ -146,29 +144,29 @@ public class ExpandComponent extends SearchComponent implements PluginInfoInitia
Sort sort = null;
if(sortParam != null) {
if (sortParam != null) {
sort = QueryParsing.parseSortSpec(sortParam, rb.req).getSort();
}
Query query = null;
if(qs == null) {
Query query;
if (qs == null) {
query = rb.getQuery();
} else {
try {
QParser parser = QParser.getParser(qs, null, req);
query = parser.getQuery();
} catch(Exception e) {
} catch (Exception e) {
throw new IOException(e);
}
}
List<Query> newFilters = new ArrayList();
List<Query> newFilters = new ArrayList<>();
if(fqs == null) {
if (fqs == null) {
List<Query> filters = rb.getFilters();
if(filters != null) {
for(Query q : filters) {
if(!(q instanceof CollapsingQParserPlugin.CollapsingPostFilter)) {
if (filters != null) {
for (Query q : filters) {
if (!(q instanceof CollapsingQParserPlugin.CollapsingPostFilter)) {
newFilters.add(q);
}
}
@ -176,12 +174,12 @@ public class ExpandComponent extends SearchComponent implements PluginInfoInitia
} else {
try {
for (String fq : fqs) {
if (fq != null && fq.trim().length()!=0 && !fq.equals("*:*")) {
if (fq != null && fq.trim().length() != 0 && !fq.equals("*:*")) {
QParser fqp = QParser.getParser(fq, null, req);
newFilters.add(fqp.getQuery());
}
}
} catch(Exception e) {
} catch (Exception e) {
throw new IOException(e);
}
}
@ -191,25 +189,25 @@ public class ExpandComponent extends SearchComponent implements PluginInfoInitia
SortedDocValues values = DocValues.getSorted(reader, field);
FixedBitSet groupBits = new FixedBitSet(values.getValueCount());
DocList docList = rb.getResults().docList;
IntOpenHashSet collapsedSet = new IntOpenHashSet(docList.size()*2);
IntOpenHashSet collapsedSet = new IntOpenHashSet(docList.size() * 2);
DocIterator idit = docList.iterator();
while(idit.hasNext()) {
while (idit.hasNext()) {
int doc = idit.nextDoc();
int ord = values.getOrd(doc);
if(ord > -1) {
if (ord > -1) {
groupBits.set(ord);
collapsedSet.add(doc);
}
}
Collector collector = null;
Collector collector;
if (sort != null)
sort = sort.rewrite(searcher);
GroupExpandCollector groupExpandCollector = new GroupExpandCollector(values, groupBits, collapsedSet, limit, sort);
SolrIndexSearcher.ProcessedFilter pfilter = searcher.getProcessedFilter(null, newFilters);
if(pfilter.postFilter != null) {
if (pfilter.postFilter != null) {
pfilter.postFilter.setLastDelegate(groupExpandCollector);
collector = pfilter.postFilter;
} else {
@ -218,22 +216,19 @@ public class ExpandComponent extends SearchComponent implements PluginInfoInitia
searcher.search(query, pfilter.filter, collector);
IntObjectMap groups = groupExpandCollector.getGroups();
Iterator<IntObjectCursor> it = groups.iterator();
Map<String, DocSlice> outMap = new HashMap<>();
BytesRef bytesRef = new BytesRef();
CharsRef charsRef = new CharsRef();
FieldType fieldType = searcher.getSchema().getField(field).getType();
while(it.hasNext()) {
IntObjectCursor cursor = it.next();
for (IntObjectCursor cursor : groups) {
int ord = cursor.key;
TopDocsCollector topDocsCollector = (TopDocsCollector)cursor.value;
TopDocsCollector topDocsCollector = (TopDocsCollector) cursor.value;
TopDocs topDocs = topDocsCollector.topDocs();
ScoreDoc[] scoreDocs = topDocs.scoreDocs;
if(scoreDocs.length > 0) {
if (scoreDocs.length > 0) {
int[] docs = new int[scoreDocs.length];
float[] scores = new float[scoreDocs.length];
for(int i=0; i<docs.length; i++) {
for (int i = 0; i < docs.length; i++) {
ScoreDoc scoreDoc = scoreDocs[i];
docs[i] = scoreDoc.doc;
scores[i] = scoreDoc.score;
@ -248,31 +243,32 @@ public class ExpandComponent extends SearchComponent implements PluginInfoInitia
rb.rsp.add("expanded", outMap);
}
@Override
@Override
public void modifyRequest(ResponseBuilder rb, SearchComponent who, ShardRequest sreq) {
}
@Override
@SuppressWarnings("unchecked")
@Override
public void handleResponses(ResponseBuilder rb, ShardRequest sreq) {
if(!rb.doExpand) {
if (!rb.doExpand) {
return;
}
if ((sreq.purpose & ShardRequest.PURPOSE_GET_FIELDS) != 0) {
SolrQueryRequest req = rb.req;
Map expanded = (Map)req.getContext().get("expanded");
if(expanded == null) {
Map expanded = (Map) req.getContext().get("expanded");
if (expanded == null) {
expanded = new HashMap();
req.getContext().put("expanded", expanded);
}
for (ShardResponse srsp : sreq.responses) {
NamedList response = srsp.getSolrResponse().getResponse();
Map ex = (Map)response.get("expanded");
Iterator<Map.Entry<String,SolrDocumentList>>it = ex.entrySet().iterator();
while(it.hasNext()) {
Map.Entry<String, SolrDocumentList> entry = it.next();
Map ex = (Map) response.get("expanded");
for (Map.Entry<String, SolrDocumentList> entry : (Iterable<Map.Entry<String, SolrDocumentList>>) ex.entrySet()) {
String name = entry.getKey();
SolrDocumentList val = entry.getValue();
expanded.put(name, val);
@ -280,10 +276,11 @@ public class ExpandComponent extends SearchComponent implements PluginInfoInitia
}
}
}
@Override
@Override
public void finishStage(ResponseBuilder rb) {
if(!rb.doExpand) {
if (!rb.doExpand) {
return;
}
@ -291,8 +288,8 @@ public class ExpandComponent extends SearchComponent implements PluginInfoInitia
return;
}
Map expanded = (Map)rb.req.getContext().get("expanded");
if(expanded == null) {
Map expanded = (Map) rb.req.getContext().get("expanded");
if (expanded == null) {
expanded = new HashMap();
}
@ -308,11 +305,11 @@ public class ExpandComponent extends SearchComponent implements PluginInfoInitia
public GroupExpandCollector(SortedDocValues docValues, FixedBitSet groupBits, IntOpenHashSet collapsedSet, int limit, Sort sort) throws IOException {
int numGroups = collapsedSet.size();
groups = new IntObjectOpenHashMap<>(numGroups*2);
groups = new IntObjectOpenHashMap<>(numGroups * 2);
DocIdSetIterator iterator = groupBits.iterator();
int group = -1;
while((group = iterator.nextDoc()) != DocIdSetIterator.NO_MORE_DOCS) {
Collector collector = (sort == null) ? TopScoreDocCollector.create(limit, true) : TopFieldCollector.create(sort,limit, false, false,false, true);
int group;
while ((group = iterator.nextDoc()) != DocIdSetIterator.NO_MORE_DOCS) {
Collector collector = (sort == null) ? TopScoreDocCollector.create(limit, true) : TopFieldCollector.create(sort, limit, false, false, false, true);
groups.put(group, collector);
}
@ -338,9 +335,9 @@ public class ExpandComponent extends SearchComponent implements PluginInfoInitia
@Override
public void collect(int docId) throws IOException {
int doc = docId+docBase;
int doc = docId + docBase;
int ord = docValues.getOrd(doc);
if(ord > -1 && groupBits.get(ord) && !collapsedSet.contains(doc)) {
if (ord > -1 && groupBits.get(ord) && !collapsedSet.contains(doc)) {
LeafCollector c = leafCollectors.get(ord);
c.collect(docId);
}
@ -363,7 +360,7 @@ public class ExpandComponent extends SearchComponent implements PluginInfoInitia
/// SolrInfoMBean
////////////////////////////////////////////
@Override
@Override
public String getDescription() {
return "Expand Component";
}