5

I am using the elasticsearch-dsl library in my Django project to index data and then query it back.

I have the following models:

class Comments(models.Model):

    comment_id = models.CharField(max_length=1000,blank=True,null=True)
    user_post_id = models.ForeignKey('UserPosts',null=True)
    score =  models.CharField(max_length=1000,blank=True,null=True)
    text = models.TextField(blank=True,null=True)
    creation_date = models.CharField(max_length=1000,blank=True,null=True)


    def __unicode__(self):
        return self.comment_id

    def indexing(self):


        obj = CommentsIndex(
            meta={'id': self.id},
            comment_id=self.comment_id,
            user_post_id=self.user_post_id,
            score=self.score,
            text=self.text,
            creation_date=self.creation_date,
        )
        obj.save(index='comments-index')
        return obj.to_dict(include_meta=True)


class UserPosts(models.Model):

    user_post_id = models.CharField(max_length = 1000 , blank = True , null = True)
    user_post_type_id = models.CharField(max_length = 1000 , blank = True , null = True)
    accepted_answer_id = models.CharField(max_length = 1000 , blank = True , null = True)
    creation_date = models.CharField(max_length=1000,blank = True , null = True)
    score = models.CharField(max_length = 1000 , blank = True , null = True)
    view_count = models.CharField(max_length = 1000 , blank = True , null = True)
    body = models.TextField( blank = True , null = True)
    last_editor_user_id = models.CharField(max_length = 1000 , blank = True , null = True)
    last_editor_display_name = models.CharField(max_length = 1000 , blank = True , null = True)
    last_edit_date = models.CharField(max_length = 1000 , blank = True , null = True)
    last_activity_date =models.CharField(max_length = 1000 , blank = True , null = True)
    title = models.CharField(max_length = 1000 , blank = True , null = True)
    tags = models.CharField(max_length = 1000 , blank = True , null = True)
    answer_count = models.CharField(max_length = 1000 , blank = True , null = True)
    comment_count = models.CharField(max_length = 1000 , blank = True , null = True)
    favorite_count = models.CharField(max_length = 1000 , blank = True , null = True)
    owner_user_id = models.ForeignKey(StackOverFlowUsers,null=True)
    parent_id = models.CharField(max_length = 1000 , blank = True , null = True)

    def __unicode__(self):

        return self.user_post_id

This is how I wrap my model in a doctype:

class UserPostsIndex(InnerDoc):
    user_post_id = Text()
    score = Text()



class CommentsIndex(DocType):
    comment_id = Text()
    user_post_id = Nested(UserPostsIndex)
    score = Text()
    text = Text()
    creation_date = Text()
 

When i call the following function, my data gets indexed into elastic search:

def bulk_indexing():
    CommentsIndex.init(index='comments-index')
    es = Elasticsearch()
    bulk(client=es, actions=(b.indexing() for b in models.Comments.objects.all().iterator()))

The way I am trying to test if i can query my data back is by using the search function which is as follow:

def search(text):
    s = Search(index="comments-index").filter("term",  score= text)
    response = s.execute()
    return response

I am unable to query the nested object and have tried a lot of different methods but failed. How can I get the nested object fields for example user_post_id.score?

1 Answer 1

7

something like this should work:

CommentsIndex.search().query('nested', path='user_post_id', query=Q('range', eser_post_id__score={'gt': 42}))
Sign up to request clarification or add additional context in comments.

2 Comments

What is "gt" used for? It does not return me anything though
The problem might be that the data has not been indexed correctly because the way my foreign keys were setup. Can you please have a look at my other question too? stackoverflow.com/questions/50252992/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.