我正在使用Apache lucene Indexer Search搜索文本,但是在使用该工具时,我遇到以下问题。
If I indexed word "Steve" and if I want to search that word then If
I search using following possibilities like *S*, *St* , *Ste*, *Stev*
then It gives correct result.
But If I search using whole word like *Steve* then Its giving zero hits.
例如我的代码如下。
--------call to Search ------
TopDocs foundDocs2 = searchByFirstName("*Steve*", searcher);
-------------------------------------------------------------
private static TopDocs searchByFirstName(String firstName, IndexSearcher searcher) throws Exception
{
MultiFieldQueryParser mqp = new MultiFieldQueryParser(new String[]{"firstName"}, new StandardAnalyzer());
mqp.setAllowLeadingWildcard(true);
Query q =mqp.parse(firstName);
TopDocs hits = searcher.search(q, 10);
return hits;
}
如果我搜索整个单词,则返回零命中。
我的索引编写器如下。
writer = getIndexWriter();
List<Document> documents = new ArrayList<>();
Document document1 = createDocument(1, "Steve", "Richard", "richard.com");
writer.addDocument(document1);
writer.commit();
private static Document createDocument(Integer id, String firstName, String lastName, String website)
{
Document document = new Document();
document.add(new StringField("id", id.toString() , Field.Store.YES));
document.add(new TextField("firstName", firstName , Field.Store.YES));
document.add(new TextField("lastName", lastName , Field.Store.YES));
document.add(new TextField("website", website , Field.Store.YES));
return document;
}
private static IndexWriter createWriter() throws IOException
{
FSDirectory dir = FSDirectory.open(Paths.get(INDEX_DIR).toFile());
IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_44,new StandardAnalyzer(Version.LUCENE_44));
IndexWriter writer = new IndexWriter(dir, config);
return writer;
}