0

I want to have a subset of json response from ElasticSearch server. I am new to ElasticSearch and JavaScript. Please have a look. The json file is the example file here from ElasticSearch doc. This is a sample:

{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "failed" : 0
  },
  "hits" : {
    "total" : 1000,
    "max_score" : 1.0,
    "hits" : [ {
      "_index" : "bank",
      "_type" : "account",
      "_id" : "4",
      "_score" : 1.0,
      "_source":{"account_number":4,"balance":27658,"firstname":"Rodriquez","lastname":"Flores","age":31,"gender":"F","address":"986 Wyckoff Avenue","employer":"Tourmania","email":"rodriquezflores@tourmania.com","city":"Eastvale","state":"HI"}
    }, {
      "_index" : "bank",
      "_type" : "account",
      "_id" : "9",
      "_score" : 1.0,
      "_source":{"account_number":9,"balance":24776,"firstname":"Opal","lastname":"Meadows","age":39,"gender":"M","address":"963 Neptune Avenue","employer":"Cedward","email":"opalmeadows@cedward.com","city":"Olney","state":"OH"}
    }
................................
]
  }
}

How to extract an array of account_number, balance and lastname for example [{"account_number":9,"balance":24776,"lastname":"Meadows"},{"account_number":4,"balance":27658,"lastname":"Flores"}, ......... ].

Here is the code I used:

var deferred = $q.defer();          
          $http.get("http://localhost:9200/bank/_search?q=*").success(function(data){          

          var country=data.hits.hists.map( function (count) {
                return {
                 account_number:  count._source.account_number,
                 balance:  count._source.balance,
                 lastname: count._source.lastname                    
                  };
              });    

          deferred.resolve(country);
          });

          return deferred.promise; 

I took inspiration from similar codes like this one for the REST call. It seems that the data I got is undefined. Is there a configuration to do from ES side?

Community
  • 1
  • 1
David
  • 311
  • 1
  • 4
  • 14

1 Answers1

1

You can instruct ES to only return those fields using source filtering:

$http.get("http://localhost:9200/bank/_search?_source=account_number,balance,lastname&q=*").success(function(data){          

That way you don't have to filter out the fields by yourself.

Then you have a typo on the next line: hists should read hits

var country=data.hits.hits.map( function (count) {

Please try it out.

Val
  • 207,596
  • 13
  • 358
  • 360