【问题标题】:SQL Query in ElasticsearchElasticsearch 中的 SQL 查询
【发布时间】:2015-01-13 03:25:11
【问题描述】:

嘿,我是 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 查询。 我试过了,但下面的弹性搜索查询还没有工作:(

我想使用过滤器来做到这一点,到目前为止:

{
"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
                }
            }
        ]
    }
}

}

我阅读了文档并尝试了示例,进行了简单的查询,希望有人可以帮助我!

【问题讨论】:

标签: json elasticsearch lucene


【解决方案1】:

您的搜索看起来不错 - 哪个部分不起作用?

我会使用size 来限制返回的结果数量(而不是使用限制检查文档数量的limit)。 我还添加了sort

使用过滤器的缺点是它不会进行任何评分 - 可能您应该将 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
                ]
            }
        }
    ]
  }
}'

【讨论】:

  • 在您的帮助下,当我删除排序部分中的“日期”时,它现在可以正常工作了。否则我得到 [No mapping found for [date] in order to sort on] 我在 DATE 上排序而不是 NUMBER
  • 很高兴它对您有用,您能接受其中一个答案吗?
猜你喜欢
  • 2019-09-24
  • 2015-08-03
  • 2019-03-29
  • 2018-04-07
  • 2018-06-28
  • 1970-01-01
  • 1970-01-01
  • 2012-01-23
相关资源
最近更新 更多