Lets first create the mapping, here in this tutorial we are using the example of book document which will contain he nested author field.
PUT /books/
{
"mappings": {
"book":{
"properties": {
"id":{
"type": "integer"
},
"title":{
"type": "string"
},
"categories":{
"type": "integer"
},
"tag":{
"type": "string"
},
"author":{
"type": "nested",
"properties": {
"firstname":{
"type": "string"
},
"lastname":{
"type": "string"
},
"id":{
"type": "integer"
}
}
}
}
}
}
}
Here the type of author is nested which means it acts as object.
Note:: "nested" is more advanced form of "object" which allow us to put objects in the arrays.
Lets populate some data
PUT /books/book/101
{
"title" : "An article title",
"categories" : [1,3,5,7],
"tag" : ["elasticsearch", "symfony", "Obtao"],
"author" : [
{
"firstname" : "Francois",
"surname": "francoisg",
"id" : 18
},
{
"firstname" : "Gregory",
"surname" : "gregquat",
"id" : "2"
}
]
}
PUT /books/book/102
{
"title" : "Elasticsearch",
"categories" : [1,3,5,7],
"tag" : ["elasticsearch", "bigdate", "Text search"],
"author" : [
{
"firstname" : "yubraj",
"surname": "pokharel",
"id" : 18
},
{
"firstname" : "dharma",
"surname" : "kshetri",
"id" : "2"
}
]
}
Now lets say we need those books whose author name is yubraj pokharel ( thats me ;) ) so what we do now umm lets try some queries
GET /books/book/_search
{
"query": {
"filtered": {
"query": {
"match_all": {}
},
"filter": {
"nested" : {
"path" : "author",
"filter":{
"bool": {
"must": [
{
"term": {
"author.firstname": "yubraj"
}
},
{
"term": {
"author.surname": "pokharel"
}
}
]
}
}
}
}
}
}
}
so the output will be
{
"took": 29,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 1,
"hits": [
{
"_index": "books",
"_type": "book",
"_id": "102",
"_score": 1,
"_source": {
"title": "Elasticsearch",
"categories": [
1,
3,
5,
7
],
"tag": [
"elasticsearch",
"bigdate",
"Text search"
],
"author": [
{
"firstname": "yubraj",
"surname": "pokharel",
"id": 18
},
{
"firstname": "dharma",
"surname": "kshetri",
"id": "2"
}
]
}
}
]
}
}
OR you can also use the query like this
GET /books/book/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"title": "elasticsearch"
}
},
{
"nested": {
"path": "author",
"query": {
"bool": {
"must": [
{
"term": {"author.firstname": {"value": "yubraj"}}
},
{
"term": {"author.surname": {"value": "pokharel"}}
}
]
}
}
}
}
]
}
}
}
PUT /books/
{
"mappings": {
"book":{
"properties": {
"id":{
"type": "integer"
},
"title":{
"type": "string"
},
"categories":{
"type": "integer"
},
"tag":{
"type": "string"
},
"author":{
"type": "nested",
"properties": {
"firstname":{
"type": "string"
},
"lastname":{
"type": "string"
},
"id":{
"type": "integer"
}
}
}
}
}
}
}
Here the type of author is nested which means it acts as object.
Note:: "nested" is more advanced form of "object" which allow us to put objects in the arrays.
Lets populate some data
PUT /books/book/101
{
"title" : "An article title",
"categories" : [1,3,5,7],
"tag" : ["elasticsearch", "symfony", "Obtao"],
"author" : [
{
"firstname" : "Francois",
"surname": "francoisg",
"id" : 18
},
{
"firstname" : "Gregory",
"surname" : "gregquat",
"id" : "2"
}
]
}
PUT /books/book/102
{
"title" : "Elasticsearch",
"categories" : [1,3,5,7],
"tag" : ["elasticsearch", "bigdate", "Text search"],
"author" : [
{
"firstname" : "yubraj",
"surname": "pokharel",
"id" : 18
},
{
"firstname" : "dharma",
"surname" : "kshetri",
"id" : "2"
}
]
}
Now lets say we need those books whose author name is yubraj pokharel ( thats me ;) ) so what we do now umm lets try some queries
GET /books/book/_search
{
"query": {
"filtered": {
"query": {
"match_all": {}
},
"filter": {
"nested" : {
"path" : "author",
"filter":{
"bool": {
"must": [
{
"term": {
"author.firstname": "yubraj"
}
},
{
"term": {
"author.surname": "pokharel"
}
}
]
}
}
}
}
}
}
}
so the output will be
{
"took": 29,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 1,
"hits": [
{
"_index": "books",
"_type": "book",
"_id": "102",
"_score": 1,
"_source": {
"title": "Elasticsearch",
"categories": [
1,
3,
5,
7
],
"tag": [
"elasticsearch",
"bigdate",
"Text search"
],
"author": [
{
"firstname": "yubraj",
"surname": "pokharel",
"id": 18
},
{
"firstname": "dharma",
"surname": "kshetri",
"id": "2"
}
]
}
}
]
}
}
OR you can also use the query like this
GET /books/book/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"title": "elasticsearch"
}
},
{
"nested": {
"path": "author",
"query": {
"bool": {
"must": [
{
"term": {"author.firstname": {"value": "yubraj"}}
},
{
"term": {"author.surname": {"value": "pokharel"}}
}
]
}
}
}
}
]
}
}
}
thank you for the information provided, we are waiting for the next info
ReplyDelete