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

**URL:** <https://discuss.elastic.co/t/need-help-to-write-elasticsearch-query-which-should-search-based-on-text-of-one-field-and-another-array-field-with-passed-values-or-null/183250>\
**Category:** Elasticsearch\
**Created:** [May 29, 2019, 7:21am UTC](https://discuss.elastic.co/t/need-help-to-write-elasticsearch-query-which-should-search-based-on-text-of-one-field-and-another-array-field-with-passed-values-or-null/183250 "2019-05-29T07:21:35Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![milankamboya](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/milankamboya/32/47041_2.png) [@milankamboya](https://discuss.elastic.co/u/milankamboya)\
**Post date:** [May 29, 2019, 7:21am UTC](https://discuss.elastic.co/t/need-help-to-write-elasticsearch-query-which-should-search-based-on-text-of-one-field-and-another-array-field-with-passed-values-or-null/183250/1 "2019-05-29T07:21:35Z")

</div>

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

```auto
**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))

```auto
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": [
                          ""
                        ]
                      }
                    }
                  ]
                }
              }
            ]
          }
        }
      ]
    }
  }
}

```

```auto
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"
                       }
                    }                
                 }
              }
            ]
          }
        }
      ]
    }
  }
}

```

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

```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [June 26, 2019, 7:21am UTC](https://discuss.elastic.co/t/need-help-to-write-elasticsearch-query-which-should-search-based-on-text-of-one-field-and-another-array-field-with-passed-values-or-null/183250/2 "2019-06-26T07:21:43Z")

</div>

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