首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Elasticsearch-dsl嵌套查询

Elasticsearch-dsl嵌套查询
EN

Stack Overflow用户
提问于 2018-05-08 06:47:10
回答 1查看 5.8K关注 0票数 2

我在我的Django项目中使用elasticsearch-dsl库来索引数据,然后再对其进行查询。

我有以下模型:

代码语言:javascript
复制
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

这就是我在doctype中包装模型的方式:

代码语言:javascript
复制
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()

当我调用以下函数时,我的数据会被索引到elastic search中:

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

我尝试通过使用搜索功能来测试是否可以查询回我的数据,如下所示:

代码语言:javascript
复制
def search(text):
    s = Search(index="comments-index").filter("term",  score= text)
    response = s.execute()
    return response

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

EN

回答 1

Stack Overflow用户

发布于 2018-05-08 20:44:41

像这样的东西应该是有效的:

代码语言:javascript
复制
CommentsIndex.search().query('nested', path='user_post_id', query=Q('range', eser_post_id__score={'gt': 42}))
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50223488

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档