赞
踩
1.matchQuery(String name,Object text)
matchQuery("filedname","value")匹配单个字段,匹配字段名为filedname,值为value的文档
//单个匹配,搜索name为jack的文档
QueryBuilder queryBuilder = QueryBuilders.matchQuery("name", "jack");
2.multiMatchQuery(Object text, String... fieldNames)
多个字段匹配某一个值
//搜索name中或interest中包含有music的文档(必须与music一致)
QueryBuilder queryBuilder = QueryBuilders.multiMatchQuery("music","name", "interest");
3.
wildcardQuery()模糊查询
模糊查询,?匹配单个字符,*匹配多个字符
//搜索名字中含有jack文档(name中只要包含jack即可)
WildcardQueryBuilder queryBuilder = QueryBuilders.wildcardQuery("name","*jack*");
4.
BoolQueryBuilder进行复合查询
4.1使用must
//模糊查询并搜索名字中含有jack的文档
WildcardQueryBuilder queryBuilder1 = QueryBuilders.wildcardQuery( "name", "*jack*");
//模糊查询并搜索interest中含有read的文档
WildcardQueryBuilder queryBuilder2 = QueryBuilders.wildcardQuery( "interest", "*read*");
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
//name中必须含有jack,interest中必须含有read,相当于and
boolQueryBuilder.must(queryBuilder1);
boolQueryBuilder.must(queryBuilder2);
输出结果:name中必须含有jack,interest中必须含有read
4.2 使用 should
WildcardQueryBuilder queryBuilder1 = QueryBuilders.wildcardQuery("name", "*jack*");//搜索名字中含有jack的文档
WildcardQueryBuilder queryBuilder2 = QueryBuilders.wildcardQuery("interest", "*read*");//搜索interest中含有read的文档
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
//name中含有jack或者interest含有read,相当于or
boolQueryBuilder.should(queryBuilder1);
boolQueryBuilder.should(queryBuilder2);
输出结果:name中含有jack或者interest中含有read的都将被匹配
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。