日期在弹性搜索中没有正确解析

时间:2015-05-30 07:04:02

标签: elasticsearch

在Elasticsearch中,我的索引“记录”并输入“记录”。此索引类型包含一些文档。 索引类型映射的结构

"mappings": {

    "records": {
        "date_detection": false,
        "properties": {
            "dateModified": {
                "format": "dd/mm/yyyy",
                "type": "date"
            },
            "org": {
                "type": "string"
            }
        }
    }
}

我的文件是

{
    "org": "pm",
    "dateModified": "01/12/2015"
},
{
    "org": "muthuraj",
    "dateModified": "10/01/2013"

},
{
    "org": "user",
    "dateModified": "12/05/2015"

},
 {
    "org": "satish",
    "dateModified": "2/05/2014"

}

我编写查询以获取文档dateModified> “2013年2月5日”

{
    "query": {
        "bool": {
            "must": [
                {
                    "range": {
                        "records.dateModified": {
                            "gt": "02/05/2013"
                        }
                    }
                }
            ]
        }
    }
}

我得到了结果 {

"took": 14,
"timed_out": false,
"_shards": {
    "total": 2,
    "successful": 2,
    "failed": 0
},
"hits": {
    "total": 4,
    "max_score": 1,
    "hits": [
        {
            "_index": "records",
            "_type": "records",
            "_id": "NXD8t3TSQxev2WMcNTdchQ",
            "_score": 1,
            "_source": {
                "org": "pm",
                "dateModified": "01/12/2015"
            }
        }
        ,
        {
            "_index": "records",
            "_type": "records",
            "_id": "vyxdivH8RLujCFSNWMQTdg",
            "_score": 1,
            "_source": {
                "org": "muthuraj",
                "dateModified": "10/01/2013"
            }
        }
        ,
        {
            "_index": "records",
            "_type": "records",
            "_id": "QqDTULhwSqOGykYCOOrcHw",
            "_score": 1,
            "_source": {
                "org": "user",
                "dateModified": "12/05/2015"
            }
        }
        ,
        {
            "_index": "records",
            "_type": "records",
            "_id": "btPGMDPgRPimNgLHK2y2OA",
            "_score": 1,
            "_source": {
                "org": "satish",
                "dateModified": "2/05/2014"
            }
        }
    ]
}

}

“dateModified”:“10/01/2013”​​这比“2015年2月2日”还要小,但这也是为什么它会像这样来的结果。 如何解决这个问题

2 个答案:

答案 0 :(得分:0)

您发布的内容存在一些问题(您的映射没有正确构建,并且您发布的文档在任何情况下都不会被您的查询返回),但也许这些只是方式的工件你发布了你的问题。

你想做的事对我有用。首先,我使用您的映射创建了一个简单的索引(结构合理,类型名称为"doc"):

DELETE /test_index

PUT /test_index
{
   "mappings": {
      "doc": {
         "date_detection": false,
         "properties": {
            "dateCreated": {
               "properties": {
                  "date": {
                     "type": "date",
                     "format": "dd/mm/yyyy"
                  },
                  "time": {
                     "type": "string"
                  }
               }
            },
            "dateModified": {
               "type": "date",
               "format": "dd/mm/yyyy"
            }
         }
      }
   }
}

然后索引了几份文件:

PUT /test_index/doc/1
{
   "Project": "02ff5704273e",
   "ProjectSpace": "tigerPark",
   "relationDesc": [
      "Project-hasSpace-recordId",
      "recordId-forProject-Project"
   ],
   "org": "user",
   "docType": "ProjectSpace",
   "dateCreated": {
      "date": "22/05/2015",
      "time": "18:28 IST"
   },
   "dateModified": "22/05/2015",
   "revision": 1
}

PUT /test_index/doc/2
{
   "Project": "02ff5704273e",
   "ProjectSpace": "tigerPark",
   "relationDesc": [
      "Project-hasSpace-recordId",
      "recordId-forProject-Project"
   ],
   "org": "user",
   "docType": "ProjectSpace",
   "dateCreated": {
      "date": "30/05/2015",
      "time": "18:28 IST"
   },
   "dateModified": "30/05/2015",
   "revision": 1
}

然后,当我运行您的查询时,返回了正确的文档:

POST /test_index/_search
{
   "query": {
      "range": {
         "dateModified": {
            "gt": "28/05/2015"
         }
      }
   }
}
...
{
   "took": 29,
   "timed_out": false,
   "_shards": {
      "total": 5,
      "successful": 5,
      "failed": 0
   },
   "hits": {
      "total": 1,
      "max_score": 1,
      "hits": [
         {
            "_index": "test_index",
            "_type": "doc",
            "_id": "2",
            "_score": 1,
            "_source": {
               "Project": "02ff5704273e",
               "ProjectSpace": "tigerPark",
               "relationDesc": [
                  "Project-hasSpace-recordId",
                  "recordId-forProject-Project"
               ],
               "org": "user",
               "docType": "ProjectSpace",
               "dateCreated": {
                  "date": "30/05/2015",
                  "time": "18:28 IST"
               },
               "dateModified": "30/05/2015",
               "revision": 1
            }
         }
      ]
   }
}

所以你的问题似乎在其他地方。我认为这不重要,但我使用的是Elasticsearch 1.5.1。

以下是我使用的所有代码:

http://sense.qbox.io/gist/2a97abc1530060b02f3b1a68ab81e5cd84bff915

答案 1 :(得分:0)

问题是"format": "dd/mm/yyyy"。 ES认为mm是指分钟,因此您需要使用MM。我总是在ES中发现日期格式有点令人困惑,而且记录不是很好,但它非常强大。您可以查看此reference

使用问题的第二个版本,我可以设置如下索引:

DELETE /test_index

PUT /test_index
{
   "settings": {
      "number_of_shards": 1
   },
   "mappings": {
      "records": {
         "date_detection": false,
         "properties": {
            "dateModified": {
               "format": "dd/MM/yyyy",
               "type": "date"
            },
            "org": {
               "type": "string"
            }
         }
      }
   }
}

POST /test_index/records/_bulk
{"index":{"_id":1}}
{"org": "pm","dateModified": "01/12/2015"}
{"index":{"_id":2}}
{"org": "muthuraj","dateModified": "10/01/2013"}
{"index":{"_id":3}}
{"org": "user","dateModified": "12/05/2015"}
{"index":{"_id":4}}
{"org": "satish","dateModified": "2/05/2014"}

然后查询按预期工作:

POST /test_index/_search
{
   "query": {
      "bool": {
         "must": [
            {
               "range": {
                  "records.dateModified": {
                     "gt": "02/05/2013"
                  }
               }
            }
         ]
      }
   }
}
...
{
   "took": 2,
   "timed_out": false,
   "_shards": {
      "total": 1,
      "successful": 1,
      "failed": 0
   },
   "hits": {
      "total": 3,
      "max_score": 1,
      "hits": [
         {
            "_index": "test_index",
            "_type": "records",
            "_id": "1",
            "_score": 1,
            "_source": {
               "org": "pm",
               "dateModified": "01/12/2015"
            }
         },
         {
            "_index": "test_index",
            "_type": "records",
            "_id": "3",
            "_score": 1,
            "_source": {
               "org": "user",
               "dateModified": "12/05/2015"
            }
         },
         {
            "_index": "test_index",
            "_type": "records",
            "_id": "4",
            "_score": 1,
            "_source": {
               "org": "satish",
               "dateModified": "2/05/2014"
            }
         }
      ]
   }
}

以下是代码:

http://sense.qbox.io/gist/24e468f010ef9323e5974b1e0534c755cb097cfd