我正在使用elasticsearch-rails gem和elasticsearch-model gem,并编写了一个非常庞大的查询,这仅仅是因为gem接受查询的方式。
查询本身不是很长,但是过滤器非常非常长,我需要传入变量才能正确地筛选出结果。下面是一个示例:
def search_for(input, question_id, tag_id)
query = {
:query => {
:filtered => {
:query => {
:match => {
:content => input
}
},
:filter => {
:bool => {
:must => [
{
# another nested bool with should
},
{
# another nested bool with must for question_id
},
{
# another nested bool with must for tag_id
}
]
}
}
}
}
}
User.search(query) # provided by elasticsearch-model gem
end为了简洁起见,我省略了其他嵌套的bools,但正如您可以想象的那样,这可能会变得相当长,相当快。
有人对如何储存这个有什么想法吗?我正在考虑一个yml文件,但这似乎是错误的,特别是因为我需要传入question_id和tag_id。还有其他想法吗?
如果有人熟悉这些宝石,并且知道gem的search方法是否接受其他格式,我也想知道。在我看来,它只是想要一些可以变成哈希的东西。
发布于 2015-11-15 18:24:46
我认为用一种方法是好的。我会将搜索与查询分开:
def query_for(input, question_id, tag_id)
query = {
:query => {
...
end
search query_for(input, question_id, tag_id)而且,我看到这个搜索功能在用户模型中,但是我不知道它是否属于那里。有一个Search或Query模型更有意义吗?
https://stackoverflow.com/questions/33723171
复制相似问题