Elasticsearch的学习笔记(三)

Elasticsearch的学习笔记(三)

默然
2022-04-27 / 0 评论 / 12 阅读 / 正在检测是否收录...

条件查询

查询指定字段

GET http://localhost:9200/shopping/_search

查询所有数据,只显示字段title和price

请求体:

{
    "query":{
        "match_all":{}
    },
    "_source":["title","price"]
}

返回结果:

{
    "took": 3,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 9,
            "relation": "eq"
        },
        "max_score": 1,
        "hits": [
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "kq3nZIAB6CX3m_v3APT6",
                "_score": 1,
                "_source": {
                    "price": 3999,
                    "title": "小米手机"
                }
            },
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "k63tZIAB6CX3m_v3vfQI",
                "_score": 1,
                "_source": {
                    "price": 3999,
                    "title": "小米手机"
                }
            },
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "l636ZIAB6CX3m_v3q_R5",
                "_score": 1,
                "_source": {
                    "price": 1999,
                    "title": "华为手机"
                }
            },
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "ma37ZIAB6CX3m_v3BvQc",
                "_score": 1,
                "_source": {
                    "price": 1999,
                    "title": "华为手机"
                }
            },
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "n60KZYAB6CX3m_v3q_Tc",
                "_score": 1,
                "_source": {
                    "price": 3999,
                    "title": "小米手机"
                }
            },
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "oK0KZYAB6CX3m_v3rfQs",
                "_score": 1,
                "_source": {
                    "price": 3999,
                    "title": "小米手机"
                }
            },
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "oa0KZYAB6CX3m_v3rvS_",
                "_score": 1,
                "_source": {
                    "price": 3999,
                    "title": "小米手机"
                }
            },
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "oq0KZYAB6CX3m_v3sPQR",
                "_score": 1,
                "_source": {
                    "price": 3999,
                    "title": "小米手机"
                }
            },
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "o60KZYAB6CX3m_v3sfT8",
                "_score": 1,
                "_source": {
                    "price": 3999,
                    "title": "小米手机"
                }
            }
        ]
    }
}

分页查询

GET http://localhost:9200/shopping/_search

请求体:

{
    "query":{
        "match_all":{}
    },
    "from":0,
    "size":2
}

返回结果:

{
    "took": 2,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 9,
            "relation": "eq"
        },
        "max_score": 1,
        "hits": [
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "kq3nZIAB6CX3m_v3APT6",
                "_score": 1,
                "_source": {
                    "title": "小米手机",
                    "category": "小米",
                    "images": "http://www.gulixueyuan.com/xm.jpg",
                    "price": 3999
                }
            },
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "k63tZIAB6CX3m_v3vfQI",
                "_score": 1,
                "_source": {
                    "title": "小米手机",
                    "category": "小米",
                    "images": "http://www.gulixueyuan.com/xm.jpg",
                    "price": 3999
                }
            }
        ]
    }
}

查询排序

GET http://localhost:9200/shopping/_search

按价格的从高到低排序

请求体:

{
    "query":{
        "match_all":{}
    },
    "sort":[
        {
            "price":{
                "order":"desc"
            }
        }
    ]
}

返回结果:

