简体   繁体   中英

Elasticsearch query array field

I used elastic search in my project. I stored some values to ES. I want to query the array field from elastic search. I have to get how many time the array of value came. For example, You could see the below code, In that, image and price are coming two times.

{
 "missing_fields_arr": ["images", "price"]
},
{
 "missing_fields_arr": ["price"]
},
{
 "missing_fields_arr": ["images"]
},
{
 "missing_fields_arr": ["images", "price"]
} 

and I expected output should be

 "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
            }
        ]
    }
}

My code is here,

{
  "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} }
 }
 }

You need to use the count api it's much more efficient than the search: of course combined with a little bit of regex ex :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM