Elasticsearch-dsl嵌套查询

5

我正在我的 Django 项目中使用 elasticsearch-dsl 库来索引数据并查询。

我有以下模型:

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

这是我如何在文档类型中包装我的模型:

这是我如何在文档类型中包装我的模型:

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

当我调用以下函数时,我的数据会被索引到 ElasticSearch 中:
def bulk_indexing():
    CommentsIndex.init(index='comments-index')
    es = Elasticsearch()
    bulk(client=es, actions=(b.indexing() for b in models.Comments.objects.all().iterator()))

我试图测试我是否能查询我的数据,方法是使用以下搜索功能:

我正在尝试测试是否可以查询我的数据,方法是使用以下搜索功能:

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

我无法查询嵌套对象,并尝试了许多不同的方法,但失败了。如何获得嵌套对象字段,例如user_post_id.score?

1个回答

7

类似这样的操作应该可以解决问题:

CommentsIndex.search().query('nested', path='user_post_id', query=Q('range', eser_post_id__score={'gt': 42}))

“gt” 用于什么?尽管它没有返回任何东西 - indexOutOfBounds
问题可能是由于我的外键设置方式不正确,导致数据没有正确索引。您能否也看一下我的另一个问题?https://stackoverflow.com/questions/50252992/error-when-indexing-data-using-elasticsearch-dsl - indexOutOfBounds

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接