Coder Social home page Coder Social logo

Comments (3)

sabricot avatar sabricot commented on June 18, 2024

hello,

I am not sure to understand your problem but if your goal is to index in es your Item Model with its tags the simplest solution is to store the tags as elastisearch object which lead to something like this.

models.py

class Tag(models.Model):
    name = models.CharField("Name", max_length=5000, blank=True)
    taglevel = models.IntegerField("Tag level", null=True, blank=True)

class Item(models.Model):
    title = models.CharField("Title", max_length=10000, blank=True)
    tag = models.ManyToManyField('Tag', blank=True)

    def tags(self):
        return self.tag.all()

documents.py

item = Index('test_item')
item.settings(
    number_of_shards=1,
    number_of_replicas=0
)

@item.doc_type
class ItemDocument(DocType):
    tags = fields.ObjectField(properties={
        'name': fields.StringField(),
        'taglevel': fields.IntegerField(),
    })

    class Meta:
        model = Item
        fields = [
            'title',
        ]

With this, your items will be indexed at each save on the django Item model.
And for example you can query elasticsearch like this:

results = ItemDocument.search().filter('term', **{'tags.taglevel': 4}

for doc in results:
    print(doc.title, doc.tags)

To get only items that have a tag with a taglevel of 4.

from django-elasticsearch-dsl.

yarnball avatar yarnball commented on June 18, 2024

Thanks but I'm not following how to implement that code.

I created a file called "documents.py" and added import models to it.

But I do not know where the following belongs:

results = ItemDocument.search().filter('term', **{'tags.taglevel': 4}

for doc in results:
    print(doc.title, doc.tags)

Can you please advise? Also- I want it to return all levels of tags

from django-elasticsearch-dsl.

yarnball avatar yarnball commented on June 18, 2024

I found the solution- it was to use signals.

m2m_changed.connect(update_search, sender=Item.tag.through)

from django-elasticsearch-dsl.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.