ユーザー検索に 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;
}
私はルセンを期待していませんが、なぜ機能しないのかわかりません。誰にもアイデアはありますか。
[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