Search in sources :

Example 41 with LookupResult

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

the class WFSTCompletionTest method testBasic.

public void testBasic() throws Exception {
    Input[] keys = new Input[] { new Input("foo", 50), new Input("bar", 10), new Input("barbar", 12), new Input("barbara", 6) };
    Random random = new Random(random().nextLong());
    Directory tempDir = getDirectory();
    WFSTCompletionLookup suggester = new WFSTCompletionLookup(tempDir, "wfst");
    suggester.build(new InputArrayIterator(keys));
    // top N of 2, but only foo is available
    List<LookupResult> results = suggester.lookup(TestUtil.stringToCharSequence("f", random), false, 2);
    assertEquals(1, results.size());
    assertEquals("foo", results.get(0).key.toString());
    assertEquals(50, results.get(0).value, 0.01F);
    // make sure we don't get a dup exact suggestion:
    results = suggester.lookup(TestUtil.stringToCharSequence("foo", random), false, 2);
    assertEquals(1, results.size());
    assertEquals("foo", results.get(0).key.toString());
    assertEquals(50, results.get(0).value, 0.01F);
    // top N of 1 for 'bar': we return this even though barbar is higher
    results = suggester.lookup(TestUtil.stringToCharSequence("bar", random), false, 1);
    assertEquals(1, results.size());
    assertEquals("bar", results.get(0).key.toString());
    assertEquals(10, results.get(0).value, 0.01F);
    // top N Of 2 for 'b'
    results = suggester.lookup(TestUtil.stringToCharSequence("b", random), false, 2);
    assertEquals(2, results.size());
    assertEquals("barbar", results.get(0).key.toString());
    assertEquals(12, results.get(0).value, 0.01F);
    assertEquals("bar", results.get(1).key.toString());
    assertEquals(10, results.get(1).value, 0.01F);
    // top N of 3 for 'ba'
    results = suggester.lookup(TestUtil.stringToCharSequence("ba", random), false, 3);
    assertEquals(3, results.size());
    assertEquals("barbar", results.get(0).key.toString());
    assertEquals(12, results.get(0).value, 0.01F);
    assertEquals("bar", results.get(1).key.toString());
    assertEquals(10, results.get(1).value, 0.01F);
    assertEquals("barbara", results.get(2).key.toString());
    assertEquals(6, results.get(2).value, 0.01F);
    tempDir.close();
}
Also used : Input(org.apache.lucene.search.suggest.Input) InputArrayIterator(org.apache.lucene.search.suggest.InputArrayIterator) LookupResult(org.apache.lucene.search.suggest.Lookup.LookupResult) Directory(org.apache.lucene.store.Directory)

Example 42 with LookupResult

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

the class Suggester method getSuggestions.

@Override
public SpellingResult getSuggestions(SpellingOptions options) throws IOException {
    LOG.debug("getSuggestions: " + options.tokens);
    if (lookup == null) {
        LOG.info("Lookup is null - invoke spellchecker.build first");
        return EMPTY_RESULT;
    }
    SpellingResult res = new SpellingResult();
    CharsRef scratch = new CharsRef();
    for (Token t : options.tokens) {
        scratch.chars = t.buffer();
        scratch.offset = 0;
        scratch.length = t.length();
        boolean onlyMorePopular = (options.suggestMode == SuggestMode.SUGGEST_MORE_POPULAR) && !(lookup instanceof WFSTCompletionLookup) && !(lookup instanceof AnalyzingSuggester);
        List<LookupResult> suggestions = lookup.lookup(scratch, onlyMorePopular, options.count);
        if (suggestions == null) {
            continue;
        }
        if (options.suggestMode != SuggestMode.SUGGEST_MORE_POPULAR) {
            Collections.sort(suggestions);
        }
        for (LookupResult lr : suggestions) {
            res.add(t, lr.key.toString(), (int) lr.value);
        }
    }
    return res;
}
Also used : WFSTCompletionLookup(org.apache.lucene.search.suggest.fst.WFSTCompletionLookup) SpellingResult(org.apache.solr.spelling.SpellingResult) LookupResult(org.apache.lucene.search.suggest.Lookup.LookupResult) Token(org.apache.lucene.analysis.Token) AnalyzingSuggester(org.apache.lucene.search.suggest.analyzing.AnalyzingSuggester) CharsRef(org.apache.lucene.util.CharsRef)

Example 43 with LookupResult

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

the class SolrSuggester method getSuggestions.

/** Returns suggestions based on the {@link SuggesterOptions} passed */
public SuggesterResult getSuggestions(SuggesterOptions options) throws IOException {
    LOG.debug("getSuggestions: " + options.token);
    if (lookup == null) {
        LOG.info("Lookup is null - invoke suggest.build first");
        return EMPTY_RESULT;
    }
    SuggesterResult res = new SuggesterResult();
    List<LookupResult> suggestions;
    if (options.contextFilterQuery == null) {
        //TODO: this path needs to be fixed to accept query params to override configs such as allTermsRequired, highlight
        suggestions = lookup.lookup(options.token, false, options.count);
    } else {
        BooleanQuery query = parseContextFilterQuery(options.contextFilterQuery);
        suggestions = lookup.lookup(options.token, query, options.count, options.allTermsRequired, options.highlight);
        if (suggestions == null) {
            // Context filtering not supported/configured by lookup
            // Silently ignore filtering and serve a result by querying without context filtering
            LOG.debug("Context Filtering Query not supported by {}", lookup.getClass());
            suggestions = lookup.lookup(options.token, false, options.count);
        }
    }
    res.add(getName(), options.token.toString(), suggestions);
    return res;
}
Also used : BooleanQuery(org.apache.lucene.search.BooleanQuery) LookupResult(org.apache.lucene.search.suggest.Lookup.LookupResult)

Example 44 with LookupResult

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

the class PersistenceTest method runTest.

private void runTest(Class<? extends Lookup> lookupClass, boolean supportsExactWeights) throws Exception {
    // Add all input keys.
    Lookup lookup;
    Directory tempDir = getDirectory();
    if (lookupClass == TSTLookup.class) {
        lookup = new TSTLookup(tempDir, "suggest");
    } else if (lookupClass == FSTCompletionLookup.class) {
        lookup = new FSTCompletionLookup(tempDir, "suggest");
    } else {
        lookup = lookupClass.newInstance();
    }
    Input[] keys = new Input[this.keys.length];
    for (int i = 0; i < keys.length; i++) keys[i] = new Input(this.keys[i], i);
    lookup.build(new InputArrayIterator(keys));
    // Store the suggester.
    Path storeDir = createTempDir(LuceneTestCase.getTestClass().getSimpleName());
    lookup.store(Files.newOutputStream(storeDir.resolve("lookup.dat")));
    // Re-read it from disk.
    lookup = lookupClass.newInstance();
    lookup.load(Files.newInputStream(storeDir.resolve("lookup.dat")));
    // Assert validity.
    Random random = random();
    long previous = Long.MIN_VALUE;
    for (Input k : keys) {
        List<LookupResult> list = lookup.lookup(TestUtil.bytesToCharSequence(k.term, random), false, 1);
        assertEquals(1, list.size());
        LookupResult lookupResult = list.get(0);
        assertNotNull(k.term.utf8ToString(), lookupResult.key);
        if (supportsExactWeights) {
            assertEquals(k.term.utf8ToString(), k.v, lookupResult.value);
        } else {
            assertTrue(lookupResult.value + ">=" + previous, lookupResult.value >= previous);
            previous = lookupResult.value;
        }
    }
    tempDir.close();
}
Also used : Path(java.nio.file.Path) TSTLookup(org.apache.lucene.search.suggest.tst.TSTLookup) Random(java.util.Random) LookupResult(org.apache.lucene.search.suggest.Lookup.LookupResult) JaspellLookup(org.apache.lucene.search.suggest.jaspell.JaspellLookup) FSTCompletionLookup(org.apache.lucene.search.suggest.fst.FSTCompletionLookup) TSTLookup(org.apache.lucene.search.suggest.tst.TSTLookup) FSTCompletionLookup(org.apache.lucene.search.suggest.fst.FSTCompletionLookup) Directory(org.apache.lucene.store.Directory)

Example 45 with LookupResult

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

the class AnalyzingInfixSuggesterTest method testBasicContext.

// LUCENE-5528 and LUCENE-6464
public void testBasicContext() throws Exception {
    Input[] keys = new Input[] { new Input("lend me your ear", 8, new BytesRef("foobar"), asSet("foo", "bar")), new Input("a penny saved is a penny earned", 10, new BytesRef("foobaz"), asSet("foo", "baz")) };
    Path tempDir = createTempDir("analyzingInfixContext");
    for (int iter = 0; iter < 2; iter++) {
        AnalyzingInfixSuggester suggester;
        Analyzer a = new MockAnalyzer(random(), MockTokenizer.WHITESPACE, false);
        if (iter == 0) {
            suggester = new AnalyzingInfixSuggester(newFSDirectory(tempDir), a, a, 3, false);
            suggester.build(new InputArrayIterator(keys));
        } else {
            // Test again, after close/reopen:
            suggester = new AnalyzingInfixSuggester(newFSDirectory(tempDir), a, a, 3, false);
        }
        // No context provided, all results returned
        List<LookupResult> results = suggester.lookup(TestUtil.stringToCharSequence("ear", random()), 10, true, true);
        assertEquals(2, results.size());
        LookupResult result = results.get(0);
        assertEquals("a penny saved is a penny earned", result.key);
        assertEquals("a penny saved is a penny <b>ear</b>ned", result.highlightKey);
        assertEquals(10, result.value);
        assertEquals(new BytesRef("foobaz"), result.payload);
        assertNotNull(result.contexts);
        assertEquals(2, result.contexts.size());
        assertTrue(result.contexts.contains(new BytesRef("foo")));
        assertTrue(result.contexts.contains(new BytesRef("baz")));
        result = results.get(1);
        assertEquals("lend me your ear", result.key);
        assertEquals("lend me your <b>ear</b>", result.highlightKey);
        assertEquals(8, result.value);
        assertEquals(new BytesRef("foobar"), result.payload);
        assertNotNull(result.contexts);
        assertEquals(2, result.contexts.size());
        assertTrue(result.contexts.contains(new BytesRef("foo")));
        assertTrue(result.contexts.contains(new BytesRef("bar")));
        // Both have "foo" context:
        results = suggester.lookup(TestUtil.stringToCharSequence("ear", random()), asSet("foo"), 10, true, true);
        assertEquals(2, results.size());
        result = results.get(0);
        assertEquals("a penny saved is a penny earned", result.key);
        assertEquals("a penny saved is a penny <b>ear</b>ned", result.highlightKey);
        assertEquals(10, result.value);
        assertEquals(new BytesRef("foobaz"), result.payload);
        assertNotNull(result.contexts);
        assertEquals(2, result.contexts.size());
        assertTrue(result.contexts.contains(new BytesRef("foo")));
        assertTrue(result.contexts.contains(new BytesRef("baz")));
        result = results.get(1);
        assertEquals("lend me your ear", result.key);
        assertEquals("lend me your <b>ear</b>", result.highlightKey);
        assertEquals(8, result.value);
        assertEquals(new BytesRef("foobar"), result.payload);
        assertNotNull(result.contexts);
        assertEquals(2, result.contexts.size());
        assertTrue(result.contexts.contains(new BytesRef("foo")));
        assertTrue(result.contexts.contains(new BytesRef("bar")));
        // Only one has "bar" context:
        results = suggester.lookup(TestUtil.stringToCharSequence("ear", random()), asSet("bar"), 10, true, true);
        assertEquals(1, results.size());
        result = results.get(0);
        assertEquals("lend me your ear", result.key);
        assertEquals("lend me your <b>ear</b>", result.highlightKey);
        assertEquals(8, result.value);
        assertEquals(new BytesRef("foobar"), result.payload);
        assertNotNull(result.contexts);
        assertEquals(2, result.contexts.size());
        assertTrue(result.contexts.contains(new BytesRef("foo")));
        assertTrue(result.contexts.contains(new BytesRef("bar")));
        // None do not have "foo" context:
        Map<BytesRef, BooleanClause.Occur> contextInfo = new HashMap<>();
        contextInfo.put(new BytesRef("foo"), BooleanClause.Occur.MUST_NOT);
        results = suggester.lookup(TestUtil.stringToCharSequence("ear", random()), contextInfo, 10, true, true);
        assertEquals(0, results.size());
        // Only one does not have "bar" context:
        contextInfo.clear();
        contextInfo.put(new BytesRef("bar"), BooleanClause.Occur.MUST_NOT);
        results = suggester.lookup(TestUtil.stringToCharSequence("ear", random()), contextInfo, 10, true, true);
        assertEquals(1, results.size());
        result = results.get(0);
        assertEquals("a penny saved is a penny earned", result.key);
        assertEquals("a penny saved is a penny <b>ear</b>ned", result.highlightKey);
        assertEquals(10, result.value);
        assertEquals(new BytesRef("foobaz"), result.payload);
        assertNotNull(result.contexts);
        assertEquals(2, result.contexts.size());
        assertTrue(result.contexts.contains(new BytesRef("foo")));
        assertTrue(result.contexts.contains(new BytesRef("baz")));
        // Both have "foo" or "bar" context:
        results = suggester.lookup(TestUtil.stringToCharSequence("ear", random()), asSet("foo", "bar"), 10, true, true);
        assertEquals(2, results.size());
        result = results.get(0);
        assertEquals("a penny saved is a penny earned", result.key);
        assertEquals("a penny saved is a penny <b>ear</b>ned", result.highlightKey);
        assertEquals(10, result.value);
        assertEquals(new BytesRef("foobaz"), result.payload);
        assertNotNull(result.contexts);
        assertEquals(2, result.contexts.size());
        assertTrue(result.contexts.contains(new BytesRef("foo")));
        assertTrue(result.contexts.contains(new BytesRef("baz")));
        result = results.get(1);
        assertEquals("lend me your ear", result.key);
        assertEquals("lend me your <b>ear</b>", result.highlightKey);
        assertEquals(8, result.value);
        assertEquals(new BytesRef("foobar"), result.payload);
        assertNotNull(result.contexts);
        assertEquals(2, result.contexts.size());
        assertTrue(result.contexts.contains(new BytesRef("foo")));
        assertTrue(result.contexts.contains(new BytesRef("bar")));
        // Both have "bar" or "baz" context:
        results = suggester.lookup(TestUtil.stringToCharSequence("ear", random()), asSet("bar", "baz"), 10, true, true);
        assertEquals(2, results.size());
        result = results.get(0);
        assertEquals("a penny saved is a penny earned", result.key);
        assertEquals("a penny saved is a penny <b>ear</b>ned", result.highlightKey);
        assertEquals(10, result.value);
        assertEquals(new BytesRef("foobaz"), result.payload);
        assertNotNull(result.contexts);
        assertEquals(2, result.contexts.size());
        assertTrue(result.contexts.contains(new BytesRef("foo")));
        assertTrue(result.contexts.contains(new BytesRef("baz")));
        result = results.get(1);
        assertEquals("lend me your ear", result.key);
        assertEquals("lend me your <b>ear</b>", result.highlightKey);
        assertEquals(8, result.value);
        assertEquals(new BytesRef("foobar"), result.payload);
        assertNotNull(result.contexts);
        assertEquals(2, result.contexts.size());
        assertTrue(result.contexts.contains(new BytesRef("foo")));
        assertTrue(result.contexts.contains(new BytesRef("bar")));
        // Only one has "foo" and "bar" context:
        contextInfo.clear();
        contextInfo.put(new BytesRef("foo"), BooleanClause.Occur.MUST);
        contextInfo.put(new BytesRef("bar"), BooleanClause.Occur.MUST);
        results = suggester.lookup(TestUtil.stringToCharSequence("ear", random()), contextInfo, 10, true, true);
        assertEquals(1, results.size());
        result = results.get(0);
        assertEquals("lend me your ear", result.key);
        assertEquals("lend me your <b>ear</b>", result.highlightKey);
        assertEquals(8, result.value);
        assertEquals(new BytesRef("foobar"), result.payload);
        assertNotNull(result.contexts);
        assertEquals(2, result.contexts.size());
        assertTrue(result.contexts.contains(new BytesRef("foo")));
        assertTrue(result.contexts.contains(new BytesRef("bar")));
        // None have "bar" and "baz" context:
        contextInfo.clear();
        contextInfo.put(new BytesRef("bar"), BooleanClause.Occur.MUST);
        contextInfo.put(new BytesRef("baz"), BooleanClause.Occur.MUST);
        results = suggester.lookup(TestUtil.stringToCharSequence("ear", random()), contextInfo, 10, true, true);
        assertEquals(0, results.size());
        // None do not have "foo" and do not have "bar" context:
        contextInfo.clear();
        contextInfo.put(new BytesRef("foo"), BooleanClause.Occur.MUST_NOT);
        contextInfo.put(new BytesRef("bar"), BooleanClause.Occur.MUST_NOT);
        results = suggester.lookup(TestUtil.stringToCharSequence("ear", random()), contextInfo, 10, true, true);
        assertEquals(0, results.size());
        // Both do not have "bar" and do not have "baz" context:
        contextInfo.clear();
        contextInfo.put(new BytesRef("bar"), BooleanClause.Occur.MUST_NOT);
        contextInfo.put(new BytesRef("baz"), BooleanClause.Occur.MUST_NOT);
        results = suggester.lookup(TestUtil.stringToCharSequence("ear", random()), asSet("bar", "baz"), 10, true, true);
        assertEquals(2, results.size());
        result = results.get(0);
        assertEquals("a penny saved is a penny earned", result.key);
        assertEquals("a penny saved is a penny <b>ear</b>ned", result.highlightKey);
        assertEquals(10, result.value);
        assertEquals(new BytesRef("foobaz"), result.payload);
        assertNotNull(result.contexts);
        assertEquals(2, result.contexts.size());
        assertTrue(result.contexts.contains(new BytesRef("foo")));
        assertTrue(result.contexts.contains(new BytesRef("baz")));
        result = results.get(1);
        assertEquals("lend me your ear", result.key);
        assertEquals("lend me your <b>ear</b>", result.highlightKey);
        assertEquals(8, result.value);
        assertEquals(new BytesRef("foobar"), result.payload);
        assertNotNull(result.contexts);
        assertEquals(2, result.contexts.size());
        assertTrue(result.contexts.contains(new BytesRef("foo")));
        assertTrue(result.contexts.contains(new BytesRef("bar")));
        // Only one has "foo" and does not have "bar" context:
        contextInfo.clear();
        contextInfo.put(new BytesRef("foo"), BooleanClause.Occur.MUST);
        contextInfo.put(new BytesRef("bar"), BooleanClause.Occur.MUST_NOT);
        results = suggester.lookup(TestUtil.stringToCharSequence("ear", random()), contextInfo, 10, true, true);
        assertEquals(1, results.size());
        result = results.get(0);
        assertEquals("a penny saved is a penny earned", result.key);
        assertEquals("a penny saved is a penny <b>ear</b>ned", result.highlightKey);
        assertEquals(10, result.value);
        assertEquals(new BytesRef("foobaz"), result.payload);
        assertNotNull(result.contexts);
        assertEquals(2, result.contexts.size());
        assertTrue(result.contexts.contains(new BytesRef("foo")));
        assertTrue(result.contexts.contains(new BytesRef("baz")));
        //LUCENE-6464 Using the advanced context filtering by query. 
        //Note that this is just a sanity test as all the above tests run through the filter by query method
        BooleanQuery.Builder query = new BooleanQuery.Builder();
        suggester.addContextToQuery(query, new BytesRef("foo"), BooleanClause.Occur.MUST);
        suggester.addContextToQuery(query, new BytesRef("bar"), BooleanClause.Occur.MUST_NOT);
        results = suggester.lookup(TestUtil.stringToCharSequence("ear", random()), query.build(), 10, true, true);
        assertEquals(1, results.size());
        suggester.close();
        a.close();
    }
}
Also used : Path(java.nio.file.Path) BooleanQuery(org.apache.lucene.search.BooleanQuery) HashMap(java.util.HashMap) Analyzer(org.apache.lucene.analysis.Analyzer) MockAnalyzer(org.apache.lucene.analysis.MockAnalyzer) 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) BytesRef(org.apache.lucene.util.BytesRef)

Aggregations

LookupResult (org.apache.lucene.search.suggest.Lookup.LookupResult)65 Input (org.apache.lucene.search.suggest.Input)48 InputArrayIterator (org.apache.lucene.search.suggest.InputArrayIterator)48 MockAnalyzer (org.apache.lucene.analysis.MockAnalyzer)45 Analyzer (org.apache.lucene.analysis.Analyzer)43 Directory (org.apache.lucene.store.Directory)36 BytesRef (org.apache.lucene.util.BytesRef)22 ArrayList (java.util.ArrayList)14 Path (java.nio.file.Path)11 HashSet (java.util.HashSet)9 MockTokenizer (org.apache.lucene.analysis.MockTokenizer)7 Reader (java.io.Reader)6 CannedTokenStream (org.apache.lucene.analysis.CannedTokenStream)6 Tokenizer (org.apache.lucene.analysis.Tokenizer)6 HashMap (java.util.HashMap)5 Token (org.apache.lucene.analysis.Token)5 TokenStream (org.apache.lucene.analysis.TokenStream)5 LinkedList (java.util.LinkedList)4 CharArraySet (org.apache.lucene.analysis.CharArraySet)4 SuggesterResult (org.apache.solr.spelling.suggest.SuggesterResult)4