简体   繁体   English

Firebase 将对象列出到 Angular 2 中的 Observable 数组

[英]Firebase List Objects to Observable Array in Angular 2

I've integrated Flashlight with my Angular 2 project.我已经将手电筒与我的 Angular 2 项目集成在一起。 Flashlight does the following:手电筒执行以下操作:

  1. Automatically indexes specified Firebase paths to an ElasticSearch cluster and monitors those paths for changes, re-indexing any changes.自动将指定的 Firebase 路径编入 ElasticSearch 集群的索引,并监控这些路径的更改,重新索引任何更改。
  2. Responds to query objects written to a specified Firebase path (eg, 'search/request'), writing ElasticSearch responses to a specified Firebase path (eg, 'search/response').响应写入指定 Firebase 路径(例如“搜索/请求”)的查询对象,将 ElasticSearch 响应写入指定的 Firebase 路径(例如“搜索/响应”)。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM