2017-06-14 28 views
0

私はElasticsearchを初めて使用しているため、次の問題を解決する方法を見つけることができません。 私の問題を説明する最も簡単な方法は、例を示すことです。アレイ内の特定のエントリのみの弾性検索集計

次の配列のリストはElasticsearchのすべてのファイルに含まれていますが、エントリが異なるため "ID" 42の "人"がファイルの50%にある可能性があります。私がしようとしているのは、ElasticsearchのすべてのファイルでID 42のすべての人物の平均「ランキング。位置。標準」を得ることです。

{ 
"listing": [ 
    { 
     "person": { 
      "id": 42 
     }, 
     "ranking": { 
      "position": { 
       "standard": 2 
      } 
     } 
    }, 
    { 
     "person": { 
      "id": 55 
     }, 
     "ranking": { 
      "position": { 
       "standard": 7 
      } 
     } 
    } 
] 
} 

ありがとうございました!

+0

これまでに何を試しましたか? –

+0

私はあらゆる種類のフィルタリングを試みました。これまで達成してきたのは、 "listing"配列が "id" 42のエントリを持っていれば、ファイル全体を戻すことでした。 – McClane

答えて

0

まず、objectまたはnestedデータ型としてリストを保存しますか?私はそれがobjectだ場合、仕事に行くと思うので、次の例を試してみません:それは正しいように見えるん

{ 
    "took": 1, 
    "timed_out": false, 
    "_shards": { 
    "total": 5, 
    "successful": 5, 
    "failed": 0 
    }, 
    "hits": { 
    "total": 2, 
    "max_score": 0, 
    "hits": [] 
    }, 
    "aggregations": { 
    "nest": { 
     "doc_count": 4, 
     "persons": { 
     "doc_count_error_upper_bound": 0, 
     "sum_other_doc_count": 0, 
     "buckets": [ 
      { 
      "key": 42, 
      "doc_count": 2, 
      "avg_standard": { 
       "value": 3.5 
      } 
      }, 
      { 
      "key": 55, 
      "doc_count": 2, 
      "avg_standard": { 
       "value": 6.5 
      } 
      } 
     ] 
     } 
    } 
    } 
} 

PUT /test 
{ 
    "mappings": { 
    "_default_": { 
     "properties": { 
     "listing": { 
      "type": "nested" 
     } 
     } 
    } 
    } 
} 

PUT /test/aa/1 
{ 
    "listing": [ 
    { 
     "person": { 
     "id": 42 
     }, 
     "ranking": { 
     "position": { 
      "standard": 2 
     } 
     } 
    }, 
    { 
     "person": { 
     "id": 55 
     }, 
     "ranking": { 
     "position": { 
      "standard": 7 
     } 
     } 
    } 
    ] 
} 

PUT /test/aa/2 
{ 
    "listing": [ 
    { 
     "person": { 
     "id": 42 
     }, 
     "ranking": { 
     "position": { 
      "standard": 5 
     } 
     } 
    }, 
    { 
     "person": { 
     "id": 55 
     }, 
     "ranking": { 
     "position": { 
      "standard": 6 
     } 
     } 
    } 
    ] 
} 

GET test/_search 
{ 
    "size": 0, 
    "aggs": { 
    "nest": { 
     "nested": { 
     "path": "listing" 
     }, 
     "aggs": { 
     "persons": { 
      "terms": { 
      "field": "listing.person.id", 
      "size": 10 
      }, 
      "aggs": { 
      "avg_standard": { 
       "avg": { 
       "field": "listing.ranking.position.standard" 
       } 
      } 
      } 
     } 
     } 
    } 
    } 
} 

これは私に次のような結果をもたらしました。

+0

ありがとう。デフォルトのマッピングが生成されました: "listing":{ "プロパティ":{...} } しかし、正確にその結果を得るにはどうしましたか? – McClane

+0

@McClaneあなたのために働いた場合は、これを受け入れるようにチェックしてください;) –

+0

ありがとうございました!まさに私が探していたもの。 – McClane

関連する問題