Skip to content

Instantly share code, notes, and snippets.

@shoito
Last active August 29, 2015 14:04
Show Gist options
  • Select an option

  • Save shoito/7bd8800d40d3a5b3da13 to your computer and use it in GitHub Desktop.

Select an option

Save shoito/7bd8800d40d3a5b3da13 to your computer and use it in GitHub Desktop.
ElasticsearchでField Collapsing/Combining http://qiita.com/shoito/items/e56aba6c76f27e16167b
{
"size": 0,
"query": {
"match": {
"title": "ほげ"
}
},
"aggs": {
"hits": {
"terms": {
"field": "gid",
"order": {
"top_hit": "desc"
}
},
"aggs": {
"top": {
"top_hits": {
"sort": [
{
"released_at": {
"order": "desc"
}
}
],
"size": 1
}
},
"top_hit": {
"max": {
"field": "downlaod_count"
}
}
}
}
}
}

この例だと13行目でorderにtop_hitを指定して、downlaod_count(数値のmax)でソートしてるけど、title.raw(※やりたかったのはgroup_name.rawでした)のようにタイトル(文字列)順でソートしたい...。例では、gidでグルーピングし、グループ毎に新しい順(released_at)で上位1件取得。

@johtani

johtani commented Jul 24, 2014

Copy link
Copy Markdown

事前にソート用かaggs用にそのデータを入れておくと幸せかもですね。
良さそうな手を思いつかなくて。。。

@shoito

shoito commented Jul 25, 2014

Copy link
Copy Markdown
Author

おぉー、ありがとうございます。
確かに事前にソート兼aggs用のフィールドを作っておくのがベターな気がしますね。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment