数组搜索中包含弹性搜索的数组

时间:2015-02-15 17:03:46

标签: arrays elasticsearch lucene

我的用户索引类别如下

{
 id: 1
 name: John
 categories: [
   {
    id: 1
    name: Category 1
   },
   {
    id: 2
    name: Category 2
   }
 ]
},
{
 id: 2
 name: Mark
 categories: [
   {
    id: 1
    name: Category 1
   }
 ]
}

我试图通过

获取所有类别1或类别2的文档
{
 filter:
   {
     bool: {
       must: [
         {
           terms: {user.categories.id: [1, 2]}
         }
       ]
     }
   }
}

但它只返回有两个类别的第一个文档,我做错了什么? 据我所知,术语搜索其中一个值包含在字段中,因此对于用户1 user.categories.id:[1,2] 用户2 user.categories.id:[1] 分类ID 1包含在两个文档中

1 个答案:

答案 0 :(得分:1)

处理此问题的最佳方法可能是使用nested filter。但是,您必须在映射中指定"nested"类型。

我可以设置这样的索引:

PUT /test_index
{
   "settings": {
      "number_of_shards": 1,
      "number_of_replicas": 0
   },
   "mappings": {
      "doc": {
         "properties": {
            "categories": {
               "type": "nested",
               "properties": {
                  "id": {
                     "type": "long"
                  },
                  "name": {
                     "type": "string"
                  }
               }
            },
            "id": {
               "type": "long"
            },
            "name": {
               "type": "string"
            }
         }
      }
   }
}

然后添加一些文档:

PUT /test_index/doc/1
{
   "id": 1,
   "name": "John",
   "categories": [
      { "id": 1, "name": "Category 1" },
      { "id": 2, "name": "Category 2" }
   ]
}

PUT /test_index/doc/2
{
   "id": 2,
   "name": "Mark",
   "categories": [
      { "id": 1, "name": "Category 1" }
   ]
}

PUT /test_index/doc/3
{
   "id": 3,
   "name": "Bill",
   "categories": [
      { "id": 3, "name": "Category 3" },
      { "id": 4, "name": "Category 4" }
   ]
}

现在我可以使用这样的嵌套术语过滤器:

POST /test_index/doc/_search
{
   "query": {
      "constant_score": {
         "filter": {
         "nested": {
            "path": "categories",
            "filter": {
                "terms": {
                   "categories.id": [1, 2]
                }
            }
         }
         },
         "boost": 1.2
      }
   }
}
...
{
   "took": 3,
   "timed_out": false,
   "_shards": {
      "total": 1,
      "successful": 1,
      "failed": 0
   },
   "hits": {
      "total": 2,
      "max_score": 1,
      "hits": [
         {
            "_index": "test_index",
            "_type": "doc",
            "_id": "1",
            "_score": 1,
            "_source": {
               "id": 1,
               "name": "John",
               "categories": [
                  {
                     "id": 1,
                     "name": "Category 1"
                  },
                  {
                     "id": 2,
                     "name": "Category 2"
                  }
               ]
            }
         },
         {
            "_index": "test_index",
            "_type": "doc",
            "_id": "2",
            "_score": 1,
            "_source": {
               "id": 2,
               "name": "Mark",
               "categories": [
                  {
                     "id": 1,
                     "name": "Category 1"
                  }
               ]
            }
         }
      ]
   }
}

以下是我使用的代码:

http://sense.qbox.io/gist/668aefe910643b52a3a10d40aca67104491668fc