Search in sources :

Example 66 with InputArrayIterator

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

the class AnalyzingInfixSuggesterTest method testCloseIndexWriterOnBuild.

public void testCloseIndexWriterOnBuild() throws Exception {
    class MyAnalyzingInfixSuggester extends AnalyzingInfixSuggester {

        public MyAnalyzingInfixSuggester(Directory dir, Analyzer indexAnalyzer, Analyzer queryAnalyzer, int minPrefixChars, boolean commitOnBuild, boolean allTermsRequired, boolean highlight, boolean closeIndexWriterOnBuild) throws IOException {
            super(dir, indexAnalyzer, queryAnalyzer, minPrefixChars, commitOnBuild, allTermsRequired, highlight, closeIndexWriterOnBuild);
        }

        public IndexWriter getIndexWriter() {
            return writer;
        }

        public SearcherManager getSearcherManager() {
            return searcherMgr;
        }
    }
    // After build(), when closeIndexWriterOnBuild = true: 
    // * The IndexWriter should be null 
    // * The SearcherManager should be non-null
    // * SearcherManager's IndexWriter reference should be closed 
    //   (as evidenced by maybeRefreshBlocking() throwing AlreadyClosedException)
    Analyzer a = new MockAnalyzer(random(), MockTokenizer.WHITESPACE, false);
    Path tempDir = createTempDir("analyzingInfixContext");
    final MyAnalyzingInfixSuggester suggester = new MyAnalyzingInfixSuggester(newFSDirectory(tempDir), a, a, 3, false, AnalyzingInfixSuggester.DEFAULT_ALL_TERMS_REQUIRED, AnalyzingInfixSuggester.DEFAULT_HIGHLIGHT, true);
    suggester.build(new InputArrayIterator(sharedInputs));
    assertNull(suggester.getIndexWriter());
    assertNotNull(suggester.getSearcherManager());
    expectThrows(AlreadyClosedException.class, () -> suggester.getSearcherManager().maybeRefreshBlocking());
    suggester.close();
    // After instantiating from an already-built suggester dir:
    // * The IndexWriter should be null
    // * The SearcherManager should be non-null
    final MyAnalyzingInfixSuggester suggester2 = new MyAnalyzingInfixSuggester(newFSDirectory(tempDir), a, a, 3, false, AnalyzingInfixSuggester.DEFAULT_ALL_TERMS_REQUIRED, AnalyzingInfixSuggester.DEFAULT_HIGHLIGHT, true);
    assertNull(suggester2.getIndexWriter());
    assertNotNull(suggester2.getSearcherManager());
    suggester2.close();
    a.close();
}
Also used : Path(java.nio.file.Path) MockAnalyzer(org.apache.lucene.analysis.MockAnalyzer) InputArrayIterator(org.apache.lucene.search.suggest.InputArrayIterator) Analyzer(org.apache.lucene.analysis.Analyzer) MockAnalyzer(org.apache.lucene.analysis.MockAnalyzer) Directory(org.apache.lucene.store.Directory)

Example 67 with InputArrayIterator

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

the class AnalyzingInfixSuggesterTest method testRandomMinPrefixLength.

