【问题标题】:How to search nested Doc in Elasticsearch_dsl如何在 Elasticsearch_dsl 中搜索嵌套文档
【发布时间】:2021-06-08 18:45:49
【问题描述】:

我有多个索引,称为 Post with Nested document Comment。

这就是我的文档的定义方式

class CommentDoc(InnerDoc):
    title = Text(analyzer=ngram_analyzer)
    content = Text(analyzer=ngram_analyzer)
class PostDoc(Document):
    content = Text(analyzer=ngram_analyzer)
    comments = Nested(Icd10Doc)
    id = Integer()

这就是我当前的搜索方式

s = Search(index=['post', 'blog', 'artical'])
q = Q("multi_match", query="whatever", fields=['name', 'title', 'content'])
s = s.query(q)

那么如何使用 cmets 中的嵌套 Docs 搜索 Post 索引?

我应该在字段数组中添加什么?

【问题讨论】:

    标签: elasticsearch-dsl elasticsearch-dsl-py


    【解决方案1】:

    起初我认为你应该编辑你的模型,但我不确定?:

    comments = Nested(Icd10Doc) ---> comments = Nested(CommentDoc)
    

    我已经在类似的条件下测试过这个查询,我认为它对你有用?

    nested_q = Q("nested", path='comments', query=Q("multi_match", query="whatever", fields=['comments.content']))
    

    最后,如果你想组合查询,你可以使用这个:

    final_q = nested_q & other_q (for AND operation)
    
    final_q = nested_q | other_q (for OR operation)
    

    最后:

    s = s.query(final_q)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-05-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-04
      • 2020-11-04
      • 1970-01-01
      • 2015-12-07
      相关资源
      最近更新 更多