2016-03-29 11 views
1

私はElastic Searchを初めて使いました。クエリで複数のフィルタ(特に "最大")を使用するといくつかの問題に直面します正確な範囲で最大の日付をクエリし、結果の選択フィールドを取得する

現在、私はElastic Searchによってインデックス付けされた巨大なデータベースに取り組んでいます。 多くのドキュメントがあり、各ドキュメントは特定の1つのサーバーに関するすべての情報です。

ソフトはこれらのサーバー上で散発的に実行され、更新された情報で新しい文書を作成します。

ので、情報は次のように格納されます。

Id : item1 
ITDiscovery_Date : 29/03/2016 
Information1 : ... 
Information2 : ... 

Id : item1 
ITDiscovery_Date : 12/03/2016 
Information1 : ... 
Information2 : ... 

Id : item2 
ITDiscovery_Date : 16/02/2016 
Information1 : ... 
Information2 : ... 

Id : item2 
ITDiscovery_Date : 27/01/2016 
Information1 : ... 
Information2 : ... 

そしてそう

に私の問題は、以下の通りである。

私は1台の特定のサーバーに関する最新の情報を取得しようとしています。このために、最初にサーバーの名前(たとえばitem456)をフィルターに掛け、特定の日付範囲(たとえば、01/01/01から今日まで)にこのサーバーのすべての文書を取得してから、最新の情報を取得し、選択されたフィールド(例:Information15、Information28、およびInformation68)を取得するために

私はいくつかの異なるリクエストを試しましたが、これを動作させることはできません:

{ 
    "took" : 34, 
    "timed_out" : false, 
    "_shards" : { 
    "total" : 982, 
    "successful" : 982, 
    "failed" : 0 
    }, 
    "hits" : { 
    "total" : 33, 
    "max_score" : 15.364556, 
    "hits" : [ { 
     "_index" : "itdiscovery_2016.03.02", 
     "_type" : "default", 
     "_id" : "item456", 
     "_score" : 15.364556, 
     "fields" : { 
     "Information15" : [ "XXX" ], 
     "Information28" : [ "XXX" ], 
     "Information68" : [ "XXX" ] 
     } 
    }, { 
     "_index" : "itdiscovery_2016.03.23", 
     "_type" : "default", 
     "_id" : "item456", 
     "_score" : 15.359651, 
     "fields" : { 
     "Information15" : [ "XXX" ], 
     "Information28" : [ "XXX" ], 
     "Information68" : [ "XXX" ] 
     } 
    } ] 
    }, { 
    ... 
    }, 
    "aggregations" : { 
    "date_range" : { 
     "doc_count" : 33, 
     "max_date" : { 
     "value" : 1.45922382E12 
     } 
    } 
    } 
} 
{ 
    "fields": [ 
    "Information15", 
    "Information28", 
    "Information68" 
    ], 
    "query": { 
    "match": { 
     "Id": "item456" 
    } 
    }, 
    "aggs": { 
    "date_range": { 
     "filter": { 
     "range": { 
      "ITDiscovery_Date": { 
      "gte": 1420066800000, 
      "lte": 1459241770000 
      } 
     } 
     }, 
     "aggs": { 
     "max_date": { 
      "max": { 
      "field": "ITDiscovery_Date" 
      } 
     } 
     } 
    } 
    } 
} 

は、それはすべての選択した日付範囲内のドキュメントとmax日がないだけで1を返します。

答えて

1

私は最終的に(一時的な)解決策を見つけました。

フィルタリングされたクエリを使用して、指定した範囲の日付に結果を取得します。 次に、ITDiscovery_Dateでソートを使用し、結果を1に制限します。 期待どおりに最新の結果が得られます。例えば

{ 
    "fields": [ 
    "Information15", 
    "Information28", 
    "Information68" 
    ], 
    "sort": [ 
    { "ITDiscovery.Date.raw": {"order": "desc", "ignore_unmapped" : true}} 
    ], 
    "size": 1, 
    "query": { 
    "filtered": { 
     "query": { 
     "query_string": { 
      "query": "Id: item456", 
      "analyze_wildcard": true 
     } 
     }, 
     "filter": { 
     "bool": { 
      "must": [ 
      { 
       "range": { 
       "ITDiscovery.Date": { 
        "gte": 1420070400000, 
        "lte": 1459241770000 
       } 
       } 
      } 
      ], 
      "must_not": [] 
     } 
     } 
    } 
    } 
} 
関連する問題