簡體   English   中英

Elasticsearch查詢數組字段

[英]Elasticsearch query array field

我在項目中使用了彈性搜索。 我將一些值存儲到ES。 我想從彈性搜索查詢數組字段。 我必須弄清楚價值陣列來了多少次。 例如,您可能會看到以下代碼,其中,圖像和價格將出現兩次。

{
 "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,它比搜索要有效得多:當然要結合一點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/zh-CN/elasticsearch/reference/current/search-count.html https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics -valuecount-aggregation.html

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM