【问题标题】:Elastic search failing ,saying date-format not supported in index弹性搜索失败,说索引中不支持日期格式
【发布时间】:2016-05-21 03:08:16
【问题描述】:

我无法使用弹性搜索日期格式进行搜索

我创建了一个索引,字段映射显示为日期格式,但查询失败。

在加载数据之前,我创建了一个带有映射的索引并将我的数据加载到弹性搜索中。

  "mappings": {
     "workers": {
        "person.birthDate": {
           "full_name": "person.birthDate",
           "mapping": {
              "birthDate": {
                 "type": "date",
                 "format": "yyyy-MM-dd||epoch_millis"
              }
           }
        }

我的输入查询如下

POST _search
{
   "query": {


  "bool" : {
    "must" : [ {
      "match" : {
        "person.birthDate" : {
          "query" : "1968-06-15",
          "type" : "date"
        }
      }
    } ]
  }



   },
   "from": 0,
   "size": 10,
   "sort": [],
   "aggs": {}
}

输出是

{
   "error": {
      "root_cause": [
         {
            "type": "query_parsing_exception",
            "reason": "[match] query does not support type date",
            "index": "index1",
            "line": 9,
            "col": 33
         }
      ],
      "type": "search_phase_execution_exception",
      "reason": "all shards failed",
      "phase": "query",
      "grouped": true,
      "failed_shards": [
         {
            "shard": 0,
            "index": "index1",
            "node": "RewaFar_TsGVGz1RmgOxlA",
            "reason": {
               "type": "query_parsing_exception",
               "reason": "[match] query does not support type date",
               "index": "index1",
               "line": 9,
               "col": 33
            }
         }
      ]
   },
   "status": 400
}

对此的任何帮助将不胜感激。 请注意,我对弹性搜索非常陌生。

【问题讨论】:

    标签: elasticsearch


    【解决方案1】:

    以上不是有效的match 查询。 type:date 不是匹配查询的有效选项。
    如果你想过滤日期,推荐range query

    例子:

    put test 
    {
        "mappings": {
            "test": {
                   "properties" : {
                        "birthDate": {
                            "type": "date",
                            "format": "yyyy-MM-dd||epoch_millis"
                        }
                   }
               }
        }
    }
    
    put test/test/1 
    {
        "birthDate" : "2016-05-20"
    }
    
    ##timestamp for 2016-05-20 19:00:24 GMT
    put test/test/2
    {
        "birthDate" : 1463771107000
    }
    
    post test/test/_search
    {
        "query": {
           "range" : {
                "birthDate" : {
                    "gte" : "2016-05-20",
                    "lt" :  "2016-05-21",
                     "format": "yyyy-MM-dd||epoch_millis"
                 }
        }
    }
    
    
        "hits": [
         {
            "_index": "test",
            "_type": "test",
            "_id": "2",
            "_score": 1,
            "_source": {
               "birthDate": 1463771107000
            }
         },
         {
            "_index": "test",
            "_type": "test",
            "_id": "1",
            "_score": 1,
            "_source": {
               "birthDate": "2016-05-20"
            }
         }
      ]
    

    【讨论】:

    • 这行得通。感谢您的帮助。打算更新一段时间后才接受答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多