1.BoolQuery( ) 用于组合多个叶子或复合查询子句的默认查询
must 相当于 与 & =
must not 相当于 非 ~ !=
should 相当于 或 | or
filter 过滤
boolQuery().must(termQuery("content", "query1"))
.must(termQuery("content", "query2"))
.mustNot(termQuery("content", "query3"))
.should(termQuery("content", "query4"))
.filter(termQuery("content", "query5"));
2.精确查询
以下字段名用 ${fieldName} 代替,具体值用 ${fieldValue} 代替
数字查询
//单个
QueryBuilder qb1 = QueryBuilders.termQuery("${fieldName}", "${fieldValue}");
//批量
QueryBuilder qb1 = QueryBuilders.termsQuery("${fieldName}", "${fieldValues}");
字符串查询
//单个
QueryBuilder qb1 = QueryBuilders.termQuery("${fieldName}.keyword", "${fieldValue}");
//批量
QueryBuilder qb1 = QueryBuilders.termsQuery("${fieldName}.keyword", "${fieldValues}");
3.模糊查询
数字
数字查询只能是精确查询
字符串
QueryBuilder qb1 = QueryBuilders.moreLikeThisQuery(
new String[]{"${fieldName}"},
new String[]{"${fieldValue}"}, null);
4.范围查询
数字
//闭区间查询
QueryBuilder qb1 = QueryBuilders.rangeQuery("${fieldName}").from(${fieldValue1}).to(${fieldValue2});
//开区间查询
QueryBuilder qb1 = QueryBuilders.rangeQuery("${fieldName}").from(${fieldValue1}, false).to(${fieldValue2}, false);
//大于
QueryBuilder qb1 = QueryBuilders.rangeQuery("${fieldName}").gt(${fieldValue});
//大于等于
QueryBuilder qb1 = QueryBuilders.rangeQuery("${fieldName}").gte(${fieldValue});
//小于
QueryBuilder qb1 = QueryBuilders.rangeQuery("${fieldName}").lt(${fieldValue});
//小于等于
QueryBuilder qb1 = QueryBuilders.rangeQuery("${fieldName}").lte(${fieldValue});
5.多条件查询
QueryBuilder qb1 = QueryBuilders.moreLikeThisQuery(new String[]{"${fieldName1}"}, new String[]{"${fieldValue1}"}, null);
QueryBuilder qb2 = QueryBuilders.rangeQuery("${fieldName2}").gt("${fieldValue2}");
QueryBuilder qb3 = QueryBuilders.boolQuery().must(qb1).must(qb2);
网友评论