为了测试和分析您的查询,您可以使用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}}}],
...
当然,这只是一次尝试,但它对于更深入的测试和分析非常有用。