简体   繁体   中英

Lucene suggest: "is not a SuggestField" exception when using a CompletionQuery

I am trying to implement search suggestions for my app. Actually I need a kind of "multi-term prefix query" and I was trying to use a PrefixCompletionQuery. The problem is that an IllegalArgumentException is thrown when "search" or "suggest" methods are called from a SuggestIndexSearcher object.

I wrote a sample code to reproduce the problem:

public static void main(String[] args) throws IOException {
    RAMDirectory dir = new RAMDirectory(); //just for this experiment
    Analyzer analyzer = new CompletionAnalyzer(new StandardAnalyzer());
    IndexWriter writer = new IndexWriter(dir, new IndexWriterConfig(analyzer));
    
    
    var doc = new Document();
    doc.add(new SuggestField("suggest", "Hi everybody!",4));
    writer.addDocument(doc);
    doc = new Document();
    doc.add(new SuggestField("suggest", "nice to meet you",4));
    writer.addDocument(doc);
    writer.commit(); // maybe redundant
    writer.close();
    var reader = DirectoryReader.open(dir);
    var searcher = new SuggestIndexSearcher(reader);
    var query = new PrefixCompletionQuery(analyzer, new Term("suggest", "everyb"));
    TopDocs results = searcher.search(query, 5);
    for (var res : results.scoreDocs) {
        System.out.println(reader.document(res.doc).get("id"));
    }
}

And this is what i get:

Exception in thread "main" java.lang.IllegalArgumentException: suggest is not a SuggestField
at org.apache.lucene.search.suggest.document.CompletionWeight.bulkScorer(CompletionWeight.java:86)
at org.apache.lucene.search.IndexSearcher.search(IndexSearcher.java:658)
at org.apache.lucene.search.IndexSearcher.search(IndexSearcher.java:445)
at org.apache.lucene.search.IndexSearcher.search(IndexSearcher.java:574)
at org.apache.lucene.search.IndexSearcher.searchAfter(IndexSearcher.java:421)
at org.apache.lucene.search.IndexSearcher.search(IndexSearcher.java:432)
at experiments.main.main(main.java:67) #TopDocs results = searcher.search(query, 5);

Trying to be as complete as possible, the project depends on lucene-core 8.8.2 and lucene-suggest 8.8.2. Where am I wrong?

I think you have to change the posting format of your suggestion field by adding a custom codec to your index writer.

For example something like this:

RAMDirectory dir = new RAMDirectory();
Analyzer analyzer = new CompletionAnalyzer(new StandardAnalyzer());
IndexWriterConfig config = new IndexWriterConfig(analyzer);
Codec codec = new Lucene87Codec() {
    @Override
    public PostingsFormat getPostingsFormatForField(String field) {
        if (field.equals("suggest")) {
            return new Completion84PostingsFormat();
        }
        return super.getPostingsFormatForField(field);
    }
};
config.setCodec(codec);
IndexWriter indexWriter = new IndexWriter(dir, config);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM