23

I post a query to elasticsearch get data of index.. I only need these fields data and how many documents found information... but there is "took","shards" and inside of a document "_id","_index","_score". these are un necessary for my purpose..

Here my simple request:

query='{"query": {"match_all": {}}}';
 $.ajax({
        url: "http://localhost:9200/webproxylog/_search?source=" + query,
        type:"GET",
        dataType: "json",
        data: $.param(params),
        success: function(data) {...

I check the response data in success method here how it seems: enter image description here

I just want to get hits which documents in, and in documents I just want to "_source" object that have fields data."took","shards","_id","_index", unneccessary, how can I disable them

3
  • 2
    Why do you not using ElasticJs elastic.co/guide/en/elasticsearch/client/javascript-api/current/… ? Commented Nov 7, 2015 at 15:51
  • because I didnt know such a library exist :D interesting.. then you mean its easy with e.sj to get only content, I dont want parse with response.hits.hits (in documents you share)I just want elasticsearch return exactly what I need. Commented Nov 7, 2015 at 15:58
  • You will save a lot of time doing another things than just parsing the returned JSON yourself ;-) Commented Nov 7, 2015 at 15:59

3 Answers 3

36

Yes you can remove the extra fields from _source

Answer is just one simple word filter_path

Curl :

curl -XGET 'http://localhost:9200/webproxylog/_search?filter_path=hits.hits._source'

Node :

If you are using any node 'elasticsearch' you need to add just one extrs param filterPath

client.search({
        index: 'index',
        type: 'type',
        filterPath : ['hits.hits._source'], // this will remove extra fileds _index / _score / _id ...
        body: {
            sort: [
                {"posted_dt": {"order": "desc"}},
                "_score"
            ],
            query: query,
            size: 50,
            from: index * 50
        }
    }

In your case :

you just need to add that extra field in your url like:

"http://localhost:9200/webproxylog/_search?filter_path=hits.hits._source&source=" + query
Sign up to request clarification or add additional context in comments.

1 Comment

for anyone coming here and looking for a solution in python: using elasticsearch official client for python filter path is an argument you can pass in the search method of the library.
6

You can't turn off the response meta-data. You can return fields instead of _source if you only want specific fields, but that doesn't really decrease the complexity. The library can abstract some of that away, but I don't find it terribly difficult to simply parse the ES responses directly.

You do, of course, have to write a little JavaScript. Luckily it's not too hard. Something like this usually works nicely for me:

var results = es_response_data['hits']['hits'].map(function(i){
    return i['_source']; 
});

1 Comment

Yes that's idea sounds good to me. thank you in the other hand I am not awere of difficulty of parse it, these unnecessary data would be post to client and consume bandwidth and slow performance a little.. I just wonder it is possible or not turn off that:)
0

Just use the ElasticJS, the API recommended by ElasticSearch will help JS client to communicate easily with elasticsearch nodes. You will save a lot of time by using this API.

1 Comment

I got your point, if I were begining I could use this, but I just need 2-3 type of only search, so cant add external library to project at this time. But its useful knowledge thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.