Skip to content

Instantly share code, notes, and snippets.

@johtani
Last active December 25, 2015 21:39
Show Gist options
  • Save johtani/7043691 to your computer and use it in GitHub Desktop.
Save johtani/7043691 to your computer and use it in GitHub Desktop.
statistical facetサンプル index:stats_facet type:1001と1002(日付の代わり) 結果のtotalが合計です。
# 2つのインデックスの場合に集計してくれる?
curl -XPOST 'http://localhost:9200/stats_facet1'
curl -XPOST 'http://localhost:9200/stats_facet2'
## データ登録
curl -XPOST 'http://localhost:9200/stats_facet1/1001/1' -d '
{
"person.id" : 1234,
"person.payment":10
}'
curl -XPOST 'http://localhost:9200/stats_facet1/1001/2' -d '
{
"person.id" : 1235,
"person.payment":10
}'
curl -XPOST 'http://localhost:9200/stats_facet1/1001/3' -d '
{
"person.id" : 1234,
"person.payment":30000
}'
curl -XPOST 'http://localhost:9200/stats_facet2/1001/1' -d '
{
"person.id" : 1234,
"person.payment":100
}'
## 検索
curl -XPOST 'http://localhost:9200/stats_facet1,stats_facet2/1001/_search?pretty' -d '
{
"query": {
"query_string": {
"query": "person.id:1234 AND person.payment:[1 TO 10000]"
}
},
"facets": {
"payment_stats": {
"statistical": {
"field": "person.payment"
}
}
}
}'
## 結果
{
"took" : 205,
"timed_out" : false,
"_shards" : {
"total" : 10,
"successful" : 10,
"failed" : 0
},
"hits" : {
"total" : 2,
"max_score" : 1.4142135,
"hits" : [ {
"_index" : "stats_facet2",
"_type" : "1001",
"_id" : "1",
"_score" : 1.4142135, "_source" :
{
"person.id" : 1234,
"person.payment":100
}
}, {
"_index" : "stats_facet1",
"_type" : "1001",
"_id" : "1",
"_score" : 1.4142135, "_source" :
{
"person.id" : 1234,
"person.payment":10
}
} ]
},
"facets" : {
"payment_stats" : {
"_type" : "statistical",
"count" : 2,
"total" : 110.0,
"min" : 10.0,
"max" : 100.0,
"mean" : 55.0,
"sum_of_squares" : 10100.0,
"variance" : 2025.0,
"std_deviation" : 45.0
}
}
}
##インデックス作成
curl -XPOST 'http://localhost:9200/stats_facet'
##登録データ
curl -XPOST 'http://localhost:9200/stats_facet/1001/1' -d '
{
"person.id" : 1234,
"person.payment":10
}'
curl -XPOST 'http://localhost:9200/stats_facet/1001/2' -d '
{
"person.id" : 1235,
"person.payment":10
}'
curl -XPOST 'http://localhost:9200/stats_facet/1001/3' -d '
{
"person.id" : 1234,
"person.payment":30000
}'
curl -XPOST 'http://localhost:9200/stats_facet/1002/1' -d '
{
"person.id" : 1234,
"person.payment":100
}'
##検索
curl -XPOST 'http://localhost:9200/stats_facet/1001,1002/_search?pretty' -d '
{
"query": {
"query_string": {
"query": "person.id:1234 AND person.payment:[1 TO 10000]"
}
},
"facets": {
"payment_stats": {
"statistical": {
"field": "person.payment"
}
}
}
}'
## 結果
{
"took": 11,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 1.4142135,
"hits": [
{
"_index": "stats_facet",
"_type": "1001",
"_id": "1",
"_score": 1.4142135,
"_source": {
"person.id": 1234,
"person.payment": 10
}
},
{
"_index": "stats_facet",
"_type": "1002",
"_id": "1",
"_score": 1.4142135,
"_source": {
"person.id": 1234,
"person.payment": 100
}
}
]
},
"facets": {
"payment_stats": {
"_type": "statistical",
"count": 2,
"total": 110,
"min": 10,
"max": 100,
"mean": 55,
"sum_of_squares": 10100,
"variance": 2025,
"std_deviation": 45
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment