需要帮助编写 elasticsearch 查询,该查询应基于一个字段的文本和另一个数组字段(传递的值或 null)进行搜索

Need help to write elasticsearch query which should search based on text of one field and another array field with (passed values or null)

我在 mysql 数据库中有数据,这些数据已使用 logstash 作业作为文档存储到 elasticsearch 中。

**Data example in database:**

firstname(text), lastname(text), email(text), tags(text & nullable)

在这里,标签包含 mysql 中的值:"t1,t2,t3" 或“”

将数据添加到 elasticsearch 时,已使用“,(逗号)”应用自定义 analyzer/tokenizer。

我需要这样的数据并且需要为此编写查询:

firstname = "text", tags = ["t1","t2"]

"Get all records which contain this firstname and tags with either "t1 or t2 or (t1 & t2) or null" (any in which tags are empty)".

我已经尝试了一些查询来获得这种结果,但没有任何效果。 (它不 return 记录带有空标签以及带有 (t1,t2) 的标签)

GET /posts/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "bool": {
            "must": [
              {
                "multi_match": {
                  "query": "some_text",
                  "fields": [
                    "firstname^1.0"
                  ]
                }
              },
              {
                "bool": {
                  "should": [
                    {
                      "terms": {
                        "tags": [
                          "t2"
                        ]
                      }
                    }
                  ]
                }
              }
            ]
          }
        },
        {
          "bool": {
            "must": [
              {
                "multi_match": {
                  "query": "some_text",
                  "fields": [
                    "firstname^1.0"
                  ]
                }
              },
              {
                "bool": {
                  "should": [
                    {
                      "terms": {
                        "tags": [
                          ""
                        ]
                      }
                    }
                  ]
                }
              }
            ]
          }
        }
      ]
    }
  }
}
GET /posts/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "bool": {
            "must": [
              {
                "multi_match": {
                  "query": "some_text",
                  "fields": [
                    "firstname^1.0"
                  ]
                }
              },
              {
                "bool": {
                  "should": [
                    {
                      "terms": {
                        "tags": [
                          "t2"
                        ]
                      }
                    }
                  ]
                }
              }
            ]
          }
        },
        {
          "bool": {
            "must": [
              {
                "multi_match": {
                  "query": "some_text",
                  "fields": [
                    "firstname^1.0"
                  ]
                }
              },
              {
                "bool": {
                   "must_not": {
                      "exists": {
                          "field": "tags"
                       }
                    }                
                 }
              }
            ]
          }
        }
      ]
    }
  }
}

将空检查 (nust_not -> exists) 与 tags.

terms 查询一起放在 should 子句中
{
  "query": {
    "bool": {
      "must": [
        {
          "multi_match": {
            "query": "some_text",
            "fields": [
              "firstname^1.0"
            ]
          }
        }
      ],
      "should": [
        {
          "terms": {
            "tags": [
              "t1",
              "t2",
              ""
            ]
          }
        },
        {
          "bool": {
            "must_not": {
              "exists": {
                "field": "tags"
              }
            }
          }
        }
      ],
      "minimum_should_match": 1
    }
  }
}

{
  "query": {
    "bool": {
      "must": [
        {
          "multi_match": {
            "query": "some_text",
            "fields": [
              "firstname^1.0"
            ]
          }
        },
        {
          "bool": {
            "should": [
              {
                "terms": {
                  "tags": [
                    "t1",
                    "t2",
                    ""
                  ]
                }
              },
              {
                "bool": {
                  "must_not": {
                    "exists": {
                      "field": "tags"
                    }
                  }
                }
              }
            ]
          }
        }
      ]
    }
  }
}