I am indexing documents over lucene, the indexes ares created, but when do realize a search, get 0 hits.
I don't know where are the error is in indexing or searching.
Indexing
Directory directory = FSDirectory.open(Paths.get("C:\\indexes\\teste"));
Analyzer analyzer = new StandardAnalyzer();
IndexWriterConfig config = new IndexWriterConfig(analyzer);
IndexWriter indexWriter = new IndexWriter(directory, config);
Document document = new Document();
document.add(new StringField("id", "1", Store.YES));
document.add(new StringField("name", "Pedro Henriques", Store.YES));
document.add(new StringField("displayName", "Pedro Rosa", Store.YES));
document.add(new StringField("segment", "segment xpto", Store.YES));
indexWriter.addDocument(document);
indexWriter.commit();
indexWriter.close();
directory.close();
Search
Directory directory = FSDirectory.open(Paths.get("C:\\indexes\\teste"));
IndexReader reader = DirectoryReader.open(directory);
IndexSearcher searcher = new IndexSearcher(reader);
Analyzer analyzer = new StandardAnalyzer();
QueryParser parser = new QueryParser("", analyzer);
Query query = parser.parse("name:Henriques");
log.info("Consulta analisada-> " + query);
TopDocs docs = searcher.search(query, 100);
for (ScoreDoc sd : docs.scoreDocs) {
Document doc = searcher.doc(sd.doc);
log.info(doc.toString());
}
docs.scoreDocs is empty.
My Lucene dependencies are
<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-core</artifactId>
<version>8.2.0</version>
</dependency>
<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-analyzers-common</artifactId>
<version>8.2.0</version>
</dependency>
<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-queries</artifactId>
<version>8.2.0</version>
</dependency>
<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-queryparser</artifactId>
<version>8.2.0</version>
</dependency>
My Java version is 11.
thank you!
The field 'name' cannot be of type StringField, because fields of this type cannot have spaces, special characters or uppercase letters.
In this case the TextField type is used.
document.add(new TextField("name", "Pedro Henriques", Store.YES));