Trouble searching for two terms using lucene
I have written a following code in my project:
final IndexSearcher indexSearcher = new IndexSearcher(INDEXING_DIRECTORY, true);
final Query query = new QueryParser(Version.LUCENE_33, "keywords", new StandardAnalyzer(Version.LUCENE_33)).parse("cats movies");
final TopScoreDocCollector collector = TopScoreDocCollector.create(10, true);
indexSearcher.search(query, collector);
final ScoreDoc[] hits = collector.topDocs(0, 10).scoreDocs;
The task is very trivial.
I, for example, have a stored record with an indexed "keywords" field. The keywords for example might be similar to "Tons of movies with a funny cats".
The problem is that my code above will return records if search query will be "funny cat开发者_如何学JAVAs" (word order like in the indexed field), but will fail on "cats movies". How should I write my query, so it would match any words order, and if is possible searching for a similar words also?
Most likely "cats movies" will be parsed as PhraseQuery
. PhraseQueries respect ordering. What you want is to have a BooleanQuery
with two TermQueries
combined with AND
.
final Query query = new QueryParser(Version.LUCENE_33, "keywords", new StandardAnalyzer(Version.LUCENE_33)).parse("+cats AND +movies");
Some more examples are listed here. Some may be already outdated.
Similar words is a rather hard task because you need to have at least some sort of wordlist or database which aligns synonyms.
精彩评论