小编典典

在elasticsearchJava API中获取某些条件下的文档

elasticsearch

据我所知,我们可以在elasticsearch中解析文档,并且当我们搜索关键字时,它将使用以下Java API代码返回文档:

  org.elasticsearch.action.search.SearchResponse searchHits =  node.client()
            .prepareSearch()
            .setIndices("indices")
            .setQuery(qb)
            .setFrom(0).setSize(1000)
            .addHighlightedField("file.filename")
            .addHighlightedField("content")
            .addHighlightedField("meta.title")
            .setHighlighterPreTags("<span class='badge badge-info'>")
            .setHighlighterPostTags("</span>")
            .addFields("*", "_source")
            .execute().actionGet();

现在我的问题是,假设某些文档具有如下字符串:

Jun 2010 to Sep 2011                First Document          
Jun 2009 to Aug 2011                Second Document             
Nov 2011 – Sep 2012                 Third Document   
Nov  2012- Sep 2013                 Forth Document   
Nov 2013 – Current                  First Document   
June 2014 – Feb 2015                Third Document   
Jan 2013 – Jan 2014                 Second Document   
July 2008 – Oct 2012                First Document   
May 2007 – Current                  Forth Document

现在,我希望出现以下情况的文件:

1 to 12 months
13-24 months
26-48 months

我该怎么做?


阅读 301

收藏
2020-06-22

共1个答案

小编典典

以这种形式索引文档时,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
                     }
                  }
               }
            ]
         }
      }
   }
}
2020-06-22