0

I have an elasticsearch database that I access through a node.js client. I can parse the JSON from elasticsearch to print out the hitsArray which looks like below:

[ { _index: 'parties',
    _type: 'suppliers',
    _id: 'AV0uELknL82XeGsCOZ-i',
    _score: 1,
    _source: { name: 'Jabil', address: [Object], rating: 4.2 } },
  { _index: 'parties',
    _type: 'suppliers',
    _id: 'AV0t_yC3L82XeGsCOZ-f',
    _score: 1,
    _source: { name: 'Apple', address: [Object], rating: 4.9 } },
  { _index: 'parties',
    _type: 'suppliers',
    _id: 'AV0t_glkL82XeGsCOZ-d',
    _score: 1,
    _source: { name: 'Flextronics', address: [Object], rating: 4.5 } },
  { _index: 'parties',
    _type: 'suppliers',
    _id: 'AV0t_ox7L82XeGsCOZ-e',
    _score: 1,
    _source: { name: 'FlashMob', address: [Object], rating: 3.5 } } ]

Now, I want to parse the _source field and print out the name field

client.search(searchParams).then(function (resp) {
    return Promise.all(resp.hits.hits)
  }).then(function(hitsArray){
    hitsArray.forEach(function(value){
      JSONObject first = hitsArray.getJSONObject(value);
      JSONObject source = first.getJSONObject("_source");
      String name = source.getString("name");
      console.log(name);
    });
  });

But I am getting error

JSONObject first = hitsArray.getJSONObject(value);
               ^^^^^
SyntaxError: Unexpected identifier

What am I missing? any suggestion?

1
  • You can use underscore.js for this purpose . it is already implemented in there. May be it can be of some help Commented Jul 11, 2017 at 5:24

1 Answer 1

2

JavaScript has no explicit type definitions like that. It looks like those 3 lines may be Java code and not JavaScript code.

As far as getting the actual JavaScript value goes, it appears to already be an object. Just access it with value._source (and value._source.name).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.