Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Many to many relationships in elasticsearch

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

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"}'

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

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.

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')

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.

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

like image 940
F21 Avatar asked Aug 06 '12 06:08

F21


People also ask

What is many to many relationship in Entity Framework?

Entity Framework Many-to-Many Relationships. Many-to-Many Relationships. In a many-to-many relationship, each row of data in one table is linked to many rows in the second table and vice versa. A many-to-many relationship occurs when multiple records in one table are associated with multiple records in another table.

How do you represent relational data in Elasticsearch?

How do you represent relational data in Elasticsearch? There are a few mechanisms that can be used to provide relation support. Each has their pros and cons, making them useful for different situations. The simplest mechanism are named "inner objects". These are JSON objects embedded inside your parent document: Simple, right?

How is the many-to-many relationship represented in the database?

The many-to-many relationship in the database is represented by a joining table which includes the foreign keys of both tables. Also, these foreign keys are composite primary keys.

How do you convert many-to-many relationships to one to many relationships?

For efficient processing, you can convert the many-to-many relationship tables into two one-to-many relationships by connecting these two tables with an intersection table that contains the keys of both tables. Our data modeling tool DeZign for Databases, automatically resolves many-to-many relationships.


1 Answers

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

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

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.

like image 125
F21 Avatar answered Sep 30 '22 18:09

F21