在Firebase中的有序列表中获取对象的索引 [英] Getting the index of an object in an ordered list in Firebase

查看:47
本文介绍了在Firebase中的有序列表中获取对象的索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Firebase构建排行榜.使用Firebase的优先级系统可以跟踪玩家在排行榜中的位置.

在程序执行的某个时刻,我需要知道给定用户在排行榜中的位置.我可能有成千上万的用户,因此遍历所有这些用户以找到具有相同ID(因此为我提供索引)的对象并不是真正的选择.

在Firebase中,有没有更有效的方法来确定对象在有序列表中的索引?

编辑:我正在尝试找出以下内容:

 /----排行榜-------- user4 {...}-------- user1 {...}-------- user3 {...}<-给定user3的快照,user3的索引是什么?--------... 

解决方案

如果您正在处理数十或数百个元素,并且不介意占用带宽,请参见 Kato 的答案.

如果要处理大量的记录,则需要遵循 pperrin 的答案中原则上概述的方法.以下答案对此进行了详细说明.

步骤1:设置Flashlight以使用ElasticSearch索引排行榜

Flashlight是一种方便的节点脚本,可将Elasticsearch与Firebase数据同步.

在此处了解如何进行设置.

步骤2:修改Flashlight,以允许您将查询选项传递给ElasticSearch

在撰写本文时,Flashlight无法让您告诉ElasticSearch您只对匹配的文档的 number 感兴趣,而对文档本身的 感兴趣.

我已提交此请求请求,该请求使用简单的单行修复程序添加此功能.如果在您阅读此答案时尚未关闭,只需手动更改手电筒的副本/叉子.

第3步:执行查询!

这是我通过Firebase发送的查询:

  {索引:"firebase",类型:"allTime",询问: {已过滤":{查询":{" match_all" ;: {}},过滤器":{范围":{积分":{"gte":minPoints}}}}},选项: {"search_type":"count";}}; 

points 替换为用户的字段跟踪点的名称,将 minPoints 替换为您感兴趣的用户的点数.

响应将类似于:

  {max_score:0,合计:2} 

是具有相同或更多点数的用户数,换句话说,就是用户的排名!

I'm building a leaderboard using Firebase. The player's position in the leaderboard is tracked using Firebase's priority system.

At some point in my program's execution, I need to know what position a given user is at in the leaderboard. I might have thousands of users, so iterating through all of them to find an object with the same ID (thus giving me the index) isn't really an option.

Is there a more performant way to determine the index of an object in an ordered list in Firebase?

edit: I'm trying to figure out the following:

/
---- leaderboard
--------user4 {...}
--------user1 {...}
--------user3 {...} <- what is the index of user3, given a snapshot of user3?
--------...

解决方案

If you are processing tens or hundreds of elements and don't mind taking a bandwidth hit, see Katos answer.

If you're processing thounsands of records, you'll need to follow an approach outlined in principle in pperrin's answer. The following answer details that.

Step 1: setup Flashlight to index your leaderboard with ElasticSearch

Flashlight is a convenient node script that syncs elasticsearch with Firebase data.

Read about how to set it up here.

Step 2: modify Flashlight to allow you to pass query options to ElasticSearch

as of this writing, Flashlight gives you no way to tell ElasticSearch you're only interested in the number of documents matched and not the documents themselves.

I've submitted this pull request which uses a simple one-line fix to add this functionality. If it isn't closed by the time you read this answer, simply make the change in your copy/fork of flashlight manually.

Step 3: Perform the query!

This is the query I sent via Firebase:

{
    index: 'firebase',
    type: 'allTime',
    query: {
        "filtered": {
            "query": {
                "match_all": {}
            },
            "filter": {
                "range": {
                    "points": {
                        "gte": minPoints
                    }
                }
            }
        }
    },
    options: {
        "search_type": "count"
    }
};

Replace points with the name of the field tracking points for your users, and minPoints with the number of points of the user whose rank you are interested in.

The response will look something like:

{
    max_score: 0,
    total: 2
}

total is the number of users who have the same or greater number of points -- in other words, the user's rank!

这篇关于在Firebase中的有序列表中获取对象的索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