首页 > 解决方案 > ElasticSearch:Multi_fields 参数不起作用

问题描述

我有一个多字段参数“startTime”,下面是映射

"startTime" : {
      "type" : "text",
      "fields" : {
        "keyword" : {
          "type" : "keyword",
          "ignore_above" : 256
        },
        "raw" : {
          "type" : "date",
          "format" : "dd-MM-yyyy HH:mm:ss||dd-MM-yyyy||hour_minute_second"
        }
      }
    }

我插入了一些文件

{    
  "orgId" => "backendorg",
  "startTime" => "01-01-1980 06:32:51"
}
{
  "orgId" => "backendorg",
  "startTime" => "01-01-1980 06:35:51"
}
{          
  "orgId" => "backendorg",
  "startTime" => "01-01-1980 06:39:51"
}

当我尝试根据以下查询过滤 startTime 时,它​​返回空结果

{
"query": {
"bool": {
  "must": [
    {
      "term": {
        "orgId": {
          "value": "backendorg",
          "boost": 1
        }
      }
    },
    {
      "bool": {
        "should": [
          {
            "range": {
              "startTime": {
                "from": "01-01-1980 06:32:51",
                "to": "01-01-1980 06:39:51",
                "include_lower": true,
                "include_upper": true,
                "boost": 1
                }
              }
            }
          ]
        }
      }
    ]
  }
 }
}

有人能告诉我我的查询或映射有什么问题吗?

标签: elasticsearch

解决方案


由于您的日期字段是一个子字段startTime.raw,因此您需要在range查询中使用它

      {
        "range": {
          "startTime.raw": {                        <----- change this
            "from": "01-01-1980 06:32:51",
            "to": "01-01-1980 06:39:51",
            "include_lower": true,
            "include_upper": true,
            "boost": 1
            }
          }
        }
      ]

推荐阅读