【问题标题】:How to know the final query DSL Elasticsearch parsed?如何知道 DSL Elasticsearch 解析的最终查询?
【发布时间】:2017-12-27 03:11:28
【问题描述】:

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

查询 1:

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

查询 2:

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

【问题讨论】:

    标签: elasticsearch elasticsearch-5 elasticsearch-dsl


    【解决方案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}}}],
    ...
    

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

    【讨论】:

      猜你喜欢
      • 2017-08-25
      • 2012-01-29
      • 2018-10-17
      • 1970-01-01
      • 1970-01-01
      • 2022-12-15
      • 1970-01-01
      • 2021-02-08
      • 1970-01-01
      相关资源
      最近更新 更多