简体   繁体   中英

How to query lucene for 2 index fields?

I'd like to execute queries with lucene . But the lookup should not only be based on the input, but also on a 2nd parameter.

Example: imagine the lucene index should contain citynames and countrycodes .

Now, during lookup I already know which country the desired cityname should be in. SO I want to query the lucene index by cityname, but tell lucene to only look on the citynames where the countrycode matches.

It it possibel? If yes, how?

For a single attribute I would just set up the following:

QueryParser q = QueryParser(Version matchVersion, String f, Analyzer a)
Query q = queryParser.parse(input);

But how for 2 attributes?

Something like this should work. Untested but you should get the idea:

String countryCode = ....;  // known in advance
QueryParser queryParser = new QueryParser(matchVersion, f, a);
Query cityNameQuery = queryParser.parse(inputWithCityName);
Query countryCodeQuery = queryParser.parse("+countrycode:" + countryCode);

BooleanQuery result = new BooleanQuery();
result.add(new BooleanClause(cityNameQuery, MUST));
result.add(new BooleanClause(countryCodeQuery, MUST));

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