如何在所有文档中查询与键关联的值?

时间:2015-06-01 05:24:00

标签: elasticsearch

假设我的索引中有一些文档如下所示:

{    
  "properties":[
    {
     "name":"foo",
     "value":"2"
    },
    {
     "name":"boo",
     "value":"sausage"
    }
  ]
},
{     
  "properties":[
    {
     "name":"foo",
     "value":"8"
    },
    {
     "name":"boo",
     "value":"chicken"
    }
  ]
}

我想以一种方式查询索引,以便返回所有properties.name及其所有关联的properties.value值。所以结果应该包含这样的东西:

 {
  "foo":["2","8"],
  "boo":["chicken","sausage"]
 }

我怎样才能得到这样的结果?

更新:我可以通过一次查询实现这一目标吗?

1 个答案:

答案 0 :(得分:1)

您可以使用multi search API

$ cat requests
{"index" : "test"}
{"fields" : ["value"], "query" : {"term": {"properties.name": "foo"}}, "from" : 0, "size" : 1000}
{"index" : "test"}
{"fields" : ["value"], "query" : {"term" : {"properties.name": "boo"}}, , "from" : 0, "size" : 1000}

$ curl -XGET localhost:9200/_msearch --data-binary @requests;

或者您可以使用bool查询:

$ curl -XGET localhost:9200/test/type -d 
'{
  "from": 0, "size": 1000,
  "query": {
    "bool": {
      "should": [
                  { "match": { "properties.name": "foo" }},
                  { "match": { "properties.name": "boo"   }}
      ]
    }
  }
}

但你需要自己过滤价值。

相关问题