此项目使用MEAN堆栈编写,需求是一个搜索框,可在整个站点中查找所有相关文档。因此,搜索约翰史密斯'例如,会找到John Smith的用户个人资料和一个按名称提及John Smith的博客文章,以及由John Smith创建的发票。我可以成功搜索所有索引和类型,并将结果返回到我的组件中的异构数组(results[]
),但我的问题是:每个结果元素来自不同的索引/类型,具有差异很大的字段结构,所以这模板专门用于blogpost元素:
<div *ngFor="let result of results">
<p>Title: {{ result._source.detail.title }}</p>
<p>Author: {{ result._source.detail.author }}</p>
<p>Content: {{ result._source.detail.content }}</p>
</div>
...但如果结果元素来自不同的索引/类型,则此模板无法正确呈现,因为例如,没有名为&#39; detail.title&#39;在除blogpost
之外的任何模型中。其他索引/类型中存在但不在blogpost中的任何字段当然也不会显示。
我使用npm包mongoose,elasticsearch和mongoosastic与.synchronize()
方法在每个模型上创建索引。我可以使用curl -X GET 'localhost:9200/_cat/indices?v&pretty'
来查看所有正确的索引是否存在。换句话说,搜索适用于所有索引和所有类型,但我目前只能通过将html模板拟合到该索引/类型来一次正确显示一个索引/类型的结果。
我服务中的搜索方法如下所示:
Search(_index, _field, _queryText): any {
return this.es_client.search({
// index: _index,
// type: _type, // by not specifying type, we are searching all.
filterPath: ['hits.hits._source', 'hits.total', '_scroll_id'],
body: {
'query': {
'multi_match': {
'query': _queryText,
'type': 'best_fields',
'fields': [*.*]
}
}
},
'_source': ['*.*']
});
}
...所以我有一个最通用,全包围的查询返回_source的所有字段。
我应该如何处理这样的问题:在搜索多个索引的结果中,每个_source都有不同的字段?