我知道以下两个查询可以得到相同的结果。但是有没有办法看到Elasticsearch最终解析的查询,所以我当然可以知道它们是相同的?(或者,它们实际上并不完全相同,也许一个比另一个花费更少的时间?
查询 1:
GET /_search
{
"query": {
"constant_score": {
"filter": {
"term": {
"price": 20
}
}
}
}
}
查询 2:
GET /_search
{
"query": {
"term": {
"price": 20
}
}
}
为了测试和分析您的查询,您可以使用慢日志,它允许将查询和获取阶段记录到日志文件中。它是高度可配置的 - 您可以为每个索引定义"慢查询"的含义。
名为"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}}}],
...
当然,这只是一次尝试,但它对于更深入的测试和分析非常有用。