在Elasticsearch中查询数组中的部分匹配

时间:2015-01-29 19:47:03

标签: elasticsearch

我按以下格式索引数据:

PUT /index/..
   {'keywords' => [
      {'keyword' => 'foo', 
       'keyword' => 'bar'}
   ]}

然后我想在keyword内获取fookeywords的数据对象。我试过了:

 GET /index/..
  { query: {
      match: {
        'keyword' => 'foo'
    }
  }

  GET /index/..
   { query: {
      term: {
        'keywords.keyword' => 'foo'
    }
  }

但它没有从PUT返回对象,在这种情况下,正确的部分匹配查询是什么?

1 个答案:

答案 0 :(得分:0)

文档编入索引时,

"keyword":"foo"会被"keyword":"bar"覆盖,因为内部对象只能有一个名为"keyword"的字段。

为了演示,我创建了一个索引并保存了一个像您发布的文档:

DELETE /test_index

PUT /test_index

PUT /test_index/doc/1
{
    "keywords": [
        {
            "keyword": "foo",
            "keyword": "bar"
        }
    ]
}

然后,当我搜索所有文档时,会返回此文档:

POST /test_index/_search
...
{
   "took": 50,
   "timed_out": false,
   "_shards": {
      "total": 5,
      "successful": 5,
      "failed": 0
   },
   "hits": {
      "total": 1,
      "max_score": 1,
      "hits": [
         {
            "_index": "test_index",
            "_type": "doc",
            "_id": "1",
            "_score": 1,
            "_source": {
               "keywords": [
                  {
                     "keyword": "bar"
                  }
               ]
            }
         }
      ]
   }
}

因此,您的匹配查询与文档不匹配,因为keywords.keyword未设置为"foo"

这是您可以采用的一种方法。如果将两个关键字保留在"keywords"列表中的单独内部对象中,则查询将按预期工作:

DELETE /test_index

PUT /test_index

PUT /test_index/doc/1
{
    "keywords": [
        { "keyword": "foo" },
        { "keyword": "bar" }
    ]
}

POST /test_index/_search
{
   "query": {
      "term": {
         "keywords.keyword": {
            "value": "foo"
         }
      }
   }
}
...
{
   "took": 2,
   "timed_out": false,
   "_shards": {
      "total": 5,
      "successful": 5,
      "failed": 0
   },
   "hits": {
      "total": 1,
      "max_score": 0.19178301,
      "hits": [
         {
            "_index": "test_index",
            "_type": "doc",
            "_id": "1",
            "_score": 0.19178301,
            "_source": {
               "keywords": [
                  {
                     "keyword": "foo"
                  },
                  {
                     "keyword": "bar"
                  }
               ]
            }
         }
      ]
   }
}

以下是我使用的代码:

http://sense.qbox.io/gist/763a939d6146eb7a43c5e19e5bfd261a88fddf7b