json 如何使用弹性搜索搜索嵌套对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8140651/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 18:03:27  来源:igfitidea点击:

How to search nested objects with elastic-search

jsonelasticsearchnested

提问by swatkins

OK, I've not been able to figure this out thus far. Hoping someone can offer some insight.

好的,到目前为止我还没有弄清楚这一点。希望有人能提供一些见解。

Given the documents below, how would I search for all documents with a video that has "test" in the video title? I'm using the HTTP API. (Basically, how do you search nested objects with elastic search? I know there has to be docs out there, but I haven't really been able to find any.)

鉴于以下文件,我将如何搜索视频标题中带有“测试”的视频的所有文件?我正在使用 HTTP API。 (基本上,您如何使用弹性搜索搜索嵌套对象?我知道那里必须有文档,但我真的找不到任何文档。)

[{
    id:4635,
    description:"This is a test description",
    author:"John",
    author_id:51421,
    video: {
        title:"This is a test title for a video",
        description:"This is my video description",
        url:"/url_of_video"
    }
},
{
    id:4636,
    description:"This is a test description 2",
    author:"John",
    author_id:51421,
    video: {
        title:"This is an example title for a video",
        description:"This is my video description2",
        url:"/url_of_video2"
    }
},
{
    id:4637,
    description:"This is a test description3",
    author:"John",
    author_id:51421,
    video: {
        title:"This is a test title for a video3",
        description:"This is my video description3",
        url:"/url_of_video3"
    }
}]

采纳答案by swatkins

OK, I finally found these pages (should have taken more time with the docs beforehand) and it seems we set the property that holds the video to type:nested, then use nested queries.

好的,我终于找到了这些页面(应该事先花更多时间看文档),看来我们将保存视频的属性设置为 type:nested,然后使用嵌套查询。

http://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-nested-query.html

http://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-nested-query.html

http://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-nested-filter.html

http://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-nested-filter.html

Hope this helps someone down the road.

希望这可以帮助某人在路上。

回答by dira

You don't necessarily need to nest video; you can map it as a normal field. Which means it will store

您不一定需要嵌套视频;您可以将其映射为普通字段。这意味着它将存储

'video:title': "This is a test title for a video3",
'video:description':"This is my video description3",
'video:url':"/url_of_video3"

And you can search for video.title:'test'.

您可以搜索video.title:'test'.

As far as I get it, nested fields are useful when you have multiple nested items, and you want to make a query only for the nested items. For example, having this data

据我所知,当您有多个嵌套项并且只想对嵌套项进行查询时,嵌套字段很有用。例如,有这个数据

[{
    id:4635,
    description:"This is a test description",
    author:"John",
    author_id:51421,
    video: [
      {
        title:"This is a test title for a video",
        description:"This is my video description",
        url:"/url_of_video"
      },
      {
        title:"This is an example title for a video",
        description:"This is my video description2",
        url:"/url_of_video2"
      }
    ]
},
{
    id:4637,
    description:"This is a test description3",
    author:"John",
    author_id:51421,
    video: [
      {
        title:"This is a test title for a video3",
        description:"This is my video description3",
        url:"/url_of_video3"
      }
    ]
}]

If you would search for video.title: 'test' and video.description: 'description2', and video was not nested, it will give you a fake result (because testis in the first video and description2in the second, but in all the video field you have both).

如果您要搜索video.title: 'test' and video.description: 'description2', 并且视频没有嵌套,它会给您一个假的结果(因为test在第一个视频和description2第二个视频中,但在所有视频字段中您都有)。

In this case, if you map video as nested, it will remember each video as a separate entity and will search for individual videos that fit those conditions, so for video.title: 'test' and video.description: 'description2'it will return nothing, for video.title: 'example' and video.description: 'description2'it will return one result.

在这种情况下,如果您将视频映射为嵌套,它会将每个视频作为一个单独的实体进行记忆,并将搜索符合这些条件的单个视频,因此video.title: 'test' and video.description: 'description2'它不会返回任何内容,因为video.title: 'example' and video.description: 'description2'它将返回一个结果。

回答by Dinesh Kumar Sahu

If you want to put it in Rest API URL format

如果你想把它放在 Rest API URL 格式

/_search?pretty&q=video.title:*test*

/_search?pretty&q=video.title:*test*

回答by tsorn

You can use the .keywordsuffix if the name of the nested object is unique:

.keyword如果嵌套对象的名称是唯一的,则可以使用后缀:

{
        'query': {
            'term': {
                'title.keyword': "This is a test title for a video"               
            }
        }
}

Which should match your first example entry. Note that the videoobject name is not specified anywhere; this matches on all objects that have a titlesub-object.

这应该与您的第一个示例条目匹配。请注意,video没有在任何地方指定对象名称;这匹配所有具有title子对象的对象。