ElasticSearch文档中多条件的对象数组搜索

pbgvytdp  于 12个月前  发布在  ElasticSearch
关注(0)|答案(1)|浏览(103)

希望大家能帮我找到解决办法,我的问题是:我有一个索引中索引的文档列表,比如

{
 "title" : "document 1",
 "status" 2,
 "documentParts" : [{ "partTitle": "part 1",
                      "typePart" : 1,
                      "statusPart" : 2},
                    { "partTitle": "part 2",
                      "typePart" : 2,
                      "statusPart" : 1}]
 

},
{
 "title" : "document 2",
 "status" 2,
 "documentParts" : [{ "partTitle": "part 2",
                      "typePart" : 2,
                      "statusPart" : 2}]
 

},
{
 "title" : "document 3",
 "status" 2,
 "documentParts" : [{ "partTitle": "part 1",
                      "typePart" : 1,
                      "statusPart" : 1},
                    { "partTitle": "part 2",
                      "typePart" : 2,
                      "statusPart" : 2}]
 

},
{
 "title" : "document 4",
 "status" 2,
 "documentParts" : [{ "partTitle": "part 1",
                      "typePart" : 1,
                      "statusPart" : 1},
                    { "partTitle": "part 2",
                      "typePart" : 2,
                      "statusPart" : 1}]
>

我希望具有“documentParts”和“typePart = 1”和“statusPart = 1”的文档在“documentParts”数组的同一项中。
希望的文档是“document 3”和“document 4”,但当我做以下操作时,我得到了“document 1”,“document 3”,“document 4”,因为查询搜索的是数组的任何元素,而不是同一个数组项。
我的问题是:

{
  "from": 0,
  "size": 10,  
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "status": {
              "value": 2
            }
          }
        },
        {
          "terms": {
            "documentParts.typePart": [ 1 ],
            "_name": "documentPartFilter"
          }
        },
        {
          "terms": {
            "documentParts.statusPart": [ 1 ],
            "_name": "documentPartStatusFilter"
          }
        }
      ]
    }
  }
}

在同一个数组项中搜索的方法是什么?
此查询检索数组中至少匹配一个条件的所有对象。预期的结果应该是数组中的那些对象,所有的条件都是fullfils.

2w3rbyxf

2w3rbyxf1#

你需要使用nested field type
嵌套类型是对象数据类型的特殊版本,允许对象数组以一种可以彼此独立地查询的方式进行索引。

相关问题