elasticsearch 带过滤和聚合的Elasricsearch查询
我有一个包含简单汽车广告数据的Elasticsearch索引。我制作了一张地图:
elasticsearch 带过滤和聚合的Elasricsearch查询,
elasticsearch,
elasticsearch,我有一个包含简单汽车广告数据的Elasticsearch索引。我制作了一张地图: "mappings": { "properties": { "brand": {"type": "keyword"}, "model": {"type": "keyword"}, "year of production": {
"mappings": {
"properties": {
"brand": {"type": "keyword"},
"model": {"type": "keyword"},
"year of production": {"type": "integer"},
"mileage": {"type": "integer"},
"color": {"type": "keyword"},
"engine capacity": {"type": "double"},
"horse power": {"type": "integer"},
"type of fuel": {"type": "keyword"},
"condition": {"type": "keyword"},
"accident history": {"type": "boolean"},
"price": {"type": "integer"}
}
}
我想创建两个查询:
GET /gielda/_search?pretty
{
"query": {
"match": {
"stan": "Used"
}
},
"aggs": {
"production_ranges": {
"range": {
"field": "year of production",
"ranges": [
{
"from": 2010,
"to": 2012
}
]
},
"aggs": {
"avg_mileage": {
"avg": {
"field": "mileage"
}
}
}
}
}
}
我发现这个查询无法正常工作。我在弹性文档和堆栈中寻找答案,但我仍然不知道如何将聚合与筛选器或范围混合使用。您需要使用、、和聚合的组合 添加带有索引数据、搜索查询和搜索结果的工作示例 索引数据:
{
"mileage": 10,
"year of production": 2011
}
{
"mileage": 30,
"year of production": 2012
}
{
"mileage": 20,
"year of production": 2013
}
{
"mileage": 200001,
"year of production": 2005,
"model": "ABC"
}
{
"mileage": 400000,
"year of production": 2006,
"model": "DEF"
}
{
"mileage": 400000,
"year of production": 2008,
"model": "GHI"
}
{
"size": 0,
"aggs": {
"production_ranges": {
"range": {
"field": "year of production",
"ranges": [
{
"from": 2010,
"to": 2013
}
]
},
"aggs": {
"avg_mileage": {
"avg": {
"field": "mileage"
}
}
}
},
"Popular_car_model": {
"filter": {
"bool": {
"must": [
{
"range": {
"year of production": {
"gte": 2005,
"lte": 2007
}
}
},
{
"range": {
"mileage": {
"gte": 200000
}
}
}
]
}
},
"aggs": {
"top_sales_hits": {
"top_hits": {
"sort": [
{
"mileage": {
"order": "desc"
}
}
],
"_source": {
"includes": [
"model",
"mileage",
"year of production"
]
},
"size": 1
}
}
}
}
}
}
"aggregations": {
"Popular_car_model": {
"doc_count": 2,
"top_sales_hits": {
"hits": {
"total": {
"value": 2,
"relation": "eq"
},
"max_score": null,
"hits": [
{
"_index": "67650682",
"_type": "_doc",
"_id": "5",
"_score": null,
"_source": {
"year of production": 2006,
"model": "DEF",
"mileage": 400000
},
"sort": [
400000
]
}
]
}
}
},
"production_ranges": {
"buckets": [
{
"key": "2010.0-2013.0",
"from": 2010.0,
"to": 2013.0,
"doc_count": 2,
"avg_mileage": {
"value": 20.0
}
}
]
}
}
搜索查询:
{
"mileage": 10,
"year of production": 2011
}
{
"mileage": 30,
"year of production": 2012
}
{
"mileage": 20,
"year of production": 2013
}
{
"mileage": 200001,
"year of production": 2005,
"model": "ABC"
}
{
"mileage": 400000,
"year of production": 2006,
"model": "DEF"
}
{
"mileage": 400000,
"year of production": 2008,
"model": "GHI"
}
{
"size": 0,
"aggs": {
"production_ranges": {
"range": {
"field": "year of production",
"ranges": [
{
"from": 2010,
"to": 2013
}
]
},
"aggs": {
"avg_mileage": {
"avg": {
"field": "mileage"
}
}
}
},
"Popular_car_model": {
"filter": {
"bool": {
"must": [
{
"range": {
"year of production": {
"gte": 2005,
"lte": 2007
}
}
},
{
"range": {
"mileage": {
"gte": 200000
}
}
}
]
}
},
"aggs": {
"top_sales_hits": {
"top_hits": {
"sort": [
{
"mileage": {
"order": "desc"
}
}
],
"_source": {
"includes": [
"model",
"mileage",
"year of production"
]
},
"size": 1
}
}
}
}
}
}
"aggregations": {
"Popular_car_model": {
"doc_count": 2,
"top_sales_hits": {
"hits": {
"total": {
"value": 2,
"relation": "eq"
},
"max_score": null,
"hits": [
{
"_index": "67650682",
"_type": "_doc",
"_id": "5",
"_score": null,
"_source": {
"year of production": 2006,
"model": "DEF",
"mileage": 400000
},
"sort": [
400000
]
}
]
}
}
},
"production_ranges": {
"buckets": [
{
"key": "2010.0-2013.0",
"from": 2010.0,
"to": 2013.0,
"doc_count": 2,
"avg_mileage": {
"value": 20.0
}
}
]
}
}
搜索结果:
{
"mileage": 10,
"year of production": 2011
}
{
"mileage": 30,
"year of production": 2012
}
{
"mileage": 20,
"year of production": 2013
}
{
"mileage": 200001,
"year of production": 2005,
"model": "ABC"
}
{
"mileage": 400000,
"year of production": 2006,
"model": "DEF"
}
{
"mileage": 400000,
"year of production": 2008,
"model": "GHI"
}
{
"size": 0,
"aggs": {
"production_ranges": {
"range": {
"field": "year of production",
"ranges": [
{
"from": 2010,
"to": 2013
}
]
},
"aggs": {
"avg_mileage": {
"avg": {
"field": "mileage"
}
}
}
},
"Popular_car_model": {
"filter": {
"bool": {
"must": [
{
"range": {
"year of production": {
"gte": 2005,
"lte": 2007
}
}
},
{
"range": {
"mileage": {
"gte": 200000
}
}
}
]
}
},
"aggs": {
"top_sales_hits": {
"top_hits": {
"sort": [
{
"mileage": {
"order": "desc"
}
}
],
"_source": {
"includes": [
"model",
"mileage",
"year of production"
]
},
"size": 1
}
}
}
}
}
}
"aggregations": {
"Popular_car_model": {
"doc_count": 2,
"top_sales_hits": {
"hits": {
"total": {
"value": 2,
"relation": "eq"
},
"max_score": null,
"hits": [
{
"_index": "67650682",
"_type": "_doc",
"_id": "5",
"_score": null,
"_source": {
"year of production": 2006,
"model": "DEF",
"mileage": 400000
},
"sort": [
400000
]
}
]
}
}
},
"production_ranges": {
"buckets": [
{
"key": "2010.0-2013.0",
"from": 2010.0,
"to": 2013.0,
"doc_count": 2,
"avg_mileage": {
"value": 20.0
}
}
]
}
}
您需要使用、、和聚合的组合 添加带有索引数据、搜索查询和搜索结果的工作示例 索引数据:
{
"mileage": 10,
"year of production": 2011
}
{
"mileage": 30,
"year of production": 2012
}
{
"mileage": 20,
"year of production": 2013
}
{
"mileage": 200001,
"year of production": 2005,
"model": "ABC"
}
{
"mileage": 400000,
"year of production": 2006,
"model": "DEF"
}
{
"mileage": 400000,
"year of production": 2008,
"model": "GHI"
}
{
"size": 0,
"aggs": {
"production_ranges": {
"range": {
"field": "year of production",
"ranges": [
{
"from": 2010,
"to": 2013
}
]
},
"aggs": {
"avg_mileage": {
"avg": {
"field": "mileage"
}
}
}
},
"Popular_car_model": {
"filter": {
"bool": {
"must": [
{
"range": {
"year of production": {
"gte": 2005,
"lte": 2007
}
}
},
{
"range": {
"mileage": {
"gte": 200000
}
}
}
]
}
},
"aggs": {
"top_sales_hits": {
"top_hits": {
"sort": [
{
"mileage": {
"order": "desc"
}
}
],
"_source": {
"includes": [
"model",
"mileage",
"year of production"
]
},
"size": 1
}
}
}
}
}
}
"aggregations": {
"Popular_car_model": {
"doc_count": 2,
"top_sales_hits": {
"hits": {
"total": {
"value": 2,
"relation": "eq"
},
"max_score": null,
"hits": [
{
"_index": "67650682",
"_type": "_doc",
"_id": "5",
"_score": null,
"_source": {
"year of production": 2006,
"model": "DEF",
"mileage": 400000
},
"sort": [
400000
]
}
]
}
}
},
"production_ranges": {
"buckets": [
{
"key": "2010.0-2013.0",
"from": 2010.0,
"to": 2013.0,
"doc_count": 2,
"avg_mileage": {
"value": 20.0
}
}
]
}
}
搜索查询:
{
"mileage": 10,
"year of production": 2011
}
{
"mileage": 30,
"year of production": 2012
}
{
"mileage": 20,
"year of production": 2013
}
{
"mileage": 200001,
"year of production": 2005,
"model": "ABC"
}
{
"mileage": 400000,
"year of production": 2006,
"model": "DEF"
}
{
"mileage": 400000,
"year of production": 2008,
"model": "GHI"
}
{
"size": 0,
"aggs": {
"production_ranges": {
"range": {
"field": "year of production",
"ranges": [
{
"from": 2010,
"to": 2013
}
]
},
"aggs": {
"avg_mileage": {
"avg": {
"field": "mileage"
}
}
}
},
"Popular_car_model": {
"filter": {
"bool": {
"must": [
{
"range": {
"year of production": {
"gte": 2005,
"lte": 2007
}
}
},
{
"range": {
"mileage": {
"gte": 200000
}
}
}
]
}
},
"aggs": {
"top_sales_hits": {
"top_hits": {
"sort": [
{
"mileage": {
"order": "desc"
}
}
],
"_source": {
"includes": [
"model",
"mileage",
"year of production"
]
},
"size": 1
}
}
}
}
}
}
"aggregations": {
"Popular_car_model": {
"doc_count": 2,
"top_sales_hits": {
"hits": {
"total": {
"value": 2,
"relation": "eq"
},
"max_score": null,
"hits": [
{
"_index": "67650682",
"_type": "_doc",
"_id": "5",
"_score": null,
"_source": {
"year of production": 2006,
"model": "DEF",
"mileage": 400000
},
"sort": [
400000
]
}
]
}
}
},
"production_ranges": {
"buckets": [
{
"key": "2010.0-2013.0",
"from": 2010.0,
"to": 2013.0,
"doc_count": 2,
"avg_mileage": {
"value": 20.0
}
}
]
}
}
搜索结果:
{
"mileage": 10,
"year of production": 2011
}
{
"mileage": 30,
"year of production": 2012
}
{
"mileage": 20,
"year of production": 2013
}
{
"mileage": 200001,
"year of production": 2005,
"model": "ABC"
}
{
"mileage": 400000,
"year of production": 2006,
"model": "DEF"
}
{
"mileage": 400000,
"year of production": 2008,
"model": "GHI"
}
{
"size": 0,
"aggs": {
"production_ranges": {
"range": {
"field": "year of production",
"ranges": [
{
"from": 2010,
"to": 2013
}
]
},
"aggs": {
"avg_mileage": {
"avg": {
"field": "mileage"
}
}
}
},
"Popular_car_model": {
"filter": {
"bool": {
"must": [
{
"range": {
"year of production": {
"gte": 2005,
"lte": 2007
}
}
},
{
"range": {
"mileage": {
"gte": 200000
}
}
}
]
}
},
"aggs": {
"top_sales_hits": {
"top_hits": {
"sort": [
{
"mileage": {
"order": "desc"
}
}
],
"_source": {
"includes": [
"model",
"mileage",
"year of production"
]
},
"size": 1
}
}
}
}
}
}
"aggregations": {
"Popular_car_model": {
"doc_count": 2,
"top_sales_hits": {
"hits": {
"total": {
"value": 2,
"relation": "eq"
},
"max_score": null,
"hits": [
{
"_index": "67650682",
"_type": "_doc",
"_id": "5",
"_score": null,
"_source": {
"year of production": 2006,
"model": "DEF",
"mileage": 400000
},
"sort": [
400000
]
}
]
}
}
},
"production_ranges": {
"buckets": [
{
"key": "2010.0-2013.0",
"from": 2010.0,
"to": 2013.0,
"doc_count": 2,
"avg_mileage": {
"value": 20.0
}
}
]
}
}
这是正确的方法!我已经对答案投了赞成票!我来这里也是为了回答这个问题,我看到了你的答案,这和我想说的是一样的;)@MikaelAmidi哦,我的错:/我还以为你是最早的海报呢。不管怎样,谢谢你的支持:-)@ESCoder我检查了你的答案,似乎它工作正常,但我有一个关于第二个问题的问题。第二个查询返回带有_id、model和miliety字段的精确记录,但我不确定它是否返回满足查询条件的最流行的模型。我认为查询应该过滤2005年到2007年之间的汽车,然后过滤里程超过200k的汽车,统计独特的车型,然后返回最常销售的车型名称。也许我用了错误的句子问这个问题。第二个查询过滤了2005年到2007年间的汽车,以及里程数超过200000的汽车。根据过滤后的数据,它会找到里程数最高的汽车。我想这就是你期望得到的?这是正确的方法!我已经对答案投了赞成票!我来这里也是为了回答这个问题,我看到了你的答案,这和我想说的是一样的;)@MikaelAmidi哦,我的错:/我还以为你是最早的海报呢。不管怎样,谢谢你的支持:-)@ESCoder我检查了你的答案,似乎它工作正常,但我有一个关于第二个问题的问题。第二个查询返回带有_id、model和miliety字段的精确记录,但我不确定它是否返回满足查询条件的最流行的模型。我认为查询应该过滤2005年到2007年之间的汽车,然后过滤里程超过200k的汽车,统计独特的车型,然后返回最常销售的车型名称。也许我用了错误的句子问这个问题。第二个查询过滤了2005年到2007年间的汽车,以及里程数超过200000的汽车。根据过滤后的数据,它会找到里程数最高的汽车。我想这就是你期望得到的?