我在项目中使用了 Elasticsearch 。我将一些值存储到ES。我想从 Elasticsearch 查询数组字段。我必须弄清楚值(value)阵列来了多少次。例如,您可能会看到以下代码,其中,图像和价格将出现两次。
{
"missing_fields_arr": ["images", "price"]
},
{
"missing_fields_arr": ["price"]
},
{
"missing_fields_arr": ["images"]
},
{
"missing_fields_arr": ["images", "price"]
}
我预计输出应该是
"aggregations": {
"missing_fields": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "images, price",
"doc_count": 2
},
{
"key": "price",
"doc_count": 1
},
{
"key": "images",
"doc_count": 1
}
]
}
}
我的代码在这里,
{
"query":{
"bool":{
"must":[
{
"range": {
"@timestamp":{
"gte": "2017-07-20T00:00:00.000Z",
"lte": "2017-07-28T23:59:59.999Z"
}
}
},
{
"term": {
"tracker_name": true
}
}
]
}
},
"from": 0,
"size": 0,
"aggregations" : {
"missing_fields": {"terms": {"field": "missing_fields_arr.raw", "size": 0} }
}
}
最佳答案
您需要使用count api,它比搜索更有效:
当然结合一点正则表达式
例如:
curl -XGET 'localhost:9200/product/item/_count?pretty' -H 'Content-Type:application/json' -d'\
{ "query" : { "term" : { "image|price" } } } '
GET /product/item/_count
{
"query" : {
"term" : { "image|price"}
}
}
https://www.elastic.co/guide/en/elasticsearch/reference/current/search-count.html
https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-valuecount-aggregation.html
关于elasticsearch - Elasticsearch查询数组字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45411014/