我使用轮胎在rails应用程序中搜索。假设我在索引中有这些值:
-- value -- time
red shoes 3 weeks ago
red car yesterday
red blue car 6 months ago
如何提升结果,以便搜索red
会显示最新值?
这不像按时间排序那么简单,因为搜索red blue
应该返回
我意识到我总是可以在轮胎查找之后重新订购结果,但我只是想知道轮胎是否可以为我做这件事。我觉得我必须将Proc
传递给:boost
,因为Time.now
必须在搜索时进行评估 - 但我不认为:boost
可以接受Proc
1}}。
mapping(:date_detection => false) do
indexes :id, :index => :not_analyzed
indexes :label, :type => 'string', :analyzer => :label_analyzer
indexes :booked_date, :type => 'date', :index => :not_analyzed
end
def self.search(q)
tire.search do
query do
boolean do
should { string "label:#{q}", boost: -> { #use Time.now and :booked_date to determine a boost } }
end
end
# sort { by :booked_date, 'desc'}
end
end
根据接受的答案更新。这就是诀窍:
def self.search(q)
tire.search do
query do
boolean do
should { string "label:#{q}" }
should { range :booked_date, { from: "now-6M", boost: 4}}
should { range :booked_date, { from: "now-3M", boost: 5}}
end
end
end
end
感谢
答案 0 :(得分:1)
我不知道厌倦了,但像这样的查询会做你要求的,但你必须自己定义时间段。
"query": {
"bool": {
"must": [{ //your query goes here }]
"should": [
{
"range": {
"timefield": {
"from": "now-1d",
"boost": 3.0
}
}
},
{
"range": {
"timefield": {
"from": "now-1w",
"boost": 2.0
}
}
},
{
"range": {
"timefield": {
"from": "now-1M",
"boost": 1.0
}
}
}
]
}
}
祝你好运!