如何知道DSL Elasticsearch解析的最终查询?

时间:2017-07-21 07:14:58

标签: elasticsearch elasticsearch-5 elasticsearch-dsl

我知道以下两个查询可以获得相同的结果。但有没有办法看到Elasticsearch最终解析的查询,所以我当然可以知道它们是一样的? (或者,它们实际上并不完全相同,可能比另一个花费更少的时间?)

查询1:

GET /_search
{
  "query": {
    "constant_score": {
      "filter": {
        "term": {
          "price": 20
        }
      }
    }
  }
}

查询2:

GET /_search
{
  "query": {
    "term": {
      "price": 20
    }
  }
}

1 个答案:

答案 0 :(得分:1)

为了测试和分析您的查询,您可以使用Slow Log,它允许将查询和获取阶段记录到日志文件中。它具有高度可配置性 - 您可以定义它的含义"慢速查询"对于每个指数。

名为" sample"的索引的简单示例(出于测试目的,它将记录时间设置为" 0s" - 您可以设置自己的阈值):

首先关闭索引:

curl -X POST http://127.0.0.1:9200/sample/_close

然后配置慢速日志:

curl -X PUT \
  'http://127.0.0.1:9200/sample/_settings?preserve_existing=true' \
  -d '{
    "index.indexing.slowlog.threshold.index.debug" : "0s",
    "index.search.slowlog.threshold.fetch.debug" : "0s",
    "index.search.slowlog.threshold.query.debug" : "0s"
}'

打开索引:

curl -X POST http://127.0.0.1:9200/sample/_open

执行您在帖子中提供的较短查询后(我有5个分片,并且针对每个分片执行查询):

[index.search.slowlog.query] [sample][1] took[594.1micros], ..., source[{"query":{"term":{"price":{"value":33,"boost":1.0}}}}], 
[index.search.slowlog.query] [sample][3] took[649.4micros], ..., source[{"query":{"term":{"price":{"value":33,"boost":1.0}}}}], 
[index.search.slowlog.query] [sample][4] took[575.6micros], ..., source[{"query":{"term":{"price":{"value":33,"boost":1.0}}}}], 
[index.search.slowlog.query] [sample][2] took[1.2ms],       ..., source[{"query":{"term":{"price":{"value":33,"boost":1.0}}}}], 
[index.search.slowlog.query] [sample][0] took[4.3ms],       ..., source[{"query":{"term":{"price":{"value":33,"boost":1.0}}}}], 
...

执行您在帖子中提供的较长查询后:

[index.search.slowlog.query] [sample][1] took[13.2ms], ..., source[{"query":{"constant_score":{"filter":{"term":{"price":{"value":33,"boost":1.0}}},"boost":1.0}}}], 
[index.search.slowlog.query] [sample][4] took[13.2ms], ..., source[{"query":{"constant_score":{"filter":{"term":{"price":{"value":33,"boost":1.0}}},"boost":1.0}}}], 
[index.search.slowlog.query] [sample][3] took[14.7ms], ..., source[{"query":{"constant_score":{"filter":{"term":{"price":{"value":33,"boost":1.0}}},"boost":1.0}}}], 
[index.search.slowlog.query] [sample][2] took[15.5ms], ..., source[{"query":{"constant_score":{"filter":{"term":{"price":{"value":33,"boost":1.0}}},"boost":1.0}}}], 
[index.search.slowlog.query] [sample][0] took[15.5ms], ..., source[{"query":{"constant_score":{"filter":{"term":{"price":{"value":33,"boost":1.0}}},"boost":1.0}}}],
...

当然这只是一次尝试,但它对于更深入的测试和分析非常有用。