Prevent KeywordTokenizer from creating multiple key-value pairs
I use the Lucene java QueryParser
with KeywordAnalyzer
. A query like topic:(hello world)
is broken up in to multiple parts by the KeywordTokenizer
so the resulting Query object looks like this topic:(hello) topic:(world)
i.e. Instead of one, I now have two key-value pairs. I would like the QueryParser
to interpret he开发者_StackOverflow社区llo world
as one value, without using double quotes. What is the best way to do so?
Parsing topic:("hello world")
results in a single key value combination but, using double quotes is not an option.
I am not using the Lucene search engine. I am using Lucene's QueryParser just for parsing the query, not for searching. The text Hello World
is entered at runtime, by the user so that can change. I would like KeywordTokenizer
to treat Hello World
as one Token instead of parsing splitting it in to two Tokens.
You'll need to use a BooleanQuery. Here's a code snippet using the .NET port of Lucene. This should work with both the KeywordAnalyzer and the StandardAnalyzer.
var luceneAnalyzer = new KeywordAnalyzer();
var query1 = new QueryParser("Topic", luceneAnalyzer).Parse("hello");
var query2 = new QueryParser("Topic", luceneAnalyzer).Parse("world");
BooleanQuery filterQuery = new BooleanQuery();
filterQuery.Add(query1, BooleanClause.Occur.MUST);
filterQuery.Add(query1, BooleanClause.Occur.MUST);
TopDocs results = searcher.Search(filterQuery);
You can construct the query directly as follows. This preserves the space.
Query query = new TermQuery(new Term("field", "value has space"));
If you print query as System.out.println(query);
you will see the following result.
field:value has space
精彩评论