在弹性搜索java API中获取一些条件的文档 [英] Get document on some condition in elastic search java API

查看:155
本文介绍了在弹性搜索java API中获取一些条件的文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我所知,我们可以在弹性搜索中解析文档,当我们搜索一个关键字时,它将使用这个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();

现在我的问题是,假设有些文档有这样的字符串: -

  2010年6月至2011年9月第一份文件
2009年6月至2011年8月第二份文件
2011年11月 - 2012年9月第三文件
2011年11月 - 2013年9月Forth文件
2013年11月 - 当前第一份文件
2014年6月 - 2015年2月第三文件
2013年1月 - 2014年1月第二份文件
2008年7月 - 2012年10月第一份文件
2007年5月 - 当前文件

现在我想要这些条件之间的文件: -

  1至12个月
13-24个月
26-48个月

我该怎么做?

解决方案

在此f中索引文档时orm,Elasticsearch将无法正确解析这些字符串作为日期。如果您将这些字符串转换为正确格式化的时间戳,您可以执行您提出的查询的唯一方法是以这种格式索引这些文档

  {
开始:2010-09,
结束:2011-10,
//文件的其余部分
}

,然后运行脚本过滤查询,编译一个脚本,用脚本语言弹性搜索来计算这两个日期之间的差异。请记住,脚本过滤和评分总是比简单的索引查找慢得多。



更快更干净的方法是为期间索引在开始和结束日期之前,像这样

  {
开始:2010-09,
end:2011-10,
duration:13
//文件的其余部分
}
pre>

如果您以此表单索引您的文档,您可以在持续时间字段上简单地执行过滤的查询:

  {
查询:{
过滤:{
过滤器:{
和:[
{
range:{
duration:{
gte:1
}
}
},
{
range:{
duration:{
lte:12
}
}
}
]
}
}
}
}


As I know we can parse document in elastic search, And when we search for a keyword, It will return the document using this code of 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();

Now my question is, suppose some documents have string like these:-

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   

Now i want those documents who comes between these conditions:-

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

How i can do this?

解决方案

When indexing documents in this form, Elasticsearch will not be able to parse those strings as dates correctly. In case you transformed those strings to correctly formatted timestamps, the only way you could perform the query you propose is to index those documents in this format

{
  "start": "2010-09",
  "end": "2011-10",
  // rest of the document
}

and subsequently run a script-filtered query over them, compiling a script that calculates the difference between those two dates with one of the scripting languages Elasticsearch provides. Bear in mind that script filtering and scoring is always much slower than a simple index lookup.

A much faster and cleaner way to do this is to index the duration of the period alongside the start and end dates, like so

{
  "start": "2010-09",
  "end": "2011-10",
  "duration": 13
  // the rest of the document
}

If you index your documents in this form, you can simply perform a filtered query on the duration field:

{
   "query":{
      "filtered":{
         "filter":{
            "and":[
               {
                  "range":{
                     "duration":{
                        "gte":1
                     }
                  }
               },
               {
                  "range":{
                     "duration":{
                        "lte":12
                     }
                  }
               }
            ]
         }
      }
   }
}

这篇关于在弹性搜索java API中获取一些条件的文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