我正在使用 Lucene 进行用户搜索。对于索引,我有以下代码
private void internalAddUser(User user) throws IOException {
Document document = new Document();
document.add(new Field("login", user.getLogin(), Field.Store.YES, Field.Index.NOT_ANALYZED));
document.add(new Field("firstName", user.getFirstName(), Field.Store.YES, Field.Index.NOT_ANALYZED));
document.add(new Field("lastName", user.getLastName(), Field.Store.YES, Field.Index.NOT_ANALYZED));
userIndexWriter.addDocument(document);
}
对于搜索,我使用以下代码,但没有得到任何结果。
@Override
@Cacheable("user-prefix-cache")
public Collection<String> searchUserByPrefix(String prefix) {
IndexSearcher searcher = null;
List<String> logins = new ArrayList<String>();
try {
searcher = userSearcherManager.acquire();
BooleanQuery booleanQuery = new BooleanQuery();
Query query1 = new TermQuery(new Term("login", prefix));
Query query2 = new TermQuery(new Term("firstName", prefix));
Query query3 = new TermQuery(new Term("lastName", prefix));
booleanQuery.add(query1, BooleanClause.Occur.SHOULD);
booleanQuery.add(query2, BooleanClause.Occur.SHOULD);
booleanQuery.add(query3, BooleanClause.Occur.SHOULD);
SortField sortField = new SortField("login", SortField.STRING, true);
Sort sort = new Sort(sortField);
TopDocs topDocs = searcher.search(booleanQuery, DEFAULT_TOP_N_SEARCH_USER, sort);
int totalHits = topDocs.totalHits;
if (totalHits == 0) {
return new ArrayList<String>();
}
ScoreDoc[] scoreDocArray = topDocs.scoreDocs;
for (int i = 0; i < scoreDocArray.length; i++) {
int documentId = scoreDocArray[i].doc;
Document document = searcher.doc(documentId);
logins.add(document.get("login"));
}
} catch (IOException e) {
log.error("A Lucene query had a I/O error : " + e.getMessage());
if (log.isDebugEnabled()) {
e.printStackTrace();
}
} finally {
try {
userSearcherManager.release(searcher);
} catch (IOException e) {
log.error("The Lucene searcher could not be given back to the searcherManager pool. " +
e.getMessage());
if (log.isDebugEnabled()) {
e.printStackTrace();
}
}
}
return logins;
}
我不是一个 lucene 期望,但我不确定它为什么不工作。有没有人有任何想法。
[DEBUG] in.daydiary.web.rest.SearchController - REST request to find users starting with : j
[DEBUG] in.daydiary.web.rest.SearchController - REST request to find users starting with : j
[DEBUG] in.daydiary.web.rest.SearchController - REST request to find users starting with : ju
[DEBUG] in.daydiary.web.rest.SearchController - REST request to find users starting with : jul
[DEBUG] in.daydiary.web.rest.SearchController - REST request to find users starting with : juli
[DEBUG] in.daydiary.web.rest.SearchController - REST request to find users starting with : julia
[DEBUG] in.daydiary.web.rest.SearchController - REST request to find users starting with : julianb
[DEBUG] in.daydiary.web.rest.SearchController - REST request to find users starting with : julianb
[DEBUG] in.daydiary.web.rest.SearchController - REST request to find users starting with : julian
[DEBUG] in.daydiary.web.rest.SearchController - REST request to find users starting with : julia
[DEBUG] in.daydiary.web.rest.SearchController - REST request to find users starting with : juli
[DEBUG] in.daydiary.web.rest.SearchController - REST request to find users starting with : jul
[DEBUG] in.daydiary.web.rest.SearchController - REST request to find users starting with : ju
[DEBUG] in.daydiary.web.rest.SearchController - REST request to find users starting with : j