0

I know the below two queries can get the same results. But is there a way to see the final parsed query by Elasticsearch, so I can certainly know they are the same? (Or, they are actually not exactly same, maybe one cost less time then the other?)

Query 1:

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

Query 2:

GET /_search
{
  "query": {
    "term": {
      "price": 20
    }
  }
}
curiousY
  • 166
  • 2
  • 9

1 Answers1

1

For testing and analyzing your queries you can use Slow Log which allows to log query and fetch phases into a log file. It's highly configurable - you can define what it means "slow query" for each index.

Simple example for index named "sample" (for test purposes it will be logging with time set to "0s" - you can set your own threshold):

First close the index:

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

Then configure the Slow Log:

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"
}'

Open the index:

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

After executing the shorter query you provided in your post (I've got 5 shards and the query is executed against each of them):

[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}}}}], 
...

After executing the longer query you provided in your post:

[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}}}],
...

Of course this is only a single attempt but it can be very useful for deeper tests and analysis.

Joanna Mamczynska
  • 2,148
  • 16
  • 14