弹性搜索中的多对多关系 [英] Many to many relationships in elasticsearch

查看:19
本文介绍了弹性搜索中的多对多关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道在elasticsearch中,我们可以在文档之间建立子/父关系.

I know that in elasticsearch, we can have child/parent relationships between documents.

然后,在索引时,我可以传递父 id,以便链接子文档和父文档:

And then, when indexing, I can pass the parent id so that the child and parent documents are linked:

$ curl -XPUT localhost:9200/blogs/blog_tag/1122?parent=1111 -d '{    "tag" : "something"}'

有没有办法在 elasticsearch 中建立多对多关系的模型?

Is there anyway to model a many to many relationship in elasticsearch?

数据驻留在具有以下架构的 MySQL 数据库中:

Data is resides in a MySQL database with the following schema:

account
========
id
name
some_property

group
========
id
name
description

account_group
=============
account_id
group_id
primary_group //This is 1 or 0 depending on whether the group is the primary group for that account.

这是目前我对 account 的映射(请原谅数组符号,我使用的是 Elastica 在 PHP 中与我的 elasticsearch 服务器通信):

This is currently my mapping for account (please excuse the array notation, I am using Elastica in PHP to talk to my elasticsearch server):

**Mapping for account**

'name' => array(
    'type' => 'string'),

'some_property' => array(
    'type' => 'string'),

'groups' => array(
   'properties' => array(
    'id'      => array('type' => 'integer'),
    'primary' => array('type' => 'boolean')
    )
),

**Mapping for group**

'name' => array(
        'type' => 'string'),

'description'=> array(
        'type' => 'string')

这种方法的问题在于,如果从索引中删除了一个组,我将需要遍历每个帐户并从每个帐户中删除组 ID.这对我来说似乎有点低效.我还认为在使用 elasticsearch 的子/父关系时这不会成为问题.

The problem with this approach is that if a group is deleted from the index, I will need to go through each account and delete the group id from each account. This seems to be a bit inefficient to me. I also presume that this would not be an issue when using elasticsearch's child/parent relationships.

无论如何可以在 elasticsearch 中对多对多关系进行建模吗?

Is there anyway to model many-to-many relationships in elasticsearch?

推荐答案

无法对多对多关系进行建模.

There's no way to model many-to-many relationships.

唯一的方法是将每个组的 id 存储在每个帐户中,就像我上面所做的那样.

The only way is to store the id of each group in each account like I have done so above.

Elasticsearch 非常高效,所以很多时候,重新索引是一个可以接受的解决方案.此外,elasticsearch 有文档的概念,不是关系存储系统,因此多对多关系可能永远不会实现.

Elasticsearch is pretty efficient, so often times, reindexing is an acceptable solution. Also, elasticsearch has the notion of documents and is not a relational storage system, so many-to-many relationships would probably never be implemented.

这篇关于弹性搜索中的多对多关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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