【问题标题】:Return the nested object corresponding to the copy_to field in elasticsearch返回elasticsearch中copy_to字段对应的嵌套对象
【发布时间】:2020-03-02 23:25:05
【问题描述】:

这是我的索引的结构。一个事件有许多嵌套对象:文档、站点和人员。

一些嵌套对象上的一些字段是copy_to到根的。

Events
|--- persons—names
|--- other fields
|
|———— Documents
          --------- other fields
|
|———— Sites
          ---------- other fields
|
|———— Persons
           -----------name (copied to the root)
           ------------- id

现在假设我正在执行查询字符串搜索。

如果返回一个文档,因为它与 person_names 匹配,我只想返回关联对象的 inner_hit,在这种情况下是“persons”。请注意,文档中可能有多个人,我需要准确返回匹配的人,所以这可能是一个问题,因为 copy_to 是一个不指向其关联嵌套对象的数组。

当然,如果结果匹配是因为从“站点”或“文档”复制的字段,我当然希望发生同样的事情。

这样,我将能够创建多对象搜索 --> 我的意思是我将返回事件、文档、站点或人员,具体取决于与查询匹配的内容。

我该怎么做?随意将我重定向到另一种方法来解决我的多对象搜索问题。

【问题讨论】:

    标签: elasticsearch


    【解决方案1】:

    使用Explain API 可能是一个好方法,如下所述: https://discuss.elastic.co/t/best-way-to-return-which-field-matched/4140/3

    所以解决办法是:

    • 进行 query_string 查询
    • 对于每个结果:
      • 对它们使用解释 API(使用它们的 ID)
      • 分析解释 API 的结果,并尝试查找结果是否因人员、站点或文档字段而匹配。
      • 只返回对应的嵌套对象。

    不过这看起来真的很复杂。

    另一种选择是在我的索引中为每个嵌套对象添加一个新文档。这看起来像这样:

    Events
    |
    |--- other fields
    |
    |———— Documents
    |         --------- other fields
    |
    |———— Sites
    |          ---------- other fields
    |
    |———— Persons
               -----------name (NOT copied to the root)
               ------------- id
    
    ### And on the same index, you have other documents for each nested objects
    
    Persons
    -----------name (NOT copied to the root)
    ------------- id
    Sites
    ---------- other fields
    
    Documents
    --------- other fields
    
    

    那么query_string就直接了。

    最后一种选择是为这些对象的每种类型使用一个索引并同时查询所有这些索引。

    【讨论】:

    • 谁有更好的选择?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多