@ES7 and or 关联条件查询JAVA
实现条件( platform=‘xxx’ and (home_path=‘xxx’ or nick_name=‘xxx’ ))
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
SearchRequest searchRequest = new SearchRequest(SampleEnum.SAMPLE_PAGE.getValue());
//查询字段
String[] array = new String[]{"person_id", "home_path", "platform", "nick_name", "source_type",
"account_id"};
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder()
.fetchSource(array, null);
//设置查询条件
MatchQueryBuilder queryBuilder2 = QueryBuilders.matchQuery("platform", platform);
boolQueryBuilder.must(queryBuilder2);
if (StringUtils.isNotBlank(searchValue)) {
BoolQueryBuilder wqBuilder1 = QueryBuilders.boolQuery()
.should(QueryBuilders.wildcardQuery("home_path.keyword", "*" + searchValue + "*"))
.should(QueryBuilders.wildcardQuery("nick_name.keyword", "*" + searchValue + "*"));
boolQueryBuilder.must(wqBuilder1);
boolQueryBuilder.minimumShouldMatch();
}
sourceBuilder.query(boolQueryBuilder);
searchRequest.source(sourceBuilder);
//每次查询1千,直到查询全部数据再返回
sourceBuilder.size(10000);
//设置允许获取超过1万条数据,需额外配置ES查询总命中数,不然此设置无效,ES默认只能查1万条
sourceBuilder.trackTotalHits(true);
try {
SearchResponse response = docService.search(searchRequest);
System.out.println(searchRequest.source().toString());
SearchHits hits = response.getHits();
long value = hits.getTotalHits().value;
SearchHit[] searchHits = hits.getHits();
JSONArray jsonArray = new JSONArray();
for (SearchHit hit : hits) {
String sourceAsString = hit.getSourceAsString();
JSONObject jsonObject = JSON.parseObject(sourceAsString);
jsonArray.add(jsonObject);
}
Map<String, Object> map = new HashMap<>();
map.put("total", value);
map.put("rows", jsonArray);
return AjaxResult.success(map);
} catch (IOException e) {
log.error("查询文档失败,异常信息:{}", e);
return AjaxResult.error("查询文档失败");
}
kibana
GET t_sample_page_data_dev/_search
{
"query": {
"bool": {
"must": [
{"match": {
"platform": "facebook"
}
}],
"should": [{
"wildcard": {
"nick_name.keyword": {
"value": "*100000461928817*"
}
}
},{
"wildcard": {
"home_path.keyword": {
"value": "*100000461928817*"
}
}
}
],"minimum_should_match": 1
}
}
}