gpt4 book ai didi

python - Elasticsearch-dsl 嵌套查询

转载 作者:太空宇宙 更新时间:2023-11-04 02:26:37 25 4
gpt4 key购买 nike

我在我的 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?

最佳答案

像这样的东西应该可以工作:

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

关于python - Elasticsearch-dsl 嵌套查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50223488/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com