Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to search nested objects with Elasticsearch

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.)

[{     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"     } }] 
like image 385
swatkins Avatar asked Nov 15 '11 17:11

swatkins


People also ask

How do you query a nested field?

You can search nested fields using dot notation that includes the complete path, such as obj1.name . Multi-level nesting is automatically supported, and detected, resulting in an inner nested query to automatically match the relevant nesting level, rather than root, if it exists within another nested query.

What is nested in elastic search?

The nested type is a specialised version of the object data type that allows arrays of objects to be indexed in a way that they can be queried independently of each other.

How do you search in Elasticsearch?

You can use the search API to search and aggregate data stored in Elasticsearch data streams or indices. The API's query request body parameter accepts queries written in Query DSL. The following request searches my-index-000001 using a match query. This query matches documents with a user.id value of kimchy .

What is a nested field?

When a packed class contains an instance field that is a packed type, the data for that field is packed directly into the containing class. The field is known as a nested field .


1 Answers

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'.

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 test is in the first video and description2 in the second, but in all the video field you have both).

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.

like image 85
dira Avatar answered Sep 28 '22 04:09

dira