如何在 should 和 must 弹性搜索 bool 查询之间建立 AND 条件

How to built AND condition between should and must elastic search bool query

这是示例 USER 文档

{
  "id" : "1234567",
  "userId" : "testuser01",
  "firstName" : "firstname",
  "lastName" : "lastname",
  "orgId" : "567890",
  "phoneNumber" : "1234567890"
}

我想建立一个搜索查询,我想在其中提取属于特定 orgId 并且与用户在任何字段中输入的搜索文本相匹配的所有用户(用户 ID、名字等)

例如。如果使用文本“first”进行搜索,我想提取所有属于特定 orgId 的记录以及其中包含 first 的字段。

我正在尝试的示例查询是

    "query" : {
    "bool" : {
      "must" : [
        {
          "term" : {
            "orgId.keyword" : {
              "value" : "567890",
              "boost" : 1.0
            }
          }
        }
      ],
      "should" : [
        {
          "simple_query_string" : {
            "query" : "first*",
            "fields" : [
              "lastName^1.0"
            ],
            "flags" : -1,
            "default_operator" : "or",
            "lenient" : false,
            "analyze_wildcard" : true,
            "boost" : 1.0
          }
        },
        {
          "simple_query_string" : {
            "query" : "first*",
            "fields" : [
              "userId^1.0"
            ],
            "flags" : -1,
            "default_operator" : "or",
            "lenient" : false,
            "analyze_wildcard" : true,
            "boost" : 1.0
          }
        },
        {
          "simple_query_string" : {
            "query" : "first*",
            "fields" : [
              "orgId^1.0"
            ],
            "flags" : -1,
            "default_operator" : "or",
            "lenient" : false,
            "analyze_wildcard" : true,
            "boost" : 1.0
          }
        },
        {
          "simple_query_string" : {
            "query" : "first*",
            "fields" : [
              "firstName^1.0"
            ],
            "flags" : -1,
            "default_operator" : "or",
            "lenient" : false,
            "analyze_wildcard" : true,
            "boost" : 1.0
          }
        },
        {
          "simple_query_string" : {
            "query" : "first*",
            "fields" : [
              "phoneNumber^1.0"
            ],
            "flags" : -1,
            "default_operator" : "or",
            "lenient" : false,
            "analyze_wildcard" : true,
            "boost" : 1.0
          }
        },
        {
          "simple_query_string" : {
            "query" : "first*",
            "fields" : [
              "id^1.0"
            ],
            "flags" : -1,
            "default_operator" : "or",
            "lenient" : false,
            "analyze_wildcard" : true,
            "boost" : 1.0
          }
        }
      ],
      "disable_coord" : false,
      "adjust_pure_negative" : true,
      "boost" : 1.0
    }
  },
  "sort" : [
    {
      "userId.keyword" : {
        "order" : "asc"
      }
    }
  ]
}

我面临的问题是,我想在 MUST 和 SHOULD 之间设置 AND 条件。

您不需要为 query_string 查询中的每个字段指定查询。相反,您可以指定字段列表,如下所示:

{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "orgId.keyword": {
              "value": "567890",
              "boost": 1
            }
          }
        },
        {
          "simple_query_string": {
            "query": "first*",
            "fields": [
              "lastName^1.0",
              "userId^1.0",
              "orgId^1.0",
              "firstName^1.0",
              "phoneNumber^1.0",
              "id^1.0"
            ]
          }
        }
      ]
    }
  },
  "sort": [
    {
      "userId.keyword": {
        "order": "asc"
      }
    }
  ]
}

也来回答

How to built AND condition between should and must elastic search bool query?

这是一个示例查询:

{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "field1": "someval"
          }
        },
        {
          "bool": {
            "should": [
              {
                "terms": {
                  "field2": [
                    "v1",
                    "v2"
                  ]
                }
              },
              {
                "query_string": {
                  "query": "this AND that OR thus"
                }
              }
            ]
          }
        }
      ]
    }
  }
}