Sai Krishna
Sai Krishna

Reputation: 624

How to pass from and size back in query response

I have a elasticsearch search query which gets the documents in paginated fashion, it is as below:

{
    "from" : 5, "size" : 2,
    "sort" : [
        { "title" : {"order" : "asc"}}
    ],
    "query" : {
        "match_all": {}
    },
    "_source": ["title"]
}

I would like to get from and size back in the response from ES which isn't currently returned from the above query. Any pointer would be very helpful.

Upvotes: 0

Views: 69

Answers (1)

Val
Val

Reputation: 217274

ES does not currently return the pagination parameters that have been sent in the request. You need to hack it... or submit a feature request

You could use named queries in order to pass that information in pretty much any queries, however, you'll need to wait until ES 5 for match_all to support named queries:

{
    "from" : 5, "size" : 2,
    "sort" : [
        { "title" : {"order" : "asc"}}
    ],
    "query" : {
        "match_all": {"_name": "5:2"}  <--- name the query with the from/size params
    },
    "_source": ["title"]
}

In the response, you'll get "5:2" back which you can parse to figure out what from and size were.

  "hits": {
    "total": 1,
    "max_score": 1,
    "hits": [
      {
        "_index": "test",
        "_type": "test",
        "_id": "1",
        "_score": 1,
        "_source": {
          "test": "test"
        },
        "matched_queries": [       <--- you can find from/size here
          "5:2"
        ]
      }
    ]
  }

Upvotes: 1

Related Questions