小编典典

elasticsearch-如何组合多个must子句?

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
                                            }
                                        }
                                    ]
                                }
                            }
                        }
                    }]
                }
            }
        }
    }
}

现在的问题是,我需要组合这些必须子句。例如,我需要找到一条记录,其“ 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
                                                        }
                                                    }
                                                ]
                                            }
                                        }
                                    ]
                                }
                            }
                        }
                    }]
                }
            }
        }
    }
}

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

谢谢!


阅读 1137

收藏
2020-06-22

共1个答案

小编典典

根据您要实现的目标,是:记录("workExperiences.isCurrentWorkplace": true"workExperiences.title.id": 259
"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
一个对象具有())应该显示一个文档"workExperiences.isCurrentWorkplace": false and "workExperiences.title.id": 256,则查询将为:

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