是否可以在ElasticSearch中分类嵌套文档? [英] Is it possible to sort nested documents in ElasticSearch?

查看:79
本文介绍了是否可以在ElasticSearch中分类嵌套文档?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我说我有以下映射:

 site:{
properties:{
title:{type:string},
description:{type:string},
category:{type:string },
tags:{type:array},
point:{type:geo_point}
localities:{
类型:'嵌套',
属性:{
title:{type:string},
description:{type:string},
point:{type:geo_point}
}
}
}
}
pre>

然后我在父文档上进行_geo_distance排序,并且可以对site.point上的文档进行排序。然而,我也希望嵌套的地方按照_geo_distance在父文档中排序。



这可能吗?如果是这样,那么怎么样?

解决方案

不幸的是,(至少还没有)



ElasticSearch中的查询只会识别与查询匹配的文档以及它们的匹配情况。



要了解嵌套文档是有用的,请考虑以下示例:

  {
title:我的帖子,
body:我的身体中的文字...,
followers:[
{
name:Joe,
status:active
},
{
name:Mary,
status :待定
},
]
}

上述JSON,一旦在ES中索引,在功能上等同于以下。请注意追踪者字段已被平铺:

  {
title:我的帖子,
body:我的身体中的文字...,
followers.name:[Joe,Mary],
followers.status:[active,pending]
}

搜索:跟状态==活动和名称==玛丽将匹配此文档...错误。



嵌套字段允许我们解决此限制。如果追踪者字段被声明为类型为嵌套而不是类型对象然后其内容在内部被创建为一个单独的(不可见)子文档。这意味着我们可以使用嵌套查询嵌套过滤器以单独的文档查询这些嵌套文档。



但是,嵌套查询/过滤器子句的输出仅告诉我们主文档是否匹配,以及它匹配的情况。它甚至没有告诉我们哪个嵌套文档匹配。为了解决这个问题,我们必须在我们的应用程序中编写代码,以便根据我们的搜索条件检查每个嵌套文档。



有几个打开问题请求添加这些功能,但这不是一个容易的问题解决。



实现所需要的唯一方法是将您的子文档作为单独的文档进行索引,并独立查询和排序。在主文档和这些单独的子文档之间建立父子关系可能是有用的。 (请参阅父类型映射 index api docs ,而 top-children has-child 查询。



此外,ES用户已经将列表邮寄到新的 has_parent 过滤器,他们目前正在一个 fork 。但是,这在主要的ES备份还不可用。


Lets say I have the following mapping:

"site": {
  "properties": {
    "title":       { "type": "string" },
    "description": { "type": "string" },
    "category":    { "type": "string" },
    "tags":        { "type": "array" },
    "point":       { "type": "geo_point" }
    "localities":  { 
      type: 'nested',
      properties: {
        "title":       { "type": "string" },
        "description": { "type": "string" },
        "point":       { "type": "geo_point" }
      }
    }
  }
}

I'm then doing an "_geo_distance" sort on the parent document and am able to sort the documents on "site.point". However I would also like the nested localities to be sorted by "_geo_distance", inside the parent document.

Is this possible? If so, how?

解决方案

Unfortunately, no (at least not yet).

A query in ElasticSearch just identifies which documents match the query, and how well they match.

To understand what nested documents are useful for, consider this example:

{
    "title":    "My post",
    "body":     "Text in my body...",
    "followers": [
        {
            "name":     "Joe",
            "status":   "active"
        },
        {
            "name":     "Mary",
            "status":   "pending"
        },
    ]
}        

The above JSON, once indexed in ES, is functionally equivalent to the following. Note how the followers field has been flattened:

{
    "title":            "My post",
    "body":             "Text in my body...",
    "followers.name":   ["Joe","Mary"],
    "followers.status": ["active","pending"]
}        

A search for: followers with status == active and name == Mary would match this document... incorrectly.

Nested fields allow us to work around this limitation. If the followers field is declared to be of type nested instead of type object then its contents are created as a separate (invisible) sub-document internally. That means that we can use a nested query or nested filter to query these nested documents as individual docs.

However, the output from the nested query/filter clauses only tells us if the main doc matches, and how well it matches. It doesn't even tell us which of the nested docs matched. To figure that out, we'd have to write code in our application to check each of the nested docs against our search criteria.

There are a few open issues requesting the addition of these features, but it is not an easy problem to solve.

The only way to achieve what you want is to index your sub-docs as separate documents, and to query and sort them independently. It may be useful to establish a parent-child relationship between the main doc and these separate sub-docs. (see parent-type mapping, the Parent & Child section of the index api docs, and the top-children and has-child queries.

Also, an ES user has mailed the list about a new has_parent filter that they are currently working on in a fork. However, this is not available in the main ES repo yet.

这篇关于是否可以在ElasticSearch中分类嵌套文档?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