[英]Firebase List Objects to Observable Array in Angular 2
I've integrated Flashlight with my Angular 2 project.我已经将手电筒与我的 Angular 2 项目集成在一起。 Flashlight does the following:
手电筒执行以下操作:
Responses are written to Firebase with the following structure:响应以以下结构写入 Firebase:
Using Angularfire 2, this is what my search looks like:使用 Angularfire 2,这就是我的搜索结果:
searchThreads(term: string) {
let queryBody: Object = {
index: 'firebase',
type: 'thread',
q: term
}
this.requestKey = this.af.database.list('/search/request').push(queryBody).key
}
if I want the raw results of that search with all paths shown in the image above, I'd do something like this:如果我想要该搜索的原始结果以及上图中显示的所有路径,我会做这样的事情:
this.af.database.list(`search/response/${this.requestKey}`)
What I want is the objects found at _source
.我想要的是在
_source
找到的对象。 I want to iterate over the response hits
and populate my view with those objects.我想遍历响应
hits
并用这些对象填充我的视图。 How can I get just these objects into an observable array?我怎样才能将这些对象放入一个可观察的数组中?
I don't believe it's possible to extract only the _source
children.我不相信只提取
_source
孩子是可能的。 However, you could make it more efficient than extracting the complete search response.但是,您可以使其比提取完整的搜索响应更有效。
Each hit contains little information additional to the source, so just retrieve the hits and map each hit to its _source
.每个命中都包含很少的附加到源的信息,因此只需检索命中并将每个命中映射到其
_source
。
this.af.database
.list(`search/response/${this.requestKey}/hits/hits`)
.map(hits => hits.map(hit => hit._source))
Also, you could use a limit query to extract a subset of hits
:此外,您可以使用限制查询来提取
hits
的子集:
this.af.database
.list(`search/response/${this.requestKey}/hits/hits`, {
query: {
limitToFirst: 10
}
})
.map(hits => hits.map(hit => hit._source))
Or, if you have some criteria for a minimum score, you could perform a query after ordering by child:或者,如果您有一些最低分数标准,您可以在按孩子排序后执行查询:
this.af.database
.list(`search/response/${this.requestKey}/hits/hits`, {
query: {
orderByChild: '_score',
startAt: 1.0
}
})
.map(hits => hits.map(hit => hit._source))
(I've not used Flashlight, so I'm not sure how the scoring works, but some combination of startAt
and/or endAt
should let you refine your query.) (我没有使用手电筒,所以我不确定评分是如何工作的,但是
startAt
和/或endAt
某种组合应该可以让您优化您的查询。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.