# Search in sub attributes

**URL:** <https://discuss.elastic.co/t/search-in-sub-attributes/13708>\
**Category:** Elasticsearch\
**Created:** [September 20, 2013, 5:30pm UTC](https://discuss.elastic.co/t/search-in-sub-attributes/13708 "2013-09-20T17:30:01Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alexey\_Sidelnikov](https://avatars.discourse-cdn.com/v4/letter/a/c5a1d2/32.png) [@Alexey\_Sidelnikov](https://discuss.elastic.co/u/Alexey_Sidelnikov)\
**Post date:** [September 20, 2013, 5:30pm UTC](https://discuss.elastic.co/t/search-in-sub-attributes/13708/1 "2013-09-20T17:30:01Z")

</div>

Hi,

Suppose I have document like:

{  
"email": {  
"to": [  
{  
"name": {  
"first\_name": "Jonh",  
"last\_name": "Johnson"  
}  
},  
{  
"name": {  
"first\_name": "Mark",  
"last\_name": "Smith"  
}  
}  
],  
"message": "This is a tweet!"  
}  
}

I'm trying to search for mails to "John Smith". If I will just search like:  
{  
"bool": {  
"must": [  
{  
"name.first\_name": {  
"user": "Jonh"  
}  
},  
{  
"name.second\_name": {  
"user": "Smith"  
}  
}  
]  
}  
}

I'm getting document I've described - it have somebody with first name "John" as recipient and somebody whos second name is Smith. But it is not correct, because I want to get mails to "John Smith" really.

Very silly question, but I can't understand how to solve it

---

<div class="post-metadata">

**Author:** ![polyfractal](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/polyfractal/32/48162_2.png) [@polyfractal](https://discuss.elastic.co/u/polyfractal)\
**Post date:** [September 20, 2013, 8:50pm UTC](https://discuss.elastic.co/t/search-in-sub-attributes/13708/2 "2013-09-20T20:50:59Z")

</div>

You'll need to use Nested documents or Parent/Child. Inner objects like  
you have do not maintain relations between elements. The fields just  
become bags of values:

{  
"name.first\_name" : ["John", "Mark"],  
"name.last\_name" : ["Johnson", "Smith",  
}

Check out this article on document relations in elasticsearch[http://www.elasticsearch.org/blog/managing-relations-inside-elasticsearch/](http://www.elasticsearch.org/blog/managing-relations-inside-elasticsearch/),  
it should clear up help understand how everything is working.

-Zach

On Friday, September 20, 2013 1:30:02 PM UTC-4, Alexey Sidelnikov wrote:

> Hi,
> 
> Suppose I have document like:
> 
> {  
> "email": {  
> "to": [  
> {  
> "name": {  
> "first\_name": "Jonh",  
> "last\_name": "Johnson"  
> }  
> },  
> {  
> "name": {  
> "first\_name": "Mark",  
> "last\_name": "Smith"  
> }  
> }  
> ],  
> "message": "This is a tweet!"  
> }  
> }
> 
> I'm trying to search for mails to "John Smith". If I will just search  
> like:  
> {  
> "bool": {  
> "must": [  
> {  
> "name.first\_name": {  
> "user": "Jonh"  
> }  
> },  
> {  
> "name.second\_name": {  
> "user": "Smith"  
> }  
> }  
> ]  
> }  
> }
> 
> I'm getting document I've described - it have somebody with first name  
> "John" as recipient and somebody whos second name is Smith. But it is not  
> correct, because I want to get mails to "John Smith" really.
> 
> Very silly question, but I can't understand how to solve it
> 
> --  
> View this message in context:  
> [http://elasticsearch-users.115913.n3.nabble.com/Search-in-sub-attributes-tp4041622.html](http://elasticsearch-users.115913.n3.nabble.com/Search-in-sub-attributes-tp4041622.html)  
> Sent from the Elasticsearch Users mailing list archive at [Nabble.com](http://Nabble.com).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 6, 2017, 2:15am UTC](https://discuss.elastic.co/t/search-in-sub-attributes/13708/3 "2017-07-06T02:15:19Z")

</div>

Closed on July 6, 2017, 2:15am UTC


