Search in sources :

Example 26 with Entry

use of org.apache.lucene.search.suggest.document.TestSuggestField.Entry in project lucene-solr by apache.

the class TestContextQuery method testSameSuggestionMultipleContext.

@Test
public void testSameSuggestionMultipleContext() throws Exception {
    Analyzer analyzer = new MockAnalyzer(random());
    RandomIndexWriter iw = new RandomIndexWriter(random(), dir, iwcWithSuggestField(analyzer, "suggest_field"));
    Document document = new Document();
    document.add(new ContextSuggestField("suggest_field", "suggestion", 4, "type1", "type2", "type3"));
    iw.addDocument(document);
    document = new Document();
    document.add(new ContextSuggestField("suggest_field", "suggestion", 1, "type4"));
    iw.addDocument(document);
    if (rarely()) {
        iw.commit();
    }
    DirectoryReader reader = iw.getReader();
    SuggestIndexSearcher suggestIndexSearcher = new SuggestIndexSearcher(reader);
    ContextQuery query = new ContextQuery(new PrefixCompletionQuery(analyzer, new Term("suggest_field", "sugg")));
    query.addContext("type1", 10);
    query.addContext("type2", 2);
    query.addContext("type3", 3);
    query.addContext("type4", 4);
    TopSuggestDocs suggest = suggestIndexSearcher.suggest(query, 5, false);
    assertSuggestions(suggest, new Entry("suggestion", "type1", 4 * 10), new Entry("suggestion", "type3", 4 * 3), new Entry("suggestion", "type2", 4 * 2), new Entry("suggestion", "type4", 1 * 4));
    reader.close();
    iw.close();
}
Also used : Entry(org.apache.lucene.search.suggest.document.TestSuggestField.Entry) MockAnalyzer(org.apache.lucene.analysis.MockAnalyzer) 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) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) Test(org.junit.Test)

Example 27 with Entry

use of org.apache.lucene.search.suggest.document.TestSuggestField.Entry in project lucene-solr by apache.

the class TestContextSuggestField method testCompletionAnalyzer.

@Test
public void testCompletionAnalyzer() throws Exception {
    CompletionAnalyzer completionAnalyzer = new CompletionAnalyzer(new StandardAnalyzer(), true, true);
    RandomIndexWriter iw = new RandomIndexWriter(random(), dir, iwcWithSuggestField(completionAnalyzer, "suggest_field"));
    Document document = new Document();
    document.add(new ContextSuggestField("suggest_field", "suggestion1", 4, "type1"));
    document.add(new ContextSuggestField("suggest_field", "suggestion2", 3, "type2"));
    document.add(new ContextSuggestField("suggest_field", "suggestion3", 2, "type3"));
    iw.addDocument(document);
    document = new Document();
    document.add(new ContextSuggestField("suggest_field", "suggestion4", 1, "type4"));
    iw.addDocument(document);
    if (rarely()) {
        iw.commit();
    }
    DirectoryReader reader = iw.getReader();
    SuggestIndexSearcher suggestIndexSearcher = new SuggestIndexSearcher(reader);
    ContextQuery query = new ContextQuery(new PrefixCompletionQuery(completionAnalyzer, new Term("suggest_field", "sugg")));
    TopSuggestDocs suggest = suggestIndexSearcher.suggest(query, 4, false);
    assertSuggestions(suggest, new Entry("suggestion1", "type1", 4), new Entry("suggestion2", "type2", 3), new Entry("suggestion3", "type3", 2), new Entry("suggestion4", "type4", 1));
    query.addContext("type1");
    suggest = suggestIndexSearcher.suggest(query, 4, false);
    assertSuggestions(suggest, new Entry("suggestion1", "type1", 4));
    reader.close();
    iw.close();
}
Also used : Entry(org.apache.lucene.search.suggest.document.TestSuggestField.Entry) DirectoryReader(org.apache.lucene.index.DirectoryReader) StandardAnalyzer(org.apache.lucene.analysis.standard.StandardAnalyzer) Term(org.apache.lucene.index.Term) Document(org.apache.lucene.document.Document) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) Test(org.junit.Test)

Aggregations

Document (org.apache.lucene.document.Document)27 DirectoryReader (org.apache.lucene.index.DirectoryReader)27 RandomIndexWriter (org.apache.lucene.index.RandomIndexWriter)27 Term (org.apache.lucene.index.Term)27 Entry (org.apache.lucene.search.suggest.document.TestSuggestField.Entry)27 Analyzer (org.apache.lucene.analysis.Analyzer)26 MockAnalyzer (org.apache.lucene.analysis.MockAnalyzer)26 Test (org.junit.Test)20 TestSuggestField.iwcWithSuggestField (org.apache.lucene.search.suggest.document.TestSuggestField.iwcWithSuggestField)10 StandardAnalyzer (org.apache.lucene.analysis.standard.StandardAnalyzer)2 NumericDocValuesField (org.apache.lucene.document.NumericDocValuesField)2 BitsProducer (org.apache.lucene.search.suggest.BitsProducer)2 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 StringField (org.apache.lucene.document.StringField)1 IndexWriter (org.apache.lucene.index.IndexWriter)1