嘿,我是Elasticsearch的新手,想“翻译”此SQL查询:
SELECT"*" FROM Results WHERE "Id"=2 AND Number IN (25,27, 29) AND Date BETWEEN Date1 AND Date 2 ORDER BY Date LIMIT 20
编辑:通过“翻译”,我的意思是我想将SQL语句转换为elaticsearch查询。
我试过了,但是下面的elasticsearch查询尚不起作用:(
我想使用过滤器做到这一点,到目前为止,我已经:
{
"filtered": {
"query": {
"match_all": {}
},
"filter": {
"and": [
{
"range": {
"date": {
"gt": "2008-01-01",
"lt": "2014-01-01"
}
}
},
{
"term": {
"Id": 2
}
},
{
"terms": {
"number": [
25,
27,
29
]
}
},
{
"limit": {
"value": 20
}
}
]
}
}
}
我阅读了文档并尝试了示例,并进行了简单的查询,希望有人可以帮助我!
最佳答案
您的搜索看起来不错-哪一部分无效?
我将使用size来限制返回的结果数(而不是限制所检查的文档数的限制)。
我还添加了sort。
使用filter的缺点是它不做任何评分-可能您应该将Number IN (25,27, 29)
部分移至查询,然后匹配次数更多的结果得分会更高?
不过其他字段看起来也适合过滤器(即二进制匹配/不匹配)。
curl -XGET 'http://localhost:9200/myindex/results/_search?pretty' -d '{
"size" : 20,
"sort" : [{"number" : {"order" : "desc"}}],
"filter": {
"and": [
{
"range": {
"date": {
"gt": "2008-01-01",
"lt": "2014-01-01"
}
}
},
{
"term": {
"Id": 2
}
},
{
"terms": {
"number": [
25,
27,
29
]
}
}
]
}
}'
关于json - Elasticsearch中的SQL查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26951981/