{
    "took": 13,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 9,
            "relation": "eq"
        },
        "max_score": null,
        "hits": [
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "kq3nZIAB6CX3m_v3APT6",
                "_score": null,
                "_source": {
                    "title": "小米手机",
                    "category": "小米",
                    "images": "http://www.gulixueyuan.com/xm.jpg",
                    "price": 3999
                },
                "sort": [
                    3999
                ]
            },
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "k63tZIAB6CX3m_v3vfQI",
                "_score": null,
                "_source": {
                    "title": "小米手机",
                    "category": "小米",
                    "images": "http://www.gulixueyuan.com/xm.jpg",
                    "price": 3999
                },
                "sort": [
                    3999
                ]
            },
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "n60KZYAB6CX3m_v3q_Tc",
                "_score": null,
                "_source": {
                    "title": "小米手机",
                    "category": "小米",
                    "images": "http://www.gulixueyuan.com/xm.jpg",
                    "price": 3999
                },
                "sort": [
                    3999
                ]
            },
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "oK0KZYAB6CX3m_v3rfQs",
                "_score": null,
                "_source": {
                    "title": "小米手机",
                    "category": "小米",
                    "images": "http://www.gulixueyuan.com/xm.jpg",
                    "price": 3999
                },
                "sort": [
                    3999
                ]
            },
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "oa0KZYAB6CX3m_v3rvS_",
                "_score": null,
                "_source": {
                    "title": "小米手机",
                    "category": "小米",
                    "images": "http://www.gulixueyuan.com/xm.jpg",
                    "price": 3999
                },
                "sort": [
                    3999
                ]
            },
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "oq0KZYAB6CX3m_v3sPQR",
                "_score": null,
                "_source": {
                    "title": "小米手机",
                    "category": "小米",
                    "images": "http://www.gulixueyuan.com/xm.jpg",
                    "price": 3999
                },
                "sort": [
                    3999
                ]
            },
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "o60KZYAB6CX3m_v3sfT8",
                "_score": null,
                "_source": {
                    "title": "小米手机",
                    "category": "小米",
                    "images": "http://www.gulixueyuan.com/xm.jpg",
                    "price": 3999
                },
                "sort": [
                    3999
                ]
            },
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "l636ZIAB6CX3m_v3q_R5",
                "_score": null,
                "_source": {
                    "title": "华为手机",
                    "category": "华为",
                    "images": "http://www.gulixueyuan.com/hw.jpg",
                    "price": 1999
                },
                "sort": [
                    1999
                ]
            },
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "ma37ZIAB6CX3m_v3BvQc",
                "_score": null,
                "_source": {
                    "title": "华为手机",
                    "category": "华为",
                    "images": "http://www.gulixueyuan.com/hw.jpg",
                    "price": 1999
                },
                "sort": [
                    1999
                ]
            }
        ]
    }
}

多条件查询

GET http://localhost:9200/shopping/_search

查询小米的牌子,价格为3999元的手机。

请求体:

{
    "query":{
        "bool":{
            "must":[{  //must表示必须满足(相当于and),should表示可以满足(相当于or)
                "match":{
                    "category":"小米"
                }
            },{
                "match":{
                    "price":3999.00
                }
            }]
        }
    }
}

返回结果:

{
    "took": 3,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 7,
            "relation": "eq"
        },
        "max_score": 1.9979823,
        "hits": [
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "kq3nZIAB6CX3m_v3APT6",
                "_score": 1.9979823,
                "_source": {
                    "title": "小米手机",
                    "category": "小米",
                    "images": "http://www.gulixueyuan.com/xm.jpg",
                    "price": 3999
                }
            },
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "k63tZIAB6CX3m_v3vfQI",
                "_score": 1.9979823,
                "_source": {
                    "title": "小米手机",
                    "category": "小米",
                    "images": "http://www.gulixueyuan.com/xm.jpg",
                    "price": 3999
                }
            },
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "n60KZYAB6CX3m_v3q_Tc",
                "_score": 1.9979823,
                "_source": {
                    "title": "小米手机",
                    "category": "小米",
                    "images": "http://www.gulixueyuan.com/xm.jpg",
                    "price": 3999
                }
            },
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "oK0KZYAB6CX3m_v3rfQs",
                "_score": 1.9979823,
                "_source": {
                    "title": "小米手机",
                    "category": "小米",
                    "images": "http://www.gulixueyuan.com/xm.jpg",
                    "price": 3999
                }
            },
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "oa0KZYAB6CX3m_v3rvS_",
                "_score": 1.9979823,
                "_source": {
                    "title": "小米手机",
                    "category": "小米",
                    "images": "http://www.gulixueyuan.com/xm.jpg",
                    "price": 3999
                }
            },
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "oq0KZYAB6CX3m_v3sPQR",
                "_score": 1.9979823,
                "_source": {
                    "title": "小米手机",
                    "category": "小米",
                    "images": "http://www.gulixueyuan.com/xm.jpg",
                    "price": 3999
                }
            },
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "o60KZYAB6CX3m_v3sfT8",
                "_score": 1.9979823,
                "_source": {
                    "title": "小米手机",
                    "category": "小米",
                    "images": "http://www.gulixueyuan.com/xm.jpg",
                    "price": 3999
                }
            }
        ]
    }
}

