使用累积过滤器设计 ElasticSearch 查询,但过滤器内的选项具有 "OR" 条件

Designing an ElasticSearch query with cummulative filters, but with "OR" condition for the options inside the filter

我正在尝试编写一个 ElasticSearch 查询来搜索索引中的联系人。用户可以选择写一个搜索词,也可以从几个过滤器中 select。

custom_field_1custom_field_2 外,一切正常。这些是用户可以定义的自定义过滤器,可以有一个或多个可能的选项 select from.

我想设计查询,使这些自定义过滤器与其他过滤器具有 AND 关系,并且与过滤器内的 selected 选项具有 OR 关系。

基本上,如果用户从 custom_field_1blahcustom_field_2 编辑了 select 和 foobar,则查询需要查找包含 foobar 且必须包含 blah.

的记录

目前我得到的是这个,但它没有提供预期的结果:

{
  "query": {
    "bool": {
      "must": [
        {
          "multi_match": {
            "query": "john",
            "type": "cross_fields",
            "fields": [
              "first_name",
              "last_name",
              "username",
              "email"
            ],
            "operator": "and"
          }
        },
        {
          "bool": {
            "should": [
              {
                "match": {
                  "custom_field_1": {
                    "query": "foo"
                  }
                }
              },
              {
                "match": {
                  "custom_field_1": {
                    "query": "bar"
                  }
                }
              },
              {
                "match": {
                  "custom_field_2": {
                    "query": "blah"
                  }
                }
              }
            ]
          }
        },
        {
          "bool": {
            "should": [
              {
                "terms": {
                  "status": [
                    "new",
                    "contacted",
                    "offer",
                    "contract"
                  ]
                }
              },
              {
                "exists": {
                  "field": "updated_at"
                }
              }
            ]
          }
        }
      ],
      "filter": [
        {
          "term": {
            "user_id": 357
          }
        },
        {
          "range": {
            "created_at": {
              "gte": "2021-05-01 00:00:00",
              "lte": "2021-05-31 23:59:59"
            }
          }
        }
      ]
    }
  }
}

使用 custom_field_1 和 custom_field_2 的术语查询。

{
  "query": {
    "bool": {
      "must": [
        {
          "multi_match": {
            "query": "john",
            "type": "cross_fields",
            "fields": [
              "first_name",
              "last_name",
              "username",
              "email"
            ],
            "operator": "and"
          }
        }
      ],
      "filter": [
        {
          "terms": {
            "custom_field_1": ["foo", "bar"]
          }
        },
        {
          "terms": {
            "custom_field_2": ["blah"]
          }
        },
        {
          "term": {
            "user_id": 357
          }
        },
        {
          "range": {
            "created_at": {
              "gte": "2021-05-01 00:00:00",
              "lte": "2021-05-31 23:59:59"
            }
          }
        }
      ],
      "should": [
            {
                "terms": {
                    "status": [
                        "new",
                        "contacted",
                        "offer",
                        "contract"
                    ]
                }
            },
            {
                "exists": {
                    "field": "updated_at"
                }
            }
        ]
    }
  }
}