在弹性搜索中,当我运行聚合查询时,它只在一个索引上运行它。如何让它在多个指标上运行?
答案 0 :(得分:3)
作为一个例子,我通过批量索引一些数据创建了几个索引(隐式):
POST /_bulk
{ "index": {"_index": "test_index", "_type":"doc"}}
{ "name": "Brown foxes"}
{ "index": {"_index": "test_index_2", "_type":"doc"}}
{ "name": "Yellow furballs" }
{ "index": {"_index": "test_index", "_type":"doc"}}
{ "name": "my discovery" }
{ "index": {"_index": "test_index_2", "_type":"doc"}}
{ "name": "myself is fun" }
{ "index": {"_index": "test_index", "_type":"doc"}}
{ "name": ["foxy", "foo"] }
{ "index": {"_index": "test_index_2", "_type":"doc"}}
{ "name": ["foo bar", "baz"] }
然后我可以在两个索引上运行聚合:
POST /test_index,test_index_2/_search?search_type=count
{
"aggs": {
"name_terms": {
"terms": {
"field": "name"
}
}
}
}
我从两者中取回所有条款:
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 10,
"successful": 10,
"failed": 0
},
"hits": {
"total": 6,
"max_score": 0,
"hits": []
},
"aggregations": {
"name_terms": {
"buckets": [
{
"key": "foo",
"doc_count": 2
},
{
"key": "bar",
"doc_count": 1
},
{
"key": "baz",
"doc_count": 1
},
{
"key": "brown",
"doc_count": 1
},
{
"key": "discovery",
"doc_count": 1
},
{
"key": "foxes",
"doc_count": 1
},
{
"key": "foxy",
"doc_count": 1
},
{
"key": "fun",
"doc_count": 1
},
{
"key": "furballs",
"doc_count": 1
},
{
"key": "is",
"doc_count": 1
}
]
}
}
}
以下是代码:
http://sense.qbox.io/gist/e053a0c6c5453eae68d7b7ff2ff12588669b046e