条件查询列子2

GET http://localhost:9200/shopping/_search

查询小米和华为的牌子,价格大于3000元的手机。

查询范围可以使用以下4个:

  • gt 大于
  • gte 大于或等于
  • lt 小于
  • lte 小于或等于

must表示必须满足(相当于and),should表示可以满足(相当于or)

请求体:

{
    "query":{
        "bool":{
            "should":[
                {
                    "match":{
                        "category":"小米"
                    }
                },
                {
                    "match":{
                        "category":"华为"
                    }
                }
            ],
            "filter":{
                "range":{
                    "price":{
                        "gt":3000
                    }
                }
            }
        }
    }
}

返回结果:

{
    "took": 2,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 7,
            "relation": "eq"
        },
        "max_score": 0.99798226,
        "hits": [
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "kq3nZIAB6CX3m_v3APT6",
                "_score": 0.99798226,
                "_source": {
                    "title": "小米手机",
                    "category": "小米",
                    "images": "http://www.gulixueyuan.com/xm.jpg",
                    "price": 3999
                }
            },
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "k63tZIAB6CX3m_v3vfQI",
                "_score": 0.99798226,
                "_source": {
                    "title": "小米手机",
                    "category": "小米",
                    "images": "http://www.gulixueyuan.com/xm.jpg",
                    "price": 3999
                }
            },
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "n60KZYAB6CX3m_v3q_Tc",
                "_score": 0.99798226,
                "_source": {
                    "title": "小米手机",
                    "category": "小米",
                    "images": "http://www.gulixueyuan.com/xm.jpg",
                    "price": 3999
                }
            },
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "oK0KZYAB6CX3m_v3rfQs",
                "_score": 0.99798226,
                "_source": {
                    "title": "小米手机",
                    "category": "小米",
                    "images": "http://www.gulixueyuan.com/xm.jpg",
                    "price": 3999
                }
            },
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "oa0KZYAB6CX3m_v3rvS_",
                "_score": 0.99798226,
                "_source": {
                    "title": "小米手机",
                    "category": "小米",
                    "images": "http://www.gulixueyuan.com/xm.jpg",
                    "price": 3999
                }
            },
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "oq0KZYAB6CX3m_v3sPQR",
                "_score": 0.99798226,
                "_source": {
                    "title": "小米手机",
                    "category": "小米",
                    "images": "http://www.gulixueyuan.com/xm.jpg",
                    "price": 3999
                }
            },
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "o60KZYAB6CX3m_v3sfT8",
                "_score": 0.99798226,
                "_source": {
                    "title": "小米手机",
                    "category": "小米",
                    "images": "http://www.gulixueyuan.com/xm.jpg",
                    "price": 3999
                }
            }
        ]
    }
}

全文检索

GET http://localhost:9200/shopping/_search

这功能像搜索引擎那样,如品牌输入“小华”,返回结果带回品牌有“小米”和华为的。

请求体:

{
    "query":{
        "match":{
            "category" : "小华"
        }
    }
}

返回结果:

{
    "took": 2,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 9,
            "relation": "eq"
        },
        "max_score": 0.93430924,
        "hits": [
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "l636ZIAB6CX3m_v3q_R5",
                "_score": 0.93430924,
                "_source": {
                    "title": "华为手机",
                    "category": "华为",
                    "images": "http://www.gulixueyuan.com/hw.jpg",
                    "price": 1999
                }
            },
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "ma37ZIAB6CX3m_v3BvQc",
                "_score": 0.93430924,
                "_source": {
                    "title": "华为手机",
                    "category": "华为",
                    "images": "http://www.gulixueyuan.com/hw.jpg",
                    "price": 1999
                }
            },
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "kq3nZIAB6CX3m_v3APT6",
                "_score": 0.49899113,
                "_source": {
                    "title": "小米手机",
                    "category": "小米",
                    "images": "http://www.gulixueyuan.com/xm.jpg",
                    "price": 3999
                }
            },
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "k63tZIAB6CX3m_v3vfQI",
                "_score": 0.49899113,
                "_source": {
                    "title": "小米手机",
                    "category": "小米",
                    "images": "http://www.gulixueyuan.com/xm.jpg",
                    "price": 3999
                }
            },
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "n60KZYAB6CX3m_v3q_Tc",
                "_score": 0.49899113,
                "_source": {
                    "title": "小米手机",
                    "category": "小米",
                    "images": "http://www.gulixueyuan.com/xm.jpg",
                    "price": 3999
                }
            },
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "oK0KZYAB6CX3m_v3rfQs",
                "_score": 0.49899113,
                "_source": {
                    "title": "小米手机",
                    "category": "小米",
                    "images": "http://www.gulixueyuan.com/xm.jpg",
                    "price": 3999
                }
            },
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "oa0KZYAB6CX3m_v3rvS_",
                "_score": 0.49899113,
                "_source": {
                    "title": "小米手机",
                    "category": "小米",
                    "images": "http://www.gulixueyuan.com/xm.jpg",
                    "price": 3999
                }
            },
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "oq0KZYAB6CX3m_v3sPQR",
                "_score": 0.49899113,
                "_source": {
                    "title": "小米手机",
                    "category": "小米",
                    "images": "http://www.gulixueyuan.com/xm.jpg",
                    "price": 3999
                }
            },
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "o60KZYAB6CX3m_v3sfT8",
                "_score": 0.49899113,
                "_source": {
                    "title": "小米手机",
                    "category": "小米",
                    "images": "http://www.gulixueyuan.com/xm.jpg",
                    "price": 3999
                }
            }
        ]
    }
}

完全匹配

GET http://localhost:9200/shopping/_search
{
    "query":{
        "match_phrase":{
            "category" : "为"
        }
    }
}

返回结果:

{
    "took": 1,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 2,
            "relation": "eq"
        },
        "max_score": 0.93430924,
        "hits": [
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "l636ZIAB6CX3m_v3q_R5",
                "_score": 0.93430924,
                "_source": {
                    "title": "华为手机",
                    "category": "华为",
                    "images": "http://www.gulixueyuan.com/hw.jpg",
                    "price": 1999
                }
            },
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "ma37ZIAB6CX3m_v3BvQc",
                "_score": 0.93430924,
                "_source": {
                    "title": "华为手机",
                    "category": "华为",
                    "images": "http://www.gulixueyuan.com/hw.jpg",
                    "price": 1999
                }
            }
        ]
    }
}

高亮查询

GET http://localhost:9200/shopping/_search

请求体:

{
    "query":{
        "match_phrase":{
            "category" : "为"
        }
    },
    "highlight":{
        "fields":{
            "category":{}//<----高亮这字段
        }
    }
}

返回结果:

{
    "took": 19,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 2,
            "relation": "eq"
        },
        "max_score": 0.93430924,
        "hits": [
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "l636ZIAB6CX3m_v3q_R5",
                "_score": 0.93430924,
                "_source": {
                    "title": "华为手机",
                    "category": "华为",
                    "images": "http://www.gulixueyuan.com/hw.jpg",
                    "price": 1999
                },
                "highlight": {
                    "category": [
                        "华<em>为</em>"
                    ]
                }
            },
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "ma37ZIAB6CX3m_v3BvQc",
                "_score": 0.93430924,
                "_source": {
                    "title": "华为手机",
                    "category": "华为",
                    "images": "http://www.gulixueyuan.com/hw.jpg",
                    "price": 1999
                },
                "highlight": {
                    "category": [
                        "华<em>为</em>" //<------高亮一个为字。
                    ]
                }
            }
        ]
    }
}

聚合查询

分组查询

GET http://localhost:9200/shopping/_search

类似与关系型数据库中的 group by,当然还有很多其他的聚合,例如取最大值max、平均值avg等等。

接下来按price字段进行分组:

请求体:

{
    "aggs":{//聚合操作
        "price_group":{//名称,随意起名
            "terms":{//分组
                "field":"price"//分组字段
            }
        }
    }
}

返回结果:

{
    "took": 11,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 9,
            "relation": "eq"
        },
        "max_score": 1,
        "hits": [
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "kq3nZIAB6CX3m_v3APT6",
                "_score": 1,
                "_source": {
                    "title": "小米手机",
                    "category": "小米",
                    "images": "http://www.gulixueyuan.com/xm.jpg",
                    "price": 3999
                }
            },
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "k63tZIAB6CX3m_v3vfQI",
                "_score": 1,
                "_source": {
                    "title": "小米手机",
                    "category": "小米",
                    "images": "http://www.gulixueyuan.com/xm.jpg",
                    "price": 3999
                }
            },
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "l636ZIAB6CX3m_v3q_R5",
                "_score": 1,
                "_source": {
                    "title": "华为手机",
                    "category": "华为",
                    "images": "http://www.gulixueyuan.com/hw.jpg",
                    "price": 1999
                }
            },
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "ma37ZIAB6CX3m_v3BvQc",
                "_score": 1,
                "_source": {
                    "title": "华为手机",
                    "category": "华为",
                    "images": "http://www.gulixueyuan.com/hw.jpg",
                    "price": 1999
                }
            },
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "n60KZYAB6CX3m_v3q_Tc",
                "_score": 1,
                "_source": {
                    "title": "小米手机",
                    "category": "小米",
                    "images": "http://www.gulixueyuan.com/xm.jpg",
                    "price": 3999
                }
            },
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "oK0KZYAB6CX3m_v3rfQs",
                "_score": 1,
                "_source": {
                    "title": "小米手机",
                    "category": "小米",
                    "images": "http://www.gulixueyuan.com/xm.jpg",
                    "price": 3999
                }
            },
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "oa0KZYAB6CX3m_v3rvS_",
                "_score": 1,
                "_source": {
                    "title": "小米手机",
                    "category": "小米",
                    "images": "http://www.gulixueyuan.com/xm.jpg",
                    "price": 3999
                }
            },
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "oq0KZYAB6CX3m_v3sPQR",
                "_score": 1,
                "_source": {
                    "title": "小米手机",
                    "category": "小米",
                    "images": "http://www.gulixueyuan.com/xm.jpg",
                    "price": 3999
                }
            },
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "o60KZYAB6CX3m_v3sfT8",
                "_score": 1,
                "_source": {
                    "title": "小米手机",
                    "category": "小米",
                    "images": "http://www.gulixueyuan.com/xm.jpg",
                    "price": 3999
                }
            }
        ]
    },
    "aggregations": {
        "price_group": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
                {
                    "key": 3999,
                    "doc_count": 7
                },
                {
                    "key": 1999,
                    "doc_count": 2
                }
            ]
        }
    }
}

上面返回结果会附带原始数据的。若不想要不附带原始数据的结果,可以使用下面的方式:
请求体:

{
    "aggs":{
        "price_group":{
            "terms":{
                "field":"price"
            }
        }
    },
    "size":0
}

返回结果:

{
    "took": 4,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 9,
            "relation": "eq"
        },
        "max_score": null,
        "hits": []
    },
    "aggregations": {
        "price_group": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
                {
                    "key": 3999,
                    "doc_count": 7
                },
                {
                    "key": 1999,
                    "doc_count": 2
                }
            ]
        }
    }
}

平均值

GET

所有手机价格求平均值。

请求体:

{
    "aggs":{
        "price_avg":{//名称,随意起名
            "avg":{//求平均
                "field":"price"
            }
        }
    },
    "size":0
}

返回结果:

{
    "took": 2,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 9,
            "relation": "eq"
        },
        "max_score": null,
        "hits": []
    },
    "aggregations": {
        "price_avg": {
            "value": 3554.5555555555557
        }
    }
}
0

评论 (0)

取消