Search in sources :

Example 1 with BitsProducer

use of org.apache.lucene.search.suggest.BitsProducer in project lucene-solr by apache.

the class TestSuggestField method testSuggestOnAllFilteredDocuments.

@Test
public void testSuggestOnAllFilteredDocuments() throws Exception {
    Analyzer analyzer = new MockAnalyzer(random());
    RandomIndexWriter iw = new RandomIndexWriter(random(), dir, iwcWithSuggestField(analyzer, "suggest_field"));
    int num = Math.min(1000, atLeast(10));
    for (int i = 0; i < num; i++) {
        Document document = new Document();
        document.add(new SuggestField("suggest_field", "abc_" + i, i));
        document.add(newStringField("str_fld", "deleted", Field.Store.NO));
        iw.addDocument(document);
        if (usually()) {
            iw.commit();
        }
    }
    BitsProducer filter = new BitsProducer() {

        @Override
        public Bits getBits(LeafReaderContext context) throws IOException {
            return new Bits.MatchNoBits(context.reader().maxDoc());
        }
    };
    DirectoryReader reader = iw.getReader();
    SuggestIndexSearcher indexSearcher = new SuggestIndexSearcher(reader);
    // no random access required;
    // calling suggest with filter that does not match any documents should early terminate
    PrefixCompletionQuery query = new PrefixCompletionQuery(analyzer, new Term("suggest_field", "abc_"), filter);
    TopSuggestDocs suggest = indexSearcher.suggest(query, num, false);
    assertThat(suggest.totalHits, equalTo(0));
    reader.close();
    iw.close();
}
Also used : DirectoryReader(org.apache.lucene.index.DirectoryReader) Term(org.apache.lucene.index.Term) Analyzer(org.apache.lucene.analysis.Analyzer) MockAnalyzer(org.apache.lucene.analysis.MockAnalyzer) Document(org.apache.lucene.document.Document) IntPoint(org.apache.lucene.document.IntPoint) MockAnalyzer(org.apache.lucene.analysis.MockAnalyzer) LeafReaderContext(org.apache.lucene.index.LeafReaderContext) BitsProducer(org.apache.lucene.search.suggest.BitsProducer) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) Test(org.junit.Test)

Example 2 with BitsProducer

use of org.apache.lucene.search.suggest.BitsProducer in project lucene-solr by apache.

the class TestPrefixCompletionQuery method testMostlyFilteredOutDocuments.

public void testMostlyFilteredOutDocuments() throws Exception {
    Analyzer analyzer = new MockAnalyzer(random());
    RandomIndexWriter iw = new RandomIndexWriter(random(), dir, iwcWithSuggestField(analyzer, "suggest_field"));
    int num = Math.min(1000, atLeast(10));
    for (int i = 0; i < num; i++) {
        Document document = new Document();
        document.add(new SuggestField("suggest_field", "abc_" + i, i));
        document.add(new NumericDocValuesField("filter_int_fld", i));
        iw.addDocument(document);
        if (usually()) {
            iw.commit();
        }
    }
    DirectoryReader reader = iw.getReader();
    SuggestIndexSearcher indexSearcher = new SuggestIndexSearcher(reader);
    int topScore = num / 2;
    BitsProducer filter = new NumericRangeBitsProducer("filter_int_fld", 0, topScore);
    PrefixCompletionQuery query = new PrefixCompletionQuery(analyzer, new Term("suggest_field", "abc_"), filter);
    // if at most half of the top scoring documents have been filtered out
    // the search should be admissible for a single segment
    TopSuggestDocs suggest = indexSearcher.suggest(query, num, false);
    assertTrue(suggest.totalHits >= 1);
    assertThat(suggest.scoreLookupDocs()[0].key.toString(), equalTo("abc_" + topScore));
    assertThat(suggest.scoreLookupDocs()[0].score, equalTo((float) topScore));
    filter = new NumericRangeBitsProducer("filter_int_fld", 0, 0);
    query = new PrefixCompletionQuery(analyzer, new Term("suggest_field", "abc_"), filter);
    // if more than half of the top scoring documents have been filtered out
    // search is not admissible, so # of suggestions requested is num instead of 1
    suggest = indexSearcher.suggest(query, num, false);
    assertSuggestions(suggest, new Entry("abc_0", 0));
    filter = new NumericRangeBitsProducer("filter_int_fld", num - 1, num - 1);
    query = new PrefixCompletionQuery(analyzer, new Term("suggest_field", "abc_"), filter);
    // if only lower scoring documents are filtered out
    // search is admissible
    suggest = indexSearcher.suggest(query, 1, false);
    assertSuggestions(suggest, new Entry("abc_" + (num - 1), num - 1));
    reader.close();
    iw.close();
}
Also used : TestSuggestField.iwcWithSuggestField(org.apache.lucene.search.suggest.document.TestSuggestField.iwcWithSuggestField) DirectoryReader(org.apache.lucene.index.DirectoryReader) Term(org.apache.lucene.index.Term) Analyzer(org.apache.lucene.analysis.Analyzer) MockAnalyzer(org.apache.lucene.analysis.MockAnalyzer) Document(org.apache.lucene.document.Document) Entry(org.apache.lucene.search.suggest.document.TestSuggestField.Entry) MockAnalyzer(org.apache.lucene.analysis.MockAnalyzer) NumericDocValuesField(org.apache.lucene.document.NumericDocValuesField) BitsProducer(org.apache.lucene.search.suggest.BitsProducer) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter)

