json - Elasticsearch中的SQL查询

标签 json elasticsearch lucene

嘿,我是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/

相关文章:

javascript - AngularJS:循环 JSON 并将值传递给 ng-model

javascript - 如何使用一维数组形成 JSON 数组?

elasticsearch - 两台Elasticsearch服务器之间的负载平衡

elasticsearch - 什么是elasticsearch索引、Lucene索引和倒排索引

search - Elasticsearch定制词干算法

java - 使用 Lucene 7.4.0 打印索引术语

javascript - 通过php或javascript读取hasoffers api生成的json数据

java - 简单的 json 到 xml 转换

elasticsearch - Elasticsearch Dedupe结果,每个 “field value”返回1个文档

将elasticsearch索引 block 标记为null的Java请求