[英]Elasticsearch-dsl nested queries
我在我的 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
這就是我將模型包裝在 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()
當我調用以下函數時,我的數據被索引到彈性搜索中:
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}))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.