简体   繁体   中英

Keep the documents with a missing field in the filter query results

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM