0

According to Retrieving a document documentation

 GET /website/blog/123/_source

would directly return the document stored inside the _source field. I'm currently using Node JS's express framework. How should I implement this in my code?

esClient.search({
        index: "myIndex",
        type: "myType",
        body: {

            "query": {
                "match_all": {}
            },
            "size": 3,
            "from": 1

        }
    }).then(function (resp) {
        var result = resp.hits.hits;
        res.status(200).send({data: {recommendations: result, showItemFrom: showItemFrom}})
    }, function (err) {
        console.trace(err.message)
        res.status(500).send({data: err.message})
    })

I'm getting the response this way...

[
  "_source":{
             {
                 "id": 1,
                 "title": "Test"
                }
            } 
]

However, I want it this way...

[
   {
      id:1,
      title:"Test"
   }
]

3 Answers 3

4

I don't think the Elasticsearch API has a method to do that for searches, the one that Val mentioned works, but it is only usable to GET documents directly through its id.

But you can map the result using the Javascript Array#map() method:

var result = resp.hits.hits.map(hit => hit._source);
Sign up to request clarification or add additional context in comments.

1 Comment

Perfect single line solution.
0

After

index:"myIndex"

Add:

source:true

1 Comment

That didn't have any effect.
0

You need to call the getSource() function, like this:

esClient.getSource({
    index: "website",
    type: "blog",
    id: "123"
}).then(function (source) {
   // do something with source
}, function (err) {
   // error happened
})

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.