public void testRandomMinPrefixLength() throws Exception {
    Input[] keys = new Input[] { new Input("lend me your ear", 8, new BytesRef("foobar")), new Input("a penny saved is a penny earned", 10, new BytesRef("foobaz")) };
    Path tempDir = createTempDir("AnalyzingInfixSuggesterTest");
    Analyzer a = new MockAnalyzer(random(), MockTokenizer.WHITESPACE, false);
    int minPrefixLength = random().nextInt(10);
    AnalyzingInfixSuggester suggester = new AnalyzingInfixSuggester(newFSDirectory(tempDir), a, a, minPrefixLength, false);
    suggester.build(new InputArrayIterator(keys));
    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 2; j++) {
            boolean doHighlight = j == 0;
            List<LookupResult> results = suggester.lookup(TestUtil.stringToCharSequence("ear", random()), 10, true, doHighlight);
            assertEquals(2, results.size());
            assertEquals("a penny saved is a penny earned", results.get(0).key);
            if (doHighlight) {
                assertEquals("a penny saved is a penny <b>ear</b>ned", results.get(0).highlightKey);
            }
            assertEquals(10, results.get(0).value);
            assertEquals("lend me your ear", results.get(1).key);
            if (doHighlight) {
                assertEquals("lend me your <b>ear</b>", results.get(1).highlightKey);
            }
            assertEquals(new BytesRef("foobaz"), results.get(0).payload);
            assertEquals(8, results.get(1).value);
            assertEquals(new BytesRef("foobar"), results.get(1).payload);
            results = suggester.lookup(TestUtil.stringToCharSequence("ear ", random()), 10, true, doHighlight);
            assertEquals(1, results.size());
            assertEquals("lend me your ear", results.get(0).key);
            if (doHighlight) {
                assertEquals("lend me your <b>ear</b>", results.get(0).highlightKey);
            }
            assertEquals(8, results.get(0).value);
            assertEquals(new BytesRef("foobar"), results.get(0).payload);
            results = suggester.lookup(TestUtil.stringToCharSequence("pen", random()), 10, true, doHighlight);
            assertEquals(1, results.size());
            assertEquals("a penny saved is a penny earned", results.get(0).key);
            if (doHighlight) {
                assertEquals("a <b>pen</b>ny saved is a <b>pen</b>ny earned", results.get(0).highlightKey);
            }
            assertEquals(10, results.get(0).value);
            assertEquals(new BytesRef("foobaz"), results.get(0).payload);
            results = suggester.lookup(TestUtil.stringToCharSequence("p", random()), 10, true, doHighlight);
            assertEquals(1, results.size());
            assertEquals("a penny saved is a penny earned", results.get(0).key);
            if (doHighlight) {
                assertEquals("a <b>p</b>enny saved is a <b>p</b>enny earned", results.get(0).highlightKey);
            }
            assertEquals(10, results.get(0).value);
            assertEquals(new BytesRef("foobaz"), results.get(0).payload);
        }
        // Make sure things still work after close and reopen:
        suggester.close();
        suggester = new AnalyzingInfixSuggester(newFSDirectory(tempDir), a, a, minPrefixLength, false);
    }
    suggester.close();
    a.close();
}
Also used : Path(java.nio.file.Path) Input(org.apache.lucene.search.suggest.Input) MockAnalyzer(org.apache.lucene.analysis.MockAnalyzer) InputArrayIterator(org.apache.lucene.search.suggest.InputArrayIterator) LookupResult(org.apache.lucene.search.suggest.Lookup.LookupResult) Analyzer(org.apache.lucene.analysis.Analyzer) MockAnalyzer(org.apache.lucene.analysis.MockAnalyzer) BytesRef(org.apache.lucene.util.BytesRef)

Example 68 with InputArrayIterator

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

the class AnalyzingInfixSuggesterTest method testBothExactAndPrefix.

public void testBothExactAndPrefix() throws Exception {
    Analyzer a = new MockAnalyzer(random(), MockTokenizer.WHITESPACE, false);
    AnalyzingInfixSuggester suggester = new AnalyzingInfixSuggester(newDirectory(), a, a, 3, false);
    suggester.build(new InputArrayIterator(new Input[0]));
    suggester.add(new BytesRef("the pen is pretty"), null, 10, new BytesRef("foobaz"));
    suggester.refresh();
    List<LookupResult> results = suggester.lookup(TestUtil.stringToCharSequence("pen p", random()), 10, true, true);
    assertEquals(1, results.size());
    assertEquals("the pen is pretty", results.get(0).key);
    assertEquals("the <b>pen</b> is <b>p</b>retty", results.get(0).highlightKey);
    assertEquals(10, results.get(0).value);
    assertEquals(new BytesRef("foobaz"), results.get(0).payload);
    suggester.close();
    a.close();
}
Also used : Input(org.apache.lucene.search.suggest.Input) MockAnalyzer(org.apache.lucene.analysis.MockAnalyzer) InputArrayIterator(org.apache.lucene.search.suggest.InputArrayIterator) LookupResult(org.apache.lucene.search.suggest.Lookup.LookupResult) Analyzer(org.apache.lucene.analysis.Analyzer) MockAnalyzer(org.apache.lucene.analysis.MockAnalyzer) BytesRef(org.apache.lucene.util.BytesRef)

