我将Rails 4.1.4
与searchkick (1.3.0)
一起使用,因此我可以使用弹性搜索。
我有User
型号
class User
searchkick
def search_data
{
name: username,
email: email,
created_at: created_at.strftime("%d-%m-%Y")
}
end
end
created_at
格式的 dd-MM-yyyy
。如何搜索日期范围,让我们说明从01-01-2014
到01-01-2015
的日期。
我厌倦了像User.search('*', where: {created_at: {gte:'01-01-2014', lte: '01-01-2015' } })
这样的东西而没有得到正确的结果。
有任何帮助吗?
答案 0 :(得分:2)
class User
searchkick
def search_data
{
name: username,
email: email,
created_at: created_at.to_time
}
end
end
而不是
created_at:created_at.strftime("%d-%m-%Y")
尝试
created_at:created_at.to_time
答案 1 :(得分:1)
我不熟悉Searchkick,但如果您使用elasticsearch gem,则可以轻松向ElasticSearch发送搜索请求,例如
query = {
query: {
bool: {
must: [
{
term: {
username: "Kevin"
}
},
{
term: {
email: "email@gmail.com"
}
},
{
range: {
created_at: {
lte: "2016-06-06",
gte: "2016-06-06",
format: "yyyy-MM-dd"
}
}
}
]
}
}
}
搜索请求如下:
client = Elasticsearch::Client.new
client.search(
index: index_name,
body: {
query: query
}
)
希望这有帮助!
答案 2 :(得分:0)
形成一个合适的DateTime对象,你的代码应该可行 只需使用
utl smtp