Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nested objects/relations #2

Closed
dethi opened this issue Jun 18, 2015 · 7 comments
Closed

Nested objects/relations #2

dethi opened this issue Jun 18, 2015 · 7 comments
Milestone

Comments

@dethi
Copy link
Contributor

dethi commented Jun 18, 2015

No description provided.

@xiaolin
Copy link

xiaolin commented Apr 6, 2016

+1

@dethi dethi added this to the 2.0.0 milestone Jun 21, 2016
@brendan-rius
Copy link

+1

@brendan-rius
Copy link

Any ETA on this one?

@dethi
Copy link
Contributor Author

dethi commented Sep 25, 2016

Same as the 2.0.0 milestone.

@RML-Admin
Copy link

What is the due date for 2.0.0?

@oliver-zhou
Copy link

Is there a way to handle nested objects yet?

@PLNech
Copy link
Member

PLNech commented Aug 21, 2017

@xiaolin @brendan-rius @adavanisanti @oliver-zhou: Hi there and sorry for the late reply.

We don't have a current date for the 2.0.0 milestone as @dethi left the company. However, you can index relations by proxying an attribute in the index configuration:

Models

class Account(models.Model):
    username = models.CharField(max_length=40)
    service = models.CharField(max_length=40)

class Contact(models.Model):
    name = models.CharField(max_length=40)
    email = models.EmailField(max_length=60)
    //...
    accounts = models.ManyToManyField(Account)

    def account_names(self):
        return [str(account) for account in self.accounts.all()]

    def account_ids(self):
        return [account.id for account in self.accounts.all()]

Index

from algoliasearch_django import AlgoliaIndex


class ContactIndex(AlgoliaIndex):
    fields = ('name', 'email', 'company', 'address', 'city', 'county',
              'state', 'zip_code', 'phone', 'fax', 'web', 'followers', 'account_names', 'account_ids')

    settings = {
        'searchableAttributes': ['name', 'email', 'company', 'city', 'county', 'account_names',
        }

  • With this configuration, you can search for a Contact using its Account names:

image

  • You can use the associated account_ids at search-time to fetch more data from your model (you should only proxy the fields relevant for search to keep your records' size as small as possible)

@PLNech PLNech closed this as completed in 0f70028 Aug 21, 2017
side2k referenced this issue in brightnetwork/algoliasearch-django Feb 27, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants