ElasticSearch使用(嵌套查询、嵌套高亮)
嵌套查询
当ES的一条文档中的数据是一个数组,数组中有多个对象时,普通的查询不再适用,比如:
{
"_index" : "t_trace_index-2019.08.11.15",
"_type" : "_doc",
"_id" : "c8fc4f6f0bcd^1565435961237^3980",
"_score" : 11.434727,
"_routing" : "c8fc4f6f0bcd^1565435961237^3980",
"_source" : {
"type" : "pdoc",
"calls" : [
{
"bizParams" : """{"input":{"CONTROL_IN_PARAM":{"DATA_STATE":["DATA_ALL"]},"OPER_ID":"10034571"}}""",
"callee" : "test-busi-ocrm-yyt",
"caller" : "test-busi-cust-yyt",
"rpc" : "CSF_SEC_OPERM_OPIDQQUERY_002",
"traceId" : "c8fc4f6f0bcd^1565435961237^3980",
"ts" : 1565507717426
},
{
"traceId" : "c8fc4f6f0bcd^1565435961237^3980",
"caller" : "test-busi-cust-yyt",
"rpc" : "ocrm_ICbossCSV_idCheck",
"callee" : "test-busi-ocrm-yyt",
"ts" : 1565507717915,
"bizParams" : """{"input":{"CONTROL_IN_PARAM":{"DATA_STATE":["DATA_ALL"]},"RSP_JSON_STR":"{
\"IdCheck\":{\"Seq\":\"10020190811151517510040\",\"CustomerName\":\"786382D36B599DF8D3C312272A75AD81\",\"IDCardType\":\"04\",\"IDCardNum\":\"99425ED800E46883C131C544E9F8BAD9\",\"ChannelId\":\"210000000100001\",\"MsOpcode\":\"C8000\"}}"}}"""
},
{
"traceId" : "c8fc4f6f0bcd^1565435961237^3980",
"caller" : "test-busi-rule-yyt",
"rpc" : "cust_IOneCardFiveUserCSV_idCheck",
"callee" : "test-busi-cust-yyt",
"ts" : 1565507717985,
"bizParams" : """{"input":{"CERT_CODE":"523453","CODE_TYPE":"CM_CERT_TYPE_CHECKCODE","CUST_NAME":"对方八年","CERT_TYPE":"3","CONTROL_IN_PARAM":{"DATA_STATE":["DATA_ALL"]}}}"""
}
],
"id" : "c8fc4f6f0bcd^1565435961237^3980",
"timestamp" : 1565507717426,
"trace_join" : {
"name" : "pdoc"
}
}
}
这时候就需要使用嵌套查询(nested)
bool 查询
查询字句 | 说明 | 类型 |
---|---|---|
must | 文档必须匹配must查询条件(AND) | 数组 |
should | 匹配should的一个或多个(OR) | 数组 |
must_not | 文档不能匹配该查询条件 | 数组 |
filter | 过滤,文档需要匹配过滤条件,功能和must一样,不过filter不参与评分 | 数组 |
must、should关系
1、只有must
must中条件必须满足
2、只有should
没有must,只有should,那么should中必须至少匹配一个才行
3、must和should同时存在
如果must中条件匹配,那么should可以不匹配,所以如果查询语句中must、should同时存在,又不设置should最小满足条数,那么should条件有时是不会生效的
项目使用场景:
快速定位功能,需要输入关键字和各种过滤条件,关键字有三种类型,查询时,每个关键字都需要搜索一遍,考虑的是关键字使用should,只要有一种关键字类型满足即可,过滤条件使用must,需要全都满足,在没有指定should最小满足条数时,发现关键字可以输入任意值,只要过滤条件满足就可以查询出结果,关键字没有起作用,在增加should最小满足条数为1之后,设置should至少满足一条,查询结果正确
4、怎样设置should必须满足、最小满足条数
可以设置should中必须匹配多少个,参数"minimum_should_match": "1"
,也可以设置匹配百分比"minimum_should_match":75%
Java代码
BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
boolQueryBuilder.should(QueryBuilders.nestedQuery("calls", QueryBuilders.boolQuery().must(QueryBuilders.matchPhraseQuery("calls.bizParams", keyword)), ScoreMode.Avg));
boolQueryBuilder.should(QueryBuilders.nestedQuery("calls", QueryBuilders.boolQuery().must(QueryBuilders.termQuery("calls.rpc", keyword)), ScoreMode.Avg));
boolQueryBuilder.should(QueryBuilders.nestedQuery("calls", QueryBuilders.boolQuery().must(QueryBuilders.termQuery("calls.traceId", keyword)), ScoreMode.Avg));
// 设置should必须匹配多少条
boolQueryBuilder.minimumShouldMatch(1);
DSL查询语句
"should": [
{
"nested": {
"query": {
"bool": {
"must": [
{
"match_phrase": {
"calls.bizParams": {
"query": "input",
"slop": 0,
"zero_terms_query": "NONE",
"boost": 1
}
}
}
],
"adjust_pure_negative": true,
"boost": 1
}
},
"path": "calls",
"ignore_unmapped": false,
"score_mode": "avg",
"boost": 1,
}
},
{
"nested": {
"query": {
"bool": {
"must": [
{
"term": {
"calls.rpc": {
"value": "input",
"boost": 1
}
}
}
],
"adjust_pure_negative": true,
"boost": 1
}
},
"path": "calls",
"ignore_unmapped": false,
"score_mode": "avg",
"boost": 1
}
},
{
"nested": {
"query": {
"bool": {
"must": [
{
"term": {
"calls.traceId": {
"value": "input",
"boost": 1
}
}
}
],
"adjust_pure_negative": true,
"boost": 1
}
},
"path": "calls",
"ignore_unmapped": false,
"score_mode": "avg",
"boost": 1
}
}
],
"adjust_pure_negative": true,
"minimum_should_match": "1",
"boost": 1
设置should中三个条件至少有一个匹配才行
查询高亮
1. 普通查询高亮
java代码