弹性搜索-如何合并多个must子句?

时间:2018-12-01 09:41:34

标签: elasticsearch

  

对于嵌套对象,我具有以下索引架构:

      "workExperiences": {
        "type": "nested",
        "properties": {
          "isCurrentWorkplace": {
            "type": "boolean"
          },
          "title": {
            "properties": {
              "id": {
                "type": "text"
              },
              "name": {
                "type": "text"
              }
            }
          }
        }
      }

此数据如下:

{
    "hits": [{
            "_source": {
                "workExperiences": [{
                        "isCurrentWorkPlace": true,
                        "title": {
                            "name": "Some name",
                            "id": 259
                        }
                    },
                    {
                        "isCurrentWorkPlace": false,
                        "title": {
                            "name": "Some other name",
                            "id": 256
                        }
                    },
                    {
                        "isCurrentWorkPlace": false,
                        "title": {
                            "name": "another name",
                            "id": 257
                        }
                    }
                ]
            }
        },
        {
            "_source": {
                "workExperiences": [{
                        "isCurrentWorkPlace": true,
                        "title": {
                            "name": "another workplace",
                            "id": 260
                        }
                    },
                    {
                        "isCurrentWorkPlace": false,
                        "title": {
                            "name": "Some other name",
                            "id": 256
                        }
                    },
                    {
                        "isCurrentWorkPlace": false,
                        "title": {
                            "name": "another name",
                            "id": 257
                        }
                    }
                ]
            }
        }
    ]
}

现在,如果我做一些简单的查询,例如查找“ isCurrentWorkplace”为true且title.id为259的工作场所,则它可以正常工作:

{
    "from": 0,
    "size": 30,
    "_source": [
        "workExperiences.*"
    ],
    "query": {
        "bool": {
            "filter": {
                "bool": {
                    "must": [{
                        "nested": {
                            "path": "workExperiences",
                            "query": {
                                "bool": {
                                    "must": [{
                                            "term": {
                                                "workExperiences.title.id": 259
                                            }
                                        },
                                        {
                                            "term": {
                                                "workExperiences.isCurrentWorkPlace": true
                                            }
                                        }
                                    ]
                                }
                            }
                        }
                    }]
                }
            }
        }
    }
}
  

现在的问题是,我需要组合这些must子句。对于   例如,我需要找到一条“ isCurrentWorkplace”为true的记录   并且“ title.id”为259 AND ,其“ isCurrentWorkplace”为false,   “ title.id”是256。

为此,我创建了以下查询:

{
    "from": 0,
    "size": 30,
    "_source": [
        "workExperiences.*"
    ],
    "query": {
        "bool": {
            "filter": {
                "bool": {
                    "must": [{
                        "nested": {
                            "path": "workExperiences",
                            "query": {
                                "bool": {
                                    "must": [{
                                            "bool": {
                                                "must": [{
                                                        "terms": {
                                                            "workExperiences.title.id": [259]
                                                        }
                                                    },
                                                    {
                                                        "term": {
                                                            "workExperiences.isCurrentWorkPlace": true
                                                        }
                                                    }
                                                ]
                                            }
                                        },
                                        {
                                            "bool": {
                                                "must": [{
                                                        "term": {
                                                            "workExperiences.title.id": 256
                                                        }
                                                    },
                                                    {
                                                        "term": {
                                                            "workExperiences.isCurrentWorkPlace": false
                                                        }
                                                    }
                                                ]
                                            }
                                        }
                                    ]
                                }
                            }
                        }
                    }]
                }
            }
        }
    }
} 

但是,这不起作用。有人可以帮我弄清楚我在做什么错吗?

谢谢!

1 个答案:

答案 0 :(得分:2)

基于您要实现的目标:记录("workExperiences.isCurrentWorkplace": true"workExperiences.title.id": 259 OR "workExperiences.isCurrentWorkplace": false"workExperiences.title.id": 256)的位置。因此,您需要should子句而不是must子句:

{
  "query": {
    "bool": {
      "filter": [
        {
          "nested": {
            "path": "workExperiences",
            "query": {
              "bool": {
                "should": [
                  {
                    "bool": {
                      "must": [
                        {
                          "term": {
                            "workExperiences.isCurrentWorkplace": true
                          }
                        },
                        {
                          "term": {
                            "workExperiences.title.id": 259
                          }
                        }
                      ]
                    }
                  },
                  {
                    "bool": {
                      "must": [
                        {
                          "term": {
                            "workExperiences.isCurrentWorkplace": false
                          }
                        },
                        {
                          "term": {
                            "workExperiences.title.id": 256
                          }
                        }
                      ]
                    }
                  }
                ]
              }
            }
          }
        }
      ]
    }
  }
}

如果您的意思是如果同一文档包含两个以上的对象,则该文档应出现,其中workExperiences下的一个对象具有("workExperiences.isCurrentWorkplace": true and "workExperiences.title.id": 259 AND 而一个对象具有({ {1}}),则查询将为:

"workExperiences.isCurrentWorkplace": false and "workExperiences.title.id": 256
相关问题