Example 69 with InputArrayIterator

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

the class AnalyzingInfixSuggesterTest method testConstructorDefaults.

private void testConstructorDefaults(AnalyzingInfixSuggester suggester, Input[] keys, Analyzer a, boolean allTermsRequired, boolean highlight) throws IOException {
    AnalyzingInfixSuggester suggester2 = new AnalyzingInfixSuggester(newDirectory(), a, a, 3, false, allTermsRequired, highlight);
    suggester2.build(new InputArrayIterator(keys));
    CharSequence key = TestUtil.stringToCharSequence("penny ea", random());
    List<LookupResult> results1 = suggester.lookup(key, 10, allTermsRequired, highlight);
    List<LookupResult> results2 = suggester2.lookup(key, false, 10);
    assertEquals(results1.size(), results2.size());
    assertEquals(results1.get(0).key, results2.get(0).key);
    assertEquals(results1.get(0).highlightKey, results2.get(0).highlightKey);
    suggester2.close();
}
Also used : InputArrayIterator(org.apache.lucene.search.suggest.InputArrayIterator) LookupResult(org.apache.lucene.search.suggest.Lookup.LookupResult)

Example 70 with InputArrayIterator

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

the class AnalyzingInfixSuggesterTest method testBasic.

public void testBasic() throws Exception {
    Input[] keys = new Input[] { new Input("lend me your ear", 8, new BytesRef("foobar")), new Input("a penny saved is a penny earned", 10, new BytesRef("foobaz")) };
    Analyzer a = new MockAnalyzer(random(), MockTokenizer.WHITESPACE, false);
    AnalyzingInfixSuggester suggester = new AnalyzingInfixSuggester(newDirectory(), a, a, 3, false);
    suggester.build(new InputArrayIterator(keys));
    List<LookupResult> results = suggester.lookup(TestUtil.stringToCharSequence("ear", random()), 10, true, true);
    assertEquals(2, results.size());
    assertEquals("a penny saved is a penny earned", results.get(0).key);
    assertEquals("a penny saved is a penny <b>ear</b>ned", results.get(0).highlightKey);
    assertEquals(10, results.get(0).value);
    assertEquals("foobaz", results.get(0).payload.utf8ToString());
    assertEquals("lend me your ear", results.get(1).key);
    assertEquals("lend me your <b>ear</b>", results.get(1).highlightKey);
    assertEquals(8, results.get(1).value);
    assertEquals(new BytesRef("foobar"), results.get(1).payload);
    results = suggester.lookup(TestUtil.stringToCharSequence("ear ", random()), 10, true, true);
    assertEquals(1, results.size());
    assertEquals("lend me your ear", results.get(0).key);
    assertEquals("lend me your <b>ear</b>", results.get(0).highlightKey);
    assertEquals(8, results.get(0).value);
    assertEquals(new BytesRef("foobar"), results.get(0).payload);
    results = suggester.lookup(TestUtil.stringToCharSequence("pen", random()), 10, true, true);
    assertEquals(1, results.size());
    assertEquals("a penny saved is a penny earned", results.get(0).key);
    assertEquals("a <b>pen</b>ny saved is a <b>pen</b>ny earned", results.get(0).highlightKey);
    assertEquals(10, results.get(0).value);
    assertEquals(new BytesRef("foobaz"), results.get(0).payload);
    results = suggester.lookup(TestUtil.stringToCharSequence("p", random()), 10, true, true);
    assertEquals(1, results.size());
    assertEquals("a penny saved is a penny earned", results.get(0).key);
    assertEquals("a <b>p</b>enny saved is a <b>p</b>enny earned", results.get(0).highlightKey);
    assertEquals(10, results.get(0).value);
    assertEquals(new BytesRef("foobaz"), results.get(0).payload);
    results = suggester.lookup(TestUtil.stringToCharSequence("money penny", random()), 10, false, true);
    assertEquals(1, results.size());
    assertEquals("a penny saved is a penny earned", results.get(0).key);
    assertEquals("a <b>penny</b> saved is a <b>penny</b> earned", results.get(0).highlightKey);
    assertEquals(10, results.get(0).value);
    assertEquals(new BytesRef("foobaz"), results.get(0).payload);
    results = suggester.lookup(TestUtil.stringToCharSequence("penny ea", random()), 10, false, true);
    assertEquals(2, results.size());
    assertEquals("a penny saved is a penny earned", results.get(0).key);
    assertEquals("a <b>penny</b> saved is a <b>penny</b> <b>ea</b>rned", results.get(0).highlightKey);
    assertEquals("lend me your ear", results.get(1).key);
    assertEquals("lend me your <b>ea</b>r", results.get(1).highlightKey);
    results = suggester.lookup(TestUtil.stringToCharSequence("money penny", random()), 10, false, false);
    assertEquals(1, results.size());
    assertEquals("a penny saved is a penny earned", results.get(0).key);
    assertNull(results.get(0).highlightKey);
    testConstructorDefaults(suggester, keys, a, true, true);
    testConstructorDefaults(suggester, keys, a, true, false);
    testConstructorDefaults(suggester, keys, a, false, false);
    testConstructorDefaults(suggester, keys, a, false, true);
    suggester.close();
    a.close();
}
Also used : Input(org.apache.lucene.search.suggest.Input) MockAnalyzer(org.apache.lucene.analysis.MockAnalyzer) InputArrayIterator(org.apache.lucene.search.suggest.InputArrayIterator) LookupResult(org.apache.lucene.search.suggest.Lookup.LookupResult) Analyzer(org.apache.lucene.analysis.Analyzer) MockAnalyzer(org.apache.lucene.analysis.MockAnalyzer) BytesRef(org.apache.lucene.util.BytesRef)

Aggregations

InputArrayIterator (org.apache.lucene.search.suggest.InputArrayIterator)76 Input (org.apache.lucene.search.suggest.Input)71 MockAnalyzer (org.apache.lucene.analysis.MockAnalyzer)67 Analyzer (org.apache.lucene.analysis.Analyzer)65 LookupResult (org.apache.lucene.search.suggest.Lookup.LookupResult)48 Directory (org.apache.lucene.store.Directory)43 BytesRef (org.apache.lucene.util.BytesRef)26 Path (java.nio.file.Path)17 MockTokenizer (org.apache.lucene.analysis.MockTokenizer)11 Tokenizer (org.apache.lucene.analysis.Tokenizer)10 Reader (java.io.Reader)8 ArrayList (java.util.ArrayList)8 CannedTokenStream (org.apache.lucene.analysis.CannedTokenStream)8 StandardAnalyzer (org.apache.lucene.analysis.standard.StandardAnalyzer)7 TokenStream (org.apache.lucene.analysis.TokenStream)6 HashSet (java.util.HashSet)5 CharArraySet (org.apache.lucene.analysis.CharArraySet)5 Token (org.apache.lucene.analysis.Token)5 InputStream (java.io.InputStream)4 OutputStream (java.io.OutputStream)4