2017-07-31 14 views
0

私のプロジェクトでは弾性検索を使用しました。 ESにいくつかの値を保存しました。私は弾性検索から配列フィールドを照会したいと思います。私は価値の配列が何回来たかを知る必要があります。例えば、あなたは以下のコードを見ることができます。その中で、画像と価格は2倍になります。弾性検索クエリ配列フィールド

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

答えて

1

あなたはそれがはるかに効率的な検索よりもだカウント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