Dear all, Along with the search results, we are trying to find which text and field gave the result. For that, We have written a custom collector for collecting query from the scorer along with the ScoreDoc.
Please clarify to us is there anything wrong/limitations in our approach. Are there any other approaches for getting the matched query from the results? ScoreDocExtended scoreDocExtended = new ScoreDocExtended(doc + docBase , score); Object[] queries = getQueries(doc , baseScorer); scoreDocExtended.setQueriesMatched(queries); pq.add(scoreDocExtended); private Object[] getQueries(int docID , Scorer scorer) { Collection<Scorer.ChildScorer> children = scorer.getChildren(); Object[] queries = new Object[children.size()]; int i = 0; for (Scorer.ChildScorer childScorer : children) { Scorer child = childScorer.child; if(child != null) { if(child.docID() == docID) { Query query = child.getWeight().getQuery(); queries[i++] = query; } } } return queries; } Thanks Aravinth.
import org.apache.lucene.index.AtomicReaderContext; import org.apache.lucene.search.Query; import org.apache.lucene.search.Scorer; import org.apache.lucene.search.TopDocsCollector; import org.apache.lucene.util.PriorityQueue; import java.io.IOException; import java.util.Collection; public class QueryTypeCollector extends TopDocsCollector { private int pqSize; private Scorer baseScorer; private int docBase = 0; private QueryTypeCollector(PriorityQueue<ScoreDocExtended> pq) { super(pq); } public QueryTypeCollector(int i) { this(new QueryTypePriorityQueue(i)); pqSize = i; } @Override public void setScorer(Scorer scorer) throws IOException { baseScorer = scorer; } @Override public void collect(int doc) throws IOException { totalHits++; float score = baseScorer.score(); if(pq.size() < pqSize) { ScoreDocExtended scoreDocExtended = new ScoreDocExtended(doc + docBase , score); Object[] queries = getQueries(doc , baseScorer); scoreDocExtended.setQueriesMatched(queries); pq.add(scoreDocExtended); } else { ScoreDocExtended pqTop = (ScoreDocExtended) pq.top(); if(pqTop.score < score) { pqTop.score = score; pqTop.doc = doc + docBase; Object[] queries = getQueries(doc , baseScorer); pqTop.setQueriesMatched(queries); pq.updateTop(); } } } private Object[] getQueries(int docID , Scorer scorer) { Collection<Scorer.ChildScorer> children = scorer.getChildren(); if(children.size() == 0) { Query query = scorer.getWeight().getQuery(); return new Object[]{query}; } Object[] queries = new Object[children.size()]; int i = 0; for (Scorer.ChildScorer childScorer : children) { Scorer child = childScorer.child; if(child!=null) { Collection<Scorer.ChildScorer> childChildren = child.getChildren(); if(childChildren.size() == 0) { if(child.docID()==docID) { Query query = child.getWeight().getQuery(); queries[i++] = query; } } else { queries[i++] = getQueries(docID , child); } } } return queries; } @Override public void setNextReader(AtomicReaderContext context) throws IOException { docBase = context.docBase; } @Override public boolean acceptsDocsOutOfOrder() { return false; } }
import org.apache.lucene.search.ScoreDoc; public class ScoreDocExtended extends ScoreDoc { public Object[] getQueriesMatched() { return queriesMatched; } private Object[] queriesMatched; public void setQueriesMatched(Object[] queriesMatched) { this.queriesMatched = queriesMatched; } public ScoreDocExtended(int doc , float score) { super(doc , score); } @Override public String toString() { StringBuilder sb = new StringBuilder(); sb.append("doc id= ").append(doc).append(" score=" ).append(score).append(" "); // NO I18N if(getQueriesMatched() != null) { sb.append(print(getQueriesMatched())); } return sb.toString(); } private StringBuilder print(Object[] queriesMatched) { StringBuilder sb = new StringBuilder(); if(queriesMatched!=null) { for (Object q : queriesMatched) { if(q != null && !q.getClass().isArray()) { sb.append(q).append(" -- "); } else { sb.append(print((Object[]) q)); } } } return sb; } }
--------------------------------------------------------------------- To unsubscribe, e-mail: java-user-unsubscr...@lucene.apache.org For additional commands, e-mail: java-user-h...@lucene.apache.org