Example 3 with BitsProducer

use of org.apache.lucene.search.suggest.BitsProducer in project lucene-solr by apache.

the class TestPrefixCompletionQuery method testDocFiltering.

public void testDocFiltering() throws Exception {
    Analyzer analyzer = new MockAnalyzer(random());
    RandomIndexWriter iw = new RandomIndexWriter(random(), dir, iwcWithSuggestField(analyzer, "suggest_field"));
    Document document = new Document();
    document.add(new NumericDocValuesField("filter_int_fld", 9));
    document.add(new SuggestField("suggest_field", "apples", 3));
    iw.addDocument(document);
    document = new Document();
    document.add(new NumericDocValuesField("filter_int_fld", 10));
    document.add(new SuggestField("suggest_field", "applle", 4));
    iw.addDocument(document);
    document = new Document();
    document.add(new NumericDocValuesField("filter_int_fld", 4));
    document.add(new SuggestField("suggest_field", "apple", 5));
    iw.addDocument(document);
    if (rarely()) {
        iw.commit();
    }
    DirectoryReader reader = iw.getReader();
    SuggestIndexSearcher indexSearcher = new SuggestIndexSearcher(reader);
    // suggest without filter
    PrefixCompletionQuery query = new PrefixCompletionQuery(analyzer, new Term("suggest_field", "app"));
    TopSuggestDocs suggest = indexSearcher.suggest(query, 3, false);
    assertSuggestions(suggest, new Entry("apple", 5), new Entry("applle", 4), new Entry("apples", 3));
    // suggest with filter
    BitsProducer filter = new NumericRangeBitsProducer("filter_int_fld", 5, 12);
    query = new PrefixCompletionQuery(analyzer, new Term("suggest_field", "app"), filter);
    suggest = indexSearcher.suggest(query, 3, false);
    assertSuggestions(suggest, new Entry("applle", 4), new Entry("apples", 3));
    reader.close();
    iw.close();
}
Also used : TestSuggestField.iwcWithSuggestField(org.apache.lucene.search.suggest.document.TestSuggestField.iwcWithSuggestField) DirectoryReader(org.apache.lucene.index.DirectoryReader) Term(org.apache.lucene.index.Term) Analyzer(org.apache.lucene.analysis.Analyzer) MockAnalyzer(org.apache.lucene.analysis.MockAnalyzer) Document(org.apache.lucene.document.Document) Entry(org.apache.lucene.search.suggest.document.TestSuggestField.Entry) MockAnalyzer(org.apache.lucene.analysis.MockAnalyzer) NumericDocValuesField(org.apache.lucene.document.NumericDocValuesField) BitsProducer(org.apache.lucene.search.suggest.BitsProducer) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter)

Example 4 with BitsProducer

use of org.apache.lucene.search.suggest.BitsProducer in project lucene-solr by apache.

the class CompletionWeight method bulkScorer.

@Override
public BulkScorer bulkScorer(final LeafReaderContext context) throws IOException {
    final LeafReader reader = context.reader();
    final Terms terms;
    final NRTSuggester suggester;
    if ((terms = reader.terms(completionQuery.getField())) == null) {
        return null;
    }
    if (terms instanceof CompletionTerms) {
        CompletionTerms completionTerms = (CompletionTerms) terms;
        if ((suggester = completionTerms.suggester()) == null) {
            // i.e. no FST was built
            return null;
        }
    } else {
        throw new IllegalArgumentException(completionQuery.getField() + " is not a SuggestField");
    }
    BitsProducer filter = completionQuery.getFilter();
    Bits filteredDocs = null;
    if (filter != null) {
        filteredDocs = filter.getBits(context);
        if (filteredDocs.getClass() == Bits.MatchNoBits.class) {
            return null;
        }
    }
    return new CompletionScorer(this, suggester, reader, filteredDocs, filter != null, automaton);
}
Also used : LeafReader(org.apache.lucene.index.LeafReader) Terms(org.apache.lucene.index.Terms) Bits(org.apache.lucene.util.Bits) BitsProducer(org.apache.lucene.search.suggest.BitsProducer)

Aggregations

BitsProducer (org.apache.lucene.search.suggest.BitsProducer)4 Analyzer (org.apache.lucene.analysis.Analyzer)3 MockAnalyzer (org.apache.lucene.analysis.MockAnalyzer)3 Document (org.apache.lucene.document.Document)3 DirectoryReader (org.apache.lucene.index.DirectoryReader)3 RandomIndexWriter (org.apache.lucene.index.RandomIndexWriter)3 Term (org.apache.lucene.index.Term)3 NumericDocValuesField (org.apache.lucene.document.NumericDocValuesField)2 Entry (org.apache.lucene.search.suggest.document.TestSuggestField.Entry)2 TestSuggestField.iwcWithSuggestField (org.apache.lucene.search.suggest.document.TestSuggestField.iwcWithSuggestField)2 IntPoint (org.apache.lucene.document.IntPoint)1 LeafReader (org.apache.lucene.index.LeafReader)1 LeafReaderContext (org.apache.lucene.index.LeafReaderContext)1 Terms (org.apache.lucene.index.Terms)1 Bits (org.apache.lucene.util.Bits)1 Test (org.junit.Test)1