3

I want to get only _source fields by the query.but it returns hits which are unnecessary for me.so how to remove this hits before the _source data.

GET fms/user/_search?filter_path=hits.hits._source{"query": {"match_all": {}}}

enter image description here

Community
  • 1
  • 1
Sachin
  • 71
  • 2
  • 14
  • 1
    This answer might help: https://stackoverflow.com/questions/31569422/elastic-search-exclude-index-and-type-from-json-response/31570789#31570789 (hint: use `filter_path`) – Val Oct 03 '17 at 05:46
  • I'm already using filter_path but I just want the _source portion without the hits and hits – Sachin Oct 03 '17 at 05:55
  • 1
    You are only getting _source field only, it is just that it is wrapped in hits since it is a part of JSON._source is nested json element of hits. – Girdhar Sojitra Oct 03 '17 at 05:58
  • You can use jq filter..this answer might help https://stackoverflow.com/questions/43758813/elasticsearch-return-total-hits-only/43758962#43758962 – Girdhar Sojitra Oct 05 '17 at 06:23

1 Answers1

1

If you want to filter _source fields, you should consider combining the already existing _source parameter with the filter_path parameter like this:

POST /library/book?refresh
{"title": "Book #1", "rating": 200.1}
POST /library/book?refresh
{"title": "Book #2", "rating": 1.7}
POST /library/book?refresh
{"title": "Book #3", "rating": 0.1}

GET /_search?filter_path=hits.hits._source&_source=title&sort=rating:desc

{
  "hits" : {
    "hits" : [ {
      "_source":{"title":"Book #1"}
    }, {
      "_source":{"title":"Book #2"}
    }, {
      "_source":{"title":"Book #3"}
    } ]
  }
}

For more details, go through at https://www.elastic.co/guide/en/elasticsearch/reference/current/common-options.html

As you are already using filter_path, you are already getting only source field only.

Girdhar Sojitra
  • 648
  • 4
  • 14