每个文档都有几个日期。
我想选择日期范围内的文档(任何日期都适合该范围-该文档匹配)。这部分有效。
我想按日期对所选文档进行排序,但是排序包括所有日期,而不仅仅是匹配的日期。
例如。

doc 1
        "1990-01-01",
        "2000-01-01",
        "2000-01-02",
        "2020-01-01"

doc 2
        "1990-01-01",
        "2000-05-01",
        "2020-01-01"

doc 3
        "1990-01-01",
        "2000-03-01",
        "2020-01-01"
doc 4
        "1990-01-01",
        "2002-03-01",
        "2020-01-01"
如果我选择范围
                  "gte": "1991-03-01",
                  "lte": "2001-03-01"
然后doc 1doc 2doc 3与之匹配。
如果我按日期排序,我希望看到asc(我希望参与排序的日期)doc 1 -> 2000-01-01doc 3 -> 2000-03-01doc 2 -> 2000-05-01但是它似乎使用最早的日期,因此订购不会影响它。
在这里,我附上我试图使测试数据:
DELETE my-index-000001

PUT my-index-000001
{
  "mappings": {
    "properties": {
      "dtmSent": {
        "type": "nested",
        "properties" : {
            "dtmSent" : {
                "type" : "date"
            }
        }

      }
    }
  }
}
PUT my-index-000001/_doc/1
{
  "dtmSent" : [
    {
      "dtmSent" : [
        "1990-01-01",
        "2000-01-01",
        "2000-01-02",
        "2020-01-01"
      ]
    }
  ]
}
PUT my-index-000001/_doc/2
{
  "dtmSent" : [
    {
      "dtmSent" : [
        "1990-01-01",
        "2000-05-01",
        "2020-01-01"
      ]
    }
  ]
}
PUT my-index-000001/_doc/3
{
  "dtmSent" : [
    {
      "dtmSent" : [
        "1990-01-01",
        "2000-03-01",
        "2020-01-01"
      ]
    }
  ]
}
PUT my-index-000001/_doc/4
{
  "dtmSent" : [
    {
      "dtmSent" : [
        "1990-01-01",
        "2002-03-01",
        "2020-01-01"
      ]
    }
  ]
}

GET /my-index-000001/_search
{
  "query": {
    "nested": {
      "path": "dtmSent",
      "query": {
        "bool": {
          "must": [
            {
              "range": {
                "dtmSent.dtmSent": {
                  "gte": "1991-03-01",
                  "lte": "2001-03-01"
                }
              }
            }
          ]
        }
      }
    }
  },
  "sort": [
    {
      "dtmSent.dtmSent": {
        "order": "asc",
        "nested": {
          "path" : "dtmSent",
          "filter" :{
            "range": {
              "dtmSent.dtmSent": {
                "gte": "1991-03-01",
                "lte": "2001-03-01"
              }
            }
          }
        }
      }
    }
  ]
}
请指教我想念的。

最佳答案

您的文档格式不正确。如果您想要适当的嵌套,则需要像这样重构它们:

{
  "dtmSent":[
    {
      "dtmSent":"1990-01-01"
    },
    {
      "dtmSent":"2000-05-01"
    },
    {
      "dtmSent":"2020-01-01"
    }
  ]
}
而不是字符串数组。
索引映射可以保持不变,查询以及排序部分也可以保持不变。之后,订单将按您期望的顺序工作1-> 3-> 2
删除索引后,以下是_bulk命令可帮助您入门(仅在设置了当然的映射之后):
POST _bulk
{"index":{"_index":"my-index-000001","_type":"_doc","_id":1}}
{"dtmSent":[{"dtmSent":"1990-01-01"},{"dtmSent":"2000-01-01"},{"dtmSent":"2000-01-02"},{"dtmSent":"2020-01-01"}]}
{"index":{"_index":"my-index-000001","_type":"_doc","_id":2}}
{"dtmSent":[{"dtmSent":"1990-01-01"},{"dtmSent":"2000-05-01"},{"dtmSent":"2020-01-01"}]}
{"index":{"_index":"my-index-000001","_type":"_doc","_id":3}}
{"dtmSent":[{"dtmSent":"1990-01-01"},{"dtmSent":"2000-03-01"},{"dtmSent":"2020-01-01"}]}
{"index":{"_index":"my-index-000001","_type":"_doc","_id":4}}
{"dtmSent":[{"dtmSent":"1990-01-01"},{"dtmSent":"2002-03-01"},{"dtmSent":"2020-01-01"}]}

关于elasticsearch - ElasticSearch。按日期数组中的日期范围选择并按匹配日期排序,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/64007939/

10-17 03:14