【问题标题】:Return field even if specific field value isn't available即使特定字段值不可用也返回字段
【发布时间】:2020-09-02 20:36:42
【问题描述】:

我有这个布尔查询:

{
  "bool": {
    "must_not": [
      {
        "exists": {
          "field": "*multiparttype.doNotDisplay",
          "boost": 1
        }
      }
    ],
    "should": [
      {
        "exists": {
          "field": "multiparttype",
          "boost": 1
        }
      },
      {
        "exists": {
          "field": "*multiparttype.oldValue",
          "boost": 1
        }
      },
      {
        "exists": {
          "field": "*multiparttype.newValue",
          "boost": 1
        }
      }
    ]
  }
}

如果 ES 具有以下结构,则返回数据。如果存在如下文档,则此查询将起作用并返回此文档

multiparttype{
  oldValue: "YY",
  newValue:"XXX",
  type:10
}

但如果文件只有这个:

multiparttype{
  type:10
}

multiparttype{

}

以上查询不会返回此文档

我怎样才能使它成为可能??

【问题讨论】:

  • 澄清:_source 不包含多部分类型,因此它不会出现,对吗?
  • 我已经更新了描述,请看一下
  • 对于您的用例,"multiparttype" 不存在 & multiparttype = {} 是两个不同的东西?
  • 这能回答你的问题吗? ElasticSearch null_value with an object?
  • 如果它的multiparttype{ oldValue: "YY", newValue:"XXX", type:10 } 查询工作正常但如果它是multiparttype{ type:10 }multiparttype{ } 或者如果multiparttype 没有退出那么我的查询不起作用。

标签: elasticsearch


【解决方案1】:

根据您的问题,您需要使用 match_all 来匹配所有文档,这将返回得分为“1.0”的所有文档。

索引中有以下数据:

  1. multiparttype = { "oldValue" : "versionX","newValue" : "versionY"}

  2. multiparttype = { "oldValue" : "versionX","newValue" : "versionY"}

  3. empty_field : "test",multiparttype : {}

  4. multiparttype" = {​​"type" : "typetest"}

考虑到可以根据要求更改的提升,已更正以下查询。

    "query": {
    "bool": {
        "should": [
                {
                    "match_all": {}
                    },
                {
                    "exists": {
                        "field": "multiparttype.oldValue",
                        "boost": 1
                    }
                },
                {
                    "exists": {
                        "field": "multiparttype.newValue",
                        "boost": 1
                    }
                }
            ],
            "must_not": [
                {
                    "exists": {
                        "field": "*multiparttype.doNotDisplay"
                    }
                }
            ]
        }
    }

将生成以下响应:

"hits" : {
"total" : {
  "value" : 4,
  "relation" : "eq"
},
"max_score" : 3.0,
"hits" : [
  {
    "_index" : "stackoverflow-field",
    "_type" : "_doc",
    "_id" : "7Qg7TnQB3IIDvL59KA7i",
    "_score" : 3.0,
    "_source" : {
      "multiparttype" : {
        "oldValue" : "versionX",
        "newValue" : "versionY"
      }
    }
  },
  {
    "_index" : "stackoverflow-field",
    "_type" : "_doc",
    "_id" : "1wmWTnQB3IIDvL59lAAL",
    "_score" : 1.0,
    "_source" : {
      "multiparttype" : {
        "type" : "typetest"
      }
    }
  },
  {
    "_index" : "stackoverflow-field",
    "_type" : "_doc",
    "_id" : "tQmbTnQB3IIDvL59Zgy7",
    "_score" : 1.0,
    "_source" : {
      "empty_field" : "test"
    }
  },
  {
    "_index" : "stackoverflow-field",
    "_type" : "_doc",
    "_id" : "tQmcTnQB3IIDvL59fA8Z",
    "_score" : 1.0,
    "_source" : {
      "empty_field" : "test",
      "multiparttype" : { }
    }
  }
 ]
}

文档:https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-all-query.html

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-08-27
    • 1970-01-01
    • 1970-01-01
    • 2020-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-03
    相关资源
    最近更新 更多