Lucene TermQuery and QueryParser

2020-03-31 06:44发布

I have 2 lucene queries:

1)

Term term = new Term(Properties.LUCENE_APPARTMENT_ADDRESS,address);
Query termQuery = new TermQuery(term);
TopDocs topDocs = indexSearcher.search(termQuery, 20); 

and 2)

QueryParser queryParser = new QueryParser(Version.LUCENE_36, Properties.LUCENE_APPARTMENT_ADDRESS, analyzer);
Query query = queryParser.parse(address);
ScoreDoc[] queryResults = indexSearcher.search(query, 20).scoreDocs; 

Why the first one returns 1 result where the 2nd returns nothing? (the address is one word or more)

标签: java lucene
1条回答
Deceive 欺骗
2楼-- · 2020-03-31 07:39

When you use QueryParser, it uses analyzer which does the same sequence of actions as when during the indexing (tokenization, lowercasing, stopwords, etc.).

When you use raw TermQuery, you need to do all these steps yourself.

I guess your analyzer does something special about Properties.LUCENE_APPARTMENT_ADDRESS and you are not when passing the address to TermQuery hence the search results discrepancy.

查看更多
登录 后发表回答