Hello everyone,
java.lang.IllegalArgumentException: completer is not a SuggestField, is the
exception I got when I run a completion query against a suggest field.
At index-time I have a CustomAnalyzer like:
ClasspathResourceLoader resourceLoader = new
ClasspathResourceLoader(Indexer.class);
Analyzer analyzer = CustomAnalyzer.builder(resourceLoader)
.withTokenizer(StandardTokenizerFactory.class)
.addTokenFilter(StandardFilterFactory.class)
.addTokenFilter(StopFilterFactory.class,
"ignoreCase", "true")
.addTokenFilter(LowerCaseFilterFactory.class)
.addTokenFilter(EnglishPossessiveFilterFactory.class)
.addTokenFilter(ASCIIFoldingFilterFactory.class)
.build();
CompletionAnalyzer completionAnalyzer = new CompletionAnalyzer(analyzer);
Directory directory = FSDirectory.open(Paths.get("completionIndex"));
IndexWriterConfig writerConfig = new IndexWriterConfig(completionAnalyzer);
writerConfig.setOpenMode(IndexWriterConfig.OpenMode.CREATE);
IndexWriter indexWriter = new IndexWriter(directory, writerConfig);
Then I walk along a file tree to index the suggester field like:
Files.walkFileTree(Paths.get("docs"), new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path ofEachFile, BasicFileAttributes attr)
throws IOException {
Document doc = new Document();
SuggestField suggestField;
List<String> fileLines = Files.readAllLines(ofEachFile);
List<String> contentLines = fileLines.subList(6, fileLines.size());
StringBuilder sb = new StringBuilder();
for(int i = 0; i < contentLines.size(); i++) {
sb.append(contentLines.get(i));
sb.append("\n");
}
String content = sb.toString();
suggestField = new SuggestField("completer", content, 4);
doc.removeField("completer");
doc.add(suggestField);
indexWriter.addDocument(doc);
return FileVisitResult.CONTINUE;
}
});
indexWriter.close();
}
At search time, I got the same CustomAnalyzer as above and then:
// CustomAnalyzer...
Directory indexDirectory = FSDirectory.open(Paths.get("completionIndex"));
IndexReader indexReader = DirectoryReader.open(indexDirectory);
// IndexSearcher indexSearcher = new IndexSearcher(indexReader);
SuggestIndexSearcher suggestSearcher = new SuggestIndexSearcher(indexReader);
//Term term = new Term("completer", "blatavts");
CompletionQuery query = new PrefixCompletionQuery(completionAnalyzer,
new Term("completer", "blavats"));
TopSuggestDocs suggestDocs = sis.suggest(query, 4, false);
int docID;
Document doc;
for(ScoreDoc sd : suggestDocs.scoreDocs) {
docID = sd.doc;
doc = sis.doc(docID);
System.out.println(sd);
}
And got the exception.