D063520
D063520

Reputation: 153

How to implement near-real time autocompletion using lucene?

Lucene offers different Autocompletion options:

org.apache.lucene.search.suggest.Lookup

I was using the AnalyzingSuggester which is good but it does not support changing data, i.e. when the index changes one needs to reindex everything.

Therefore I tries out the AnalyzingInfixSuggester. This has and add method and an update method but no remove.

Does someone know if it is possible to implement near-real time suggestions with pure lucene?

Upvotes: 1

Views: 177

Answers (1)

D063520
D063520

Reputation: 153

I do not know why this is not part of the public implementation. At the end I extended the AnalyzingInfixSuggester like this:

public class MyAnalyzingInfixSuggester extends AnalyzingInfixSuggester {

    public MyAnalyzingInfixSuggester(Directory dir, Analyzer analyzer) throws IOException {
        super(dir, analyzer);
    }

    public void remove(String text) throws IOException, NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        // call method ensureOpen via reflection since it is private
        Method method = AnalyzingInfixSuggester.class.getDeclaredMethod("ensureOpen");
        method.setAccessible(true);
        method.invoke(this);
        Query query1 = new TermQuery(new Term(TEXT_FIELD_NAME, text.toLowerCase()));

        BooleanQuery booleanQuery = new BooleanQuery.Builder()
                .add(query1, BooleanClause.Occur.MUST)
                .build();
        writer.deleteDocuments(booleanQuery);
    }
}

Upvotes: 2

Related Questions