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

I have data in the mysql database which have been stored into elasticsearch as documents using logstash job.

**Data example in database:**
firstname(text), lastname(text), email(text), tags(text & nullable)

Here, tags conains value as per this in mysql : "t1,t2,t3" or ""

While adding data into elasticsearch, custom analyzer/tokenizer has been applied using ",(comma)".

I need data as per this and need to write the query for this:

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)". Means records should contains tags with empty plus tags matching with t1 or t2 or both.

FYI : Documents will have either like this : tags : ["t4","t5"] or tags : [""]. it will not have as like this ["t5", "t4", ""]
In elasticseach, it is showing me this way: "tags": [""]. So I am thinking this as empty array. is it correct?

I have tried a few queries to get this kind of result but nothing worked. (It doesn't return records with null tags as well tags with (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"
                       }
                    }                
                 }
              }
            ]
          }
        }
      ]
    }
  }
}
{
  "query": {
    "bool": {
      "must": [
        {
          "multi_match": {
            "query": "some_text",
            "fields": [
              "firstname^1.0"
            ]
          }
        },
        {
          "bool": {
            "should": [
              {
                "terms": {
                  "tags": [
                    "t1",
                    "t2"
                  ]
                }
              },
              {
                "bool": {
                  "must_not": {
                    "exists": {
                      "field": "tags"
                    }
                  }
                }
              }
            ]
          }
        }
      ]
    }
  }
}

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.