1
votes

I'm querying my index with the following query:

GET /dbpedia201510/entity/_search
{
    "from": 0,
    "size": 10000,
    "query": {
        "bool": {
            "must": {
                "query_string":{
                    "fields": ["name","alias","pseudonyme"],
                    "query": "Elias~ Franck~",
                    "default_operator": "OR",
                    "fuzziness": "auto"
                }
            },
            "filter": {
                "bool": {
                    "must": [
                        {
                            "terms": {
                                "job.keyword":["Architect","Politician","Tailor"]
                            }
                        }
                    ]
                }
            }
        }
    }
}

The job field is an array of strings, and the query works like expected. Nevertheless, some of the documents do not have the job field, and I want to get those documents as well.

How can I do that?

1

1 Answers

4
votes

You need to use should clause here, which matches when either job field exists and matches terms query OR job field doesn't exists.

Your final query should look like this:

GET /dbpedia201510/entity/_search
{
  "from": 0,
  "size": 10000,
  "query": {
    "bool": {
      "must": {
        "query_string": {
          "fields": ["name","alias","pseudonyme"],
          "query": "Elias~ Franck~",
          "default_operator": "OR",
          "fuzziness": "auto"
        }
      },
      "filter": {
        "bool": {
          "should": [
            {
              "terms": {
                "job.keyword": ["Architect","Politician","Tailor"]
              }
            },
            {
              "bool": {
                "must_not": {
                  "exists": {
                    "field": "job"
                  }
                }
              }
            }
          ]
        }
      }
    }
  }
}