Elasticsearch嵌套范围查询must_not

vfh0ocws  于 2023-04-20  发布在  ElasticSearch
关注(0)|答案(1)|浏览(91)

我已经在google & stack中寻找了一段时间。在ElasticSearch索引中,我们有一个嵌套字段,其中包含几个表示dare range的日期,如下所示:

"plazos" : [
        {
          "id" : 1,
          "fechainicio" : "01/04/2023",
          "fechafin" : "31/04/2023"
        },
        {
          "id" : 2,
          "fechainicio" : "09/11/2023",
          "fechafin" : "29/11/2023"
        }
      ]

我们可以在嵌套中有2个以上的元素。如果一个元素是IN date today,则必须在嵌套plazos中的一个或多个元素中的fechainiciofechafin之间,这可以很好地工作:

"bool" : {
"must" : [
  {
    "range" : {
      "plazos.fechainicio" : {
        "from" : null,
        "to" : "19/04/2023",
        "include_lower" : true,
        "include_upper" : true,
        "boost" : 1.0
      }
    }
  },
  {
    "range" : {
      "plazos.fechafin" : {
        "from" : "19/04/2023",
        "to" : null,
        "include_lower" : true,
        "include_upper" : true,
        "boost" : 1.0
      }
    }
  }
],
"adjust_pure_negative" : true,
"boost" : 1.0

因为它匹配元素"id":1
问题是如果一个元素是过时的今天必须在没有元素。我已经尝试了相同的请求,但与must_not,但作为一个元素("id":2)是OUT OF DATE,同一个文档匹配IN DATE和OUT OF DATE。我怎么能说“要成为OUT OF DATE,你必须在plazos嵌套元素中的所有元素中都是OUT OF DATE”呢?plazos可以有两个以上的元素。
提前感谢最好的问候
编辑由于隐私和类似的东西,我可以分享一些信息。我分享了一个plazos的例子和范围查询。我想我可以分享plazosMap,但我认为这是我可以分享的限制:

"plazos" : {
        "type" : "nested",
        "properties" : {
          "fechafin" : {
            "type" : "date",
            "format" : "dd/MM/yyyy"
          },
          "fechainicio" : {
            "type" : "date",
            "format" : "dd/MM/yyyy"
          },
          "id" : {
            "type" : "integer",
            "fields" : {
              "keyword" : {
                "type" : "keyword",
                "ignore_above" : 256
              }
            }
          },
          "masinformacion" : {
            "type" : "keyword",
            "fields" : {
              "keyword" : {
                "type" : "keyword",
                "ignore_above" : 256
              }
            }
          }
        }
      }
w6lpcovy

w6lpcovy1#

终于得到了:

"bool" : {
    
    "must_not" : [
      {
        "range" : {
          "plazos.fechainicio" : {
            "from" : "19/04/2023",
            "to" : null,
            "include_lower" : true,
            "include_upper" : true,
            "boost" : 1.0
            
          }
        }
      },
      {
        "range" : {
          "plazos.fechafin" : {
            "from" : null,
            "to" : "19/04/2023",
            "include_lower" : true,
            "include_upper" : true,
            "boost" : 1.0
            
          }
        }
      }
    ],
    "should": [
      {
        "range": {
          "plazos.fechainicio" : {
            "from" : null,
            "to" : "19/04/2023",
            "include_lower" : false,
            "include_upper" : false,
            "boost" : 1.0
            
          }
        }
        
      },
      {
      "range": {
          "plazos.fechafin" : {
            "from" : "19/04/2023",
            "to" : null,
            "include_lower" : false,
            "include_upper" : false,
            "boost" : 1.0
            
          }
        }
      }
    ], 
    "adjust_pure_negative" : true,
    "boost" : 1.0,
    "minimum_should_match": 2
    
    
  }

希望这能有所帮助!

相关问题