简体   繁体   中英

Lucene(QueryParser) in Java

I create query by QueryParser:

QueryParser parser = new QueryParser(Version.LUCENE_30, "Text", new RussianAnalayzer());
parser.setDefaultOperator(QueryParser.Operator.AND);
Query query = parser.parse(searchString);

When I use the phrase where the words have variations, I'm getting the united with "AND", like: "term1 term2" -> "term1 AND "term2_1 AND term2_2"". How can I get query like : "term1 AND "term2_1 OR term2_2"" without splitting string for words?

Great thanks.

I'm not entirely sure I understand you. How is term2 split into multiple terms? Is it just a list of terms stored in a variable that you're talking about, something like: term1 = "term1" term2 = "term2_1 term2_2" query = term1 + " " + term2;

Or is it really only two terms, which are split up automatically (by the analyzer or something perhaps)?

At any rate, I think it would probably serve your purpose to change the second line given to:

parser.setDefaultOperator(QueryParser.Operator.OR);
//Or just delete this line if you prefer, OR is the default behavior.

and pass a query that makes sense given that behavior, such as:

"term1 AND (term2)"

Which is, presumably, equivalent to:

"term1 AND (term2_1 term2_2)"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM