以这种形式索引文档时,Elasticsearch将无法正确地将这些字符串解析为日期。如果您将这些字符串转换为正确格式的时间戳记,则可以执行建议的查询的唯一方法是以这种格式索引这些文档
{ "start": "2010-09", "end": "2011-10", // rest of the document}然后对它们运行脚本过滤的查询,然后使用Elasticsearch提供的一种脚本语言编译一个脚本,计算这两个日期之间的差值。请记住,脚本过滤和评分总是比简单的索引查找慢得多。
一种更快,更清洁的方法是,将时间段的长短与开始日期和结束日期一起编制索引,就像这样
{ "start": "2010-09", "end": "2011-10", "duration": 13 // the rest of the document}如果以这种形式索引文档,则只需在工期字段中执行过滤查询:
{ "query":{ "filtered":{ "filter":{ "and":[ { "range":{ "duration":{ "gte":1 } } }, { "range":{ "duration":{ "lte":12 } } } ] } } }}


