在弹性搜索中获取某些条件下的文档 java API
Get document on some condition in elastic search java API
据我所知,我们可以在弹性搜索中解析文档,当我们搜索关键字时,它将 return 使用 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
我该怎么做?
以这种形式索引文档时,Elasticsearch 将无法将这些字符串正确解析为日期。如果您将这些字符串转换为 correctly formatted timestamps,您可以执行您建议的查询的唯一方法是以这种格式索引这些文档
{
"start": "2010-09",
"end": "2011-10",
// rest of the document
}
随后 运行 对它们进行 script-filtered 查询,编译一个脚本,使用 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
}
}
}
]
}
}
}
}
据我所知,我们可以在弹性搜索中解析文档,当我们搜索关键字时,它将 return 使用 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
我该怎么做?
以这种形式索引文档时,Elasticsearch 将无法将这些字符串正确解析为日期。如果您将这些字符串转换为 correctly formatted timestamps,您可以执行您建议的查询的唯一方法是以这种格式索引这些文档
{
"start": "2010-09",
"end": "2011-10",
// rest of the document
}
随后 运行 对它们进行 script-filtered 查询,编译一个脚本,使用 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
}
}
}
]
}
}
}
}