@ES7 and or 关联条件查询JAVA
实现条件( platform=‘xxx’ and (home_path=‘xxx’ or nick_name=‘xxx’ ))文章来源:https://www.toymoban.com/news/detail-717557.html
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文章来源地址https://www.toymoban.com/news/detail-717557.html
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
}
}
}
到了这里,关于ES7 and or 关联条件查询JAVA的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!