有两种方式可以实现,
一是:Lucene搜索API中提供了一个布尔查询器(BooleanQuery),它可以包含多个查询器,每个查询器Occur枚举控制是“and” 还是“or”
BooleanQuery.Builder booleanQueryBuilder = new BooleanQuery.Builder(); Query query1 = NumericRangeQuery.newIntRange("cid", 1, 2, true, true); Query query2 = NumericRangeQuery.newLongRange("price", 1000L, 2000L, true, true); booleanQueryBuilder.add(query1, Occur.SHOULD);//Occur.MUST(和查询),Occur.SHOULD(或查询),Occur.MUST_NOT(异查询) booleanQueryBuilder.add(query2, Occur.MUST); Query query = booleanQueryBuilder.build(); |
二是:利用QueryParser解析一个多条件语法搜索查询器
QueryParser queryParser = new QueryParser("*", analyzer); Query query = queryParser.parse("name:'手机' AND brand:'小米'"); |