Search in sources :

Example 1 with PhraseScorer

use of org.opengrok.suggest.query.PhraseScorer in project OpenGrok by OpenGrok.

the class CustomSloppyPhraseScorerTest method test.

public static void test(final int slop, final int offset, final String[] terms, final Integer[] expectedPositions) throws IOException {
    Directory dir = new ByteBuffersDirectory();
    try (IndexWriter iw = new IndexWriter(dir, new IndexWriterConfig())) {
        Document doc = new Document();
        doc.add(new TextField("test", "zero one two three four five six seven eight nine ten", Field.Store.NO));
        iw.addDocument(doc);
    }
    CustomPhraseQuery query = new CustomPhraseQuery(slop, "test", terms);
    query.setOffset(offset);
    try (IndexReader ir = DirectoryReader.open(dir)) {
        IndexSearcher is = new IndexSearcher(ir);
        Weight w = query.createWeight(is, ScoreMode.COMPLETE_NO_SCORES, 1);
        LeafReaderContext context = ir.getContext().leaves().get(0);
        Scorer scorer = w.scorer(context);
        TwoPhaseIterator it = scorer.twoPhaseIterator();
        int correctDoc = -1;
        int docId;
        while ((docId = it.approximation().nextDoc()) != DocIdSetIterator.NO_MORE_DOCS) {
            if (it.matches()) {
                correctDoc = docId;
            }
        }
        BitIntsHolder bs = (BitIntsHolder) ((PhraseScorer) scorer).getPositions(correctDoc);
        assertThat(toSet(bs), contains(expectedPositions));
    }
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) BitIntsHolder(org.opengrok.suggest.query.data.BitIntsHolder) TwoPhaseIterator(org.apache.lucene.search.TwoPhaseIterator) Scorer(org.apache.lucene.search.Scorer) PhraseScorer(org.opengrok.suggest.query.PhraseScorer) Document(org.apache.lucene.document.Document) Weight(org.apache.lucene.search.Weight) IndexWriter(org.apache.lucene.index.IndexWriter) ByteBuffersDirectory(org.apache.lucene.store.ByteBuffersDirectory) IndexReader(org.apache.lucene.index.IndexReader) TextField(org.apache.lucene.document.TextField) LeafReaderContext(org.apache.lucene.index.LeafReaderContext) ByteBuffersDirectory(org.apache.lucene.store.ByteBuffersDirectory) Directory(org.apache.lucene.store.Directory) IndexWriterConfig(org.apache.lucene.index.IndexWriterConfig)

Example 2 with PhraseScorer

use of org.opengrok.suggest.query.PhraseScorer in project OpenGrok by OpenGrok.

the class SuggesterSearcher method getComplexQueryData.

private ComplexQueryData getComplexQueryData(final Query query, final LeafReaderContext leafReaderContext) {
    ComplexQueryData data = new ComplexQueryData();
    if (query == null || query instanceof SuggesterQuery) {
        data.documentIds = new BitIntsHolder(0);
        return data;
    }
    BitIntsHolder documentIds = new BitIntsHolder();
    try {
        search(query, new Collector() {

            @Override
            public LeafCollector getLeafCollector(final LeafReaderContext context) {
                return new LeafCollector() {

                    final int docBase = context.docBase;

                    @Override
                    public void setScorer(final Scorable scorer) {
                        if (leafReaderContext == context) {
                            if (scorer instanceof PhraseScorer) {
                                data.scorer = (PhraseScorer) scorer;
                            } else {
                                try {
                                    // in #setScorer but no better way was found
                                    for (Scorer.ChildScorable childScorer : scorer.getChildren()) {
                                        if (childScorer.child instanceof PhraseScorer) {
                                            data.scorer = (PhraseScorer) childScorer.child;
                                        }
                                    }
                                } catch (Exception e) {
                                // ignore
                                }
                            }
                        }
                    }

                    @Override
                    public void collect(int doc) {
                        if (leafReaderContext == context) {
                            documentIds.set(docBase + doc);
                        }
                    }
                };
            }

            @Override
            public ScoreMode scoreMode() {
                return ScoreMode.COMPLETE_NO_SCORES;
            }
        });
    } catch (IOException e) {
        if (Thread.currentThread().isInterrupted()) {
            interrupted = true;
            return null;
        } else {
            logger.log(Level.WARNING, "Could not get document ids for " + query, e);
        }
    } catch (Exception e) {
        logger.log(Level.WARNING, "Could not get document ids for " + query, e);
    }
    data.documentIds = documentIds;
    return data;
}
Also used : ScoreMode(org.apache.lucene.search.ScoreMode) BitIntsHolder(org.opengrok.suggest.query.data.BitIntsHolder) Scorable(org.apache.lucene.search.Scorable) SuggesterQuery(org.opengrok.suggest.query.SuggesterQuery) IOException(java.io.IOException) PhraseScorer(org.opengrok.suggest.query.PhraseScorer) IOException(java.io.IOException) LeafCollector(org.apache.lucene.search.LeafCollector) LeafCollector(org.apache.lucene.search.LeafCollector) Collector(org.apache.lucene.search.Collector) LeafReaderContext(org.apache.lucene.index.LeafReaderContext)

Aggregations

LeafReaderContext (org.apache.lucene.index.LeafReaderContext)2 PhraseScorer (org.opengrok.suggest.query.PhraseScorer)2 BitIntsHolder (org.opengrok.suggest.query.data.BitIntsHolder)2 IOException (java.io.IOException)1 Document (org.apache.lucene.document.Document)1 TextField (org.apache.lucene.document.TextField)1 IndexReader (org.apache.lucene.index.IndexReader)1 IndexWriter (org.apache.lucene.index.IndexWriter)1 IndexWriterConfig (org.apache.lucene.index.IndexWriterConfig)1 Collector (org.apache.lucene.search.Collector)1 IndexSearcher (org.apache.lucene.search.IndexSearcher)1 LeafCollector (org.apache.lucene.search.LeafCollector)1 Scorable (org.apache.lucene.search.Scorable)1 ScoreMode (org.apache.lucene.search.ScoreMode)1 Scorer (org.apache.lucene.search.Scorer)1 TwoPhaseIterator (org.apache.lucene.search.TwoPhaseIterator)1 Weight (org.apache.lucene.search.Weight)1 ByteBuffersDirectory (org.apache.lucene.store.ByteBuffersDirectory)1 Directory (org.apache.lucene.store.Directory)1 SuggesterQuery (org.opengrok.suggest.query.SuggesterQuery)1