Reputation: 16651
I am using Lucene for my user search. For indexing i have the following code
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);
}
and for search I use the following code but i am not getting any results.
@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;
}
I am not a lucene expect but i am not sure why it is not working. Does anybody have any idea.
[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
Upvotes: 0
Views: 3151
Reputation: 2843
First. Care to provide the lucene version.
Second. You can certainly see the indexes. Get luke of the same version as that of Lucene..You will be able to see the indexed data inside luke.. Run it just by executing java -jar jarname.
Third. Check if the Analyzer used during the indexing process and analyzer used during the searching process is same.. This is a very common mistake made by everyone.
Fourth. Since you are not getting hit, please refer this excellent link which mentions some points which might be leading to no HITS found.
http://wiki.apache.org/lucene-java/LuceneFAQ#Why_am_I_getting_no_hits_.2F_incorrect_hits.3F
Upvotes: 0
Reputation: 33341
Two things look suspect here.
All your fields are Field.Index.NOT_ANALYZED
. This means they will not be tokenized, but rather will only match an exact match on the entire field, as you are searching here. This will be case-sensitive as well. Seeing exactly what data is indexed, and what you are querying for would help understanding whether this is actually the problem.
Second, your method's name indicates you want a prefix search, but you aren't using a prefix search. A TermQuery
will only get exact matches. A PrefixQuery
would be used to search by prefix, which is used exactly like a TermQuery:
Query query1 = new PrefixQuery(new Term("login", prefix));
booleanQuery.add(query1, BooleanClause.Occur.SHOULD);
Upvotes: 1