1

I am trying to display searchresults on a elasticsearch database in my reactjs-app. Nevertheless I can only access the upper layer of key value pairs and am unable to reach the children.

The structure of my elasticsearch-objects is as follows:

...
"hits":[{
"_index":"knowledgecrawler",
"_type":"doc",
"_id":"5fea73f38399f4c01f",
"_score":1.0,
"_source":{
        "content":"content comes here",
        "meta":{
                "author":"Jones@stackoverflow.com",
                "title":"I am lost"
                "date":"today",
                "keywords":["Stackoverflow"],
                "language":"Javascript",
                }
        "raw":{
                "date":"2017-03-08",
                "subject":"How do I get here?"
                }
        }}]

The elasticsearch-object is much bigger, but I am only searching in the "hits". With the following lines of code i can easily display the firstlevel values(_index, _type, _id), but any attempt to access the children failed so far:

render() {
    const result = this.props.result;
        return (
            <div className={this.props.bemBlocks.item().mix(this.props.bemBlocks.container("item"))} key={result._id}>
                <div className={this.props.bemBlocks.item("title")}>{result._id}</div>
                <div className={this.props.bemBlocks.item("title")}>{result._type}</div>
            </div>
        )
    }

Can anybody advice me how to access the children of _source. E.g. I want to display the author in my frontend.

EDIT:

So the error is apparently due to the fact that the _source field in Elasticsearch is not indexed and thus not searchable (according to the elasticsearch docs).
I haven't come up with a solution so far, but will post it here, when I find it. Help is also much appreciated.

Jones1220
  • 786
  • 2
  • 11
  • 22

1 Answers1

1

Let's say you have following data:

data = {"hits":[{
"_index":"knowledgecrawler",
"_type":"doc",
"_id":"5fea73f38399f4c01f",
"_score":1.0,
"_source":{
        "content":"content comes here",
        "meta":{
                "author":"Jones@stackoverflow.com",
                "title":"I am lost"
                "date":"today",
                "keywords":["Stackoverflow"],
                "language":"Javascript",
                }
        "raw":{
                "date":"2017-03-08",
                "subject":"How do I get here?"
                }
        }}]}

Now you question is : Can anybody advice me how to access the children of _source. E.g. I want to display the author in my frontend.

So, you can do the following to get the author of _source:

{data.hits && data.hits.length > 0 && data.hits[0]._source && data.hits[0]._source.meta && data.hits[0]._source.meta.author ?data.hits[0]._source.meta.author : ''}

If you have multiple hits, then you can use array.map() like this:

data.hits.map(value => value._source && value._source.meta && value._source.meta.author ? value._source.meta.author : '')
Triyugi Narayan Mani
  • 3,039
  • 8
  • 36
  • 56