Search code examples
elasticsearchelasticsearch-dsl

How to do a fields query using query string search on elastic search?


I want to convert this query:

GET demo-index/_search
{
 "fields": [
 "*"
 ]
}

into something like this:

GET demo-index/_search?fields=*

is it possible to do fields queries in this way or a way similar to it without using a json body for the request?


Solution

  • You can try with filter_path :

    If we've have the next return:

    {
      "took" : 3,
      "timed_out" : false,
      "_shards" : {
        "total" : 3,
        "successful" : 3,
        "skipped" : 0,
        "failed" : 0
      },
      "hits" : {
        "total" : {
          "value" : 10000,
          "relation" : "gte"
        },
        "max_score" : 1.0,
        "hits" : [
          {
            "_index" : "demo-index",
            "_type" : "_doc",
            "_id" : "32223223d3e23dd23d2x23",
            "_score" : 1.0,
            "_source" : {
              "username" : "Mike",
              "date" : "2022-04-04"
            }
          }
        ]
      }
    }
    

    And we would like to return all fields,we should write the path as follows:

    GET demo-index/_search?filter_path=hits.hits._source.*
    

    If we would like the specifics fields like "username", we should write the path as follows

    GET demo-index/_search?filter_path=hits.hits._source.username