我在应用程序中使用Elasticsearch在表中的任何位置搜索匹配的单词。
这是我用来获取结果的查询字符串:
search({ query: { prefix: { _all: keywords }}, sort: [ { start_date: 'asc', start_time: 'asc' } ] })
然后通过以下查询,查询所选记录的日期以匹配应用程序中指定的日期范围:
where("status_id= ? and active=? and (((start_date >= ?) and (start_date <= ?))
or ((start_date <= ?) and (? <= end_date)))",2,true,range_start_date,
range_end_date,range_start_date,range_start_date)
但是我知道这不是获取结果的好方法。现在,我想修改它以仅从Elasticsearch索引中获取所需的数据。
经过长时间的搜索,我发现“query_string”和“simple_query_string”符合我的要求。但是直到现在我仍无法获得所需的结果。
如何在查询中附加elasticsearch结果以获取所需的记录?
有人可以帮忙吗?
提前致谢。
最佳答案
最终,我自己找到了问题的答案。我能够使用“过滤器”关键字根据日期过滤搜索到的内容。
我将查询修改为:
search_query = {
query: {
prefix: {
_all: keywords
}
},
filter: {
query: {
query_string: {
query: "status_id:2 AND active:true AND
((start_date:>=#{range_start_date} AND start_date:<=#{range_end_date}) (start_date:<=#{range_start_date} AND end_date:>=#{range_start_date}))"
}
}
},
sort: [ { start_date: 'asc', start_time: 'asc' } ] }
最后通过以下方式获取结果:
@result = self.search(search_query)
如果有的话我可以修改此代码,请提出建议。谢谢。
关于ruby-on-rails-4 - Rails 4中的 Elasticsearch 查询字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38301097/