弹性搜索失败,说索引

时间:2016-05-20 18:11:06

标签: elasticsearch

我无法使用日期格式搜索elastic-search

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

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

  "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
}

对此有任何帮助将不胜感激。 请注意我对弹性搜索很新。

1 个答案:

答案 0 :(得分:2)

上述内容不是有效的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"
        }
     }
  ]
相关问题