https://blog.csdn.net/lpp_dd/article/details/73136059
字段解释:
条件查询:
_source:表示需要展示的字段
聚合查询
field: 表示聚合的字段
size 显示条数
order 排序方法
1、条件查询语句
{
"_source": [
"UUID",
"workOrderData.appNo"
],
"query": {
"bool": {
"must": [
{
"terms": {
"workOrderData.appNo": [
"2017060487300285"
]
}
}
],
"must_not": [],
"should": []
}
},
"size": 100
}
2、聚合查询语句
例子1
{
"query": {
"bool": {
"must": [
{
"terms": {
"workOrderData.center": [
"7110101",
"7110102"
]
}
}
],
"must_not": [],
"should": []
}
},
"size": 0,
"aggs": {
"aggs_block": {
"terms": {
"field": "workOrderData.busiType"
}
}
}
}
例子2
{
"query": {
"bool": {
"must": [
{
"range": {
"relateData.callTime": {
"gte": "2016-07-26 00:00:00"
}
}
},
{
"range": {
"relateData.callTime": {
"lte": "2017-08-02 23:59:59"
}
}
}
]
}
},
"size": 0,
"aggs": {
"1": {
"terms": {
"field": "analysisData.competitorList",
"size": 10,
"order": {
"_count": "desc"
}
}
}
}
}
3、should字段和must字段一起使用
例子1(should和must同级使用)
{
"_source": [
"relateData.orderNum",
"relateDate.businessType",
"relateData.agentGroupName",
"relateData.agentDepartmentName",
"relateData.acceptTime",
"analysisData.is_rule_complain"
],
"query": {
"bool": {
"must": [
{
"range": {
"relateData.callTime": {
"gte": "2017-02-22 00:00:00",
"lte": "2017-08-16 23:59:59"
}
}
}
],
"should": [
{
"term": {
"transData.emotionList.emtionType": {
"value": "2"
}
}
},
{
"term": {
"analysisData.is_rule_complain": {
"value": "是"
}
}
}
]
}
},
"sort": {
"relateData.acceptTime": {
"order": "desc"
}
},
"from": 0,
"size": 10
}
在这里should里面的条件满足一条就可以。相当于or
但是当should与must一起使用时候就失去了should的意义 因为只要满足must的条件就可以
例子2(should为must的上一级)
当我们有and 和 or 并列的查询要求时,(a==0 && b== 0 && (c==0 || d== 0))
我们可以使用如下的elasticsearch查询语句
在should里面包含bool
{
"_source": [
"relateData.orderNum",
"relateDate.businessType",
"relateData.agentGroupName",
"relateData.agentDepartmentName",
"relateData.acceptTime",
"analysisData.is_rule_complain"
],
"query": {
"bool": {
"should": [
{
"bool": {
"must": [
{
"range": {
"relateData.callTime": {
"gte": "2017-02-22 00:00:00",
"lte": "2017-08-16 23:59:59"
}
}
},
{
"term": {
"transData.emotionList.emtionType": {
"value": "2"
}
}
}
]
}
},
{
"bool": {
"must": [
{
"range": {
"relateData.callTime": {
"gte": "2017-02-22 00:00:00",
"lte": "2017-08-16 23:59:59"
}
}
},
{
"term": {
"analysisData.is_rule_complain": {
"value": "是"
}
}
}
]
}
}
]
}
}
}
4、aggs嵌套聚合语句
{
"query": {
"bool": {
"must": [
{
"range": {
"relateData.callTime": {
"gte": "2017-08-30 00:00:00"
}
}
},
{
"range": {
"relateData.callTime": {
"lte": "2017-09-06 23:59:59"
}
}
}
]
}
},
"size": 0,
"aggs": {
"1": {
"terms": {
"field": "relateData.businessType",
"size": 15,
"order": {
"_count": "desc"
}
},
"aggs": {
"2": {
"terms": {
"field": "qualityData.ATTENTION_WORD.keyWord.keyword",
"order": {
"_count": "desc"
}
},
"aggs": {
"3": {
"terms": {
"field": "relateData.customerPhoneNo",
"size": 2147483647,
"min_doc_count": 2
}
}
}
}
}
}
}
}
查询部分结果展示:
"hits": {
"total": 190,
"max_score": 0,
"hits": []
},
"aggregations": {
"1": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"2": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"3": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "13554186343",
"doc_count": 2
},
{
"key": "13554186390",
"doc_count": 2
},
{
"key": "13554186399",
"doc_count": 2
}
]
},
"key": "减肥",
"doc_count": 8
},
{
"3": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "13554186356",
"doc_count": 2
},
{
"key": "13554186389",
"doc_count": 2
}
]
},
"key": "是个",
"doc_count": 7
},
{
"3": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "13554186389",
"doc_count": 2
}
]
},
"key": "绿瘦",
"doc_count": 4
}
]
},
"key": "故障",
"doc_count": 187
}
查询的结果为:首先聚合业务类型,然后聚合业务关注词,最后聚合电话号,聚合结果的意义为在指定业务类型下的指定的业务关注词下的重复来电电话与个数。
5、interval关键字的使用
{
"query": {
"bool": {
"must": [
{
"range": {
"relateData.callTime": {
"gte": "2017-08-30 00:00:00"
}
}
},
{
"range": {
"relateData.callTime": {
"lte": "2017-09-06 23:59:59"
}
}
}
]
}
},
"size": 0,
"aggs": {
"1": {
"terms": {
"field": "qualityData.ATTENTION_WORD.keyWord.keyword",
"size": 10,
"order": {
"_count": "desc"
}
},
"aggs": {
"2": {
"date_histogram": {
"field": "relateData.callTime",
"interval": "1d",
"extended_bounds": {
"min": "2017-08-30 00:00:00",
"max": "2017-09-06 23:59:59"
}
}
}
}
}
}
}
结果展示:
"aggregations": {
"1": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"2": {
"buckets": [
{
"key_as_string": "2017-08-30 00:00:00",
"key": 1504051200000,
"doc_count": 0
},
{
"key_as_string": "2017-08-31 00:00:00",
"key": 1504137600000,
"doc_count": 0
},
{
"key_as_string": "2017-09-01 00:00:00",
"key": 1504224000000,
"doc_count": 0
},
{
"key_as_string": "2017-09-02 00:00:00",
"key": 1504310400000,
"doc_count": 0
},
{
"key_as_string": "2017-09-03 00:00:00",
"key": 1504396800000,
"doc_count": 0
},
{
"key_as_string": "2017-09-04 00:00:00",
"key": 1504483200000,
"doc_count": 0
},
{
"key_as_string": "2017-09-05 00:00:00",
"key": 1504569600000,
"doc_count": 0
},
{
"key_as_string": "2017-09-06 00:00:00",
"key": 1504656000000,
"doc_count": 8
}
]
},
"key": "减肥",
"doc_count": 8
}
interval关键字使得在聚合callTime字段的时候是按照1天的间隔进行聚合,这对于数据统计过长时间范围内能够更加智能的查询,不会导致横坐标过于密。
extended_bounds字段表示强制的将数据显示在指定的时间范围,防止查询的结果超出预期范围。
6、正则表达式
"query": {
"regexp":{
"name.first": "s.*y"
}
}