在Elasticsearch上查询Javascript不适用于子元素



我正试图在reactjs应用程序中的弹性搜索数据库上显示搜索结果。尽管如此,我只能访问键值对的上层,无法访问子级。

我的弹性搜索对象的结构如下:

...
"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?"
}
}}]

弹性搜索对象要大得多,但我只在"点击"中搜索。使用以下代码行,我可以很容易地显示第一级值(_index、_type、_id(,但到目前为止,任何访问子级的尝试都失败了:

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>
)
}

有人能告诉我如何接触_source的孩子吗。例如,我想在我的前端显示作者。

编辑:

因此,该错误显然是由于Elasticsearch中的_source字段没有索引,因此无法搜索(根据弹性搜索文档(
到目前为止,我还没有想出解决方案,但当我找到它时,我会把它发布在这里。我们也非常感谢您的帮助。

假设您有以下数据:

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?"
}
}}]}

现在你的问题是:有人能告诉我如何访问_source的孩子吗。例如,我想在我的前端显示作者

因此,您可以执行以下操作来获得_sourceauthor

{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 : ''}

如果你有多个命中,那么你可以像这样使用array.map((:

data.hits.map(value => value._source && value._source.meta && value._source.meta.author ? value._source.meta.author : '')

最新更新