15
votes

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
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.user4005632
You will save a lot of time doing another things than just parsing the returned JSON yourself ;-)CodeNotFound

3 Answers

4
votes

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']; 
});
22
votes

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
1
votes

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.