gpt4 book ai didi

ruby-on-rails - 如何使用 Rails 将一个索引的 "foreign key"id 与 Elasticsearch 中另一个索引的相应数据相关联?

转载 作者:行者123 更新时间:2023-12-02 22:40:58 25 4
gpt4 key购买 nike

情况

Elasticsearch 有索引“用户”和“调用”,用户可以在其中调用或接听许多电话。在数据库端,调用表的用户 ID 充当用户表的外键。 Elasticsearch 中的这种关系与数据库设计直接相关。

下面是一个使用 Sense 的 Elasticsearch JSON 输出示例:

GET /calls/call/23


{
"_index": "calls",
"_type": "call",
"_id": "23",
"_version": 2,
"found": true,
"_source": {
"id": 23,
"user1_id": 1, //Jack "called"
"user2_id": 12, // George "received"
"start_time": "2015-05-29T16:01:28.233Z",
"end_time": null,
"status": "NONE",
"reason": null,
"created_at": null,
"updated_at": null
}
}

这是一个 Rails 应用程序,所以我的问题是当我在调用 View 中按名称搜索调用时,什么都找不到,因为只有 id(user1_id 和 user2_id)存储在调用索引中。如果按名称搜索类似于此数据库查询的调用,我基本上需要 Elasticsearch 返回结果:
select calls.id AS "Call id", users.id AS "User Id", users.name AS "Name" from calls, users where (calls.user1_id=users.id OR calls.user2_id=users.id) AND users.name LIKE '%Jack%';

上面的查询当然会返回由搜索的姓名 Jack 发出和接收的所有调用,同时比较用户和调用 ID。

所需输出
{
"_index": "calls",
"_type": "call",
"_id": "23",
"_version": 2,
"found": true,
"_source": {
"id": 23,
"dialer": {
"id": 1,
"name": "Jack"
},
"receiver": {
"id": 12,
"name": "George"
},
"start_time": "2015-04-27T17:29:04.868Z",
"end_time": "2015-04-27T17:29:59.198Z",
"status": "SUCCESS",
"reason": "User hungup",
"created_at": null,
"updated_at": null
}
}

根据我在 Elasticsearch 中创建新映射或创建自定义搜索查询时收集到的信息,我认为我需要使用新的源映射“user1_name”和“user2_name”创建调用索引;然后认为 Elasticsearch 会相应地填充这些源映射,将 user1_id 和 user2_id 与相应用户索引中的名称相关联。

我试过的

我在 Sense 中尝试了一个 HTTP 请求,它手动创建了我想要的树结构,但只针对一个调用项:
PUT /calls/call/23
{
"id": 23,
"dialer": {
"id": 1,
"name": "Jack"
},
"receiver": {
"id": 12,
"name": "George"
},
"start_time": "2015-04-27T17:29:04.868Z",
"end_time": "2015-04-27T17:29:59.198Z",
"status": "SUCCESS",
"reason": "User hungup",
"created_at": null,
"updated_at": null
}

我还尝试创建别名以希望结合关系索引:
POST /_aliases
{
"actions" : [
{ "add" : { "index" : "users", "alias" : "searchcalls" } },
{ "add" : { "index" : "calls", "alias" : "searchcalls" } }
]
}

还有这个: http://www.spacevatican.org/2012/6/3/fun-with-elasticsearch-s-children-and-nested-documents/

但是我不能指望我为数万个可能数百万个电话做这些事情......

问题

任何关于如何修改 Elasticsearch 或我的 rails 代码的指导将不胜感激。我在这里100%陷入泥潭。不确定我是否需要创建映射,使用 ES 做某事,或者完全在 rails 中做其他事情或如何做任何事情......简单地说,我如何将调用索引中的用户 ID 与存在的用户名相关联在用户索引中,以便我能够按名称搜索调用?我想无论需要什么索引方法都需要批量处理并与实时数据库保持同步......谢谢!

最佳答案

在过去一周敲定了一些事情之后,我相信我找到了解决方案。在将 Bulk API 上的大量 Elasticsearch 文档拼凑在一起后和 Rubydocs ,这是我在rails中提出的:

型号

# elasticsearch custom mapping
mappings :dynamic => 'true' do
indexes :dialer do
indexes :id, :type => 'long'
indexes :name, :type => 'string'
indexes :email, :type => 'string'
end
indexes :receiver do
indexes :id, :type => 'long'
indexes :name, :type => 'string'
indexes :email, :type => 'string'
end
end

# customize the index for for relationships
def as_indexed_json(options={})
as_json(
except: [:user1_id, :user2_id],
include: {
dialer: { only: [:id, :name, :email]},
receiver: { only: [:id, :name, :email]}
}
)
end

这给了我想要的输出,但我现在面临的问题是进口很慢,因为糖蜜......

This post很有帮助,尤其是“编写更快的导入”部分,但仍然像糖蜜一样慢。任何评论仍然会有所帮助,谢谢。我愿意接受任何和所有建议!

关于ruby-on-rails - 如何使用 Rails 将一个索引的 "foreign key"id 与 Elasticsearch 中另一个索引的相应数据相关联?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30602898/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com