简体   繁体   English

在过滤器查询结果中保留缺少字段的文档

[英]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. job字段是一个字符串数组,查询的工作方式与预期的一样。 Nevertheless, some of the documents do not have the job field, and I want to get those documents as well. 但是,有些文档没有job字段,我也想获取这些文档。

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. 您需要在此处使用should clause ,该should clause在任何一个job字段存在且匹配术语查询 job字段不存在时匹配。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 用变量过滤数组并保留结果 - Filter array with variable and keep results 查询嵌套文档的数组以获得最高的字段值 - Query array of nested documents for highest value of field 跨文档的Elasticsearch查询数组字段 - Elasticsearch query array field across documents 如何通过大于条件的子文档数组中的字段进行查询? - How to query by a field in an array of sub-documents with greater than condition? NodeJ:使用另一个集合的结果过滤mongo查询结果 - NodeJs: Filter mongo query results with results from another collection 按表过滤mySQL查询结果 <td> 使用jQuery吗? - Filter mySQL query results in table by <td> using jQuery? 使用动态字段参数获取Javascript中的OData JSON查询的数组结果 - Get array results of OData JSON query in Javascript with dynamic field parameter Angular/Firestore 集合文档查询将所有文档中的单个文档字段返回到数组中 - Angular/Firestore Collection Document Query to return a single document field from all documents into an array 查询所有元素的字段值相同的子文档数组 - Query an array of sub-documents where a field's value is same for all elements 如何过滤具有多个部门的文档 - How to filter documents with multiple departments
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM