Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you convert a Django model with ManytoMany field to an elasticsearch_dsl DocType class?

I'm using elasticsearch_dsl to help with interfacing with elasticsearch and this is the model I currently have and I'm trying to recreate as a DocType:

class HouseIndex(DocType):
    house_type = String()
    #people
    sold = Boolean()
    built_datetime = Date()
    #alerts
    # associated_locations
    hash = String()


class House(models.Model):
    house_type = models.ForeignKey(HouseType, db_index=True,
                                   on_delete=models.CASCADE)
    people = models.ManyToManyField(to='Person', db_index=True,
                                      through='PersonToHouseMap')
    sold = models.BooleanField(default=False)
    built_datetime = models.DateTimeField()
    alerts = models.ManyToManyField(Alert)
    associated_locations = models.ManyToManyField(to='Location')
    hash = models.CharField(max_length=64, unique=True, null=True)
    objects = HouseManager()

But I'm not sure what to do when it's a ManyToMany field. Such as with people, alerts, and associated locations.

like image 494
Stupid.Fat.Cat Avatar asked Nov 30 '25 08:11

Stupid.Fat.Cat


1 Answers

It really depends on what you wish to query for, but I'd probably use a Nested field containing a list of the Location objects. You can see something similar in my example repo here - https://github.com/HonzaKral/es-django-example/blob/master/qa/models.py#L48

like image 114
Honza Král Avatar answered Dec 02 '25 03:12

Honza Král



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!