# Not able to search on nested data

**URL:** <https://discuss.elastic.co/t/not-able-to-search-on-nested-data/143296>\
**Category:** Elasticsearch\
**Created:** [August 7, 2018, 9:35am UTC](https://discuss.elastic.co/t/not-able-to-search-on-nested-data/143296 "2018-08-07T09:35:34Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![shruthi1](https://avatars.discourse-cdn.com/v4/letter/s/ecd19e/32.png) [@shruthi1](https://discuss.elastic.co/u/shruthi1)\
**Post date:** [August 7, 2018, 9:35am UTC](https://discuss.elastic.co/t/not-able-to-search-on-nested-data/143296/1 "2018-08-07T09:35:34Z")

</div>

I am not able to search on nested data which is a list of addresses for contacts,  
Expected: If i type anything directly related to contacts(FirstName, LastName, CompanyName1 etc) i am getting search results  
If i type any of the text related to address, i expect the contact related to that address should show  
the **Addresses** here is list of addresses related to that contact.  
please suggest me for any workaround.

meta data from elastic search head:

```
index: "contacts",
type: "GetContactViewModel",
id: "63c6f419-c30a-4afc-b4e1-8cacc4bc0d65",
version: 1,
score: 1,
    source: {
    			"Xmin": 2965,
    			"Id": "63c6f419-c30a-4afc-b4e1-8cacc4bc0d65",
    			"Type": "Company",
    			"SalutationId": "a29dd803-419b-4330-b5cd-e316ac50cb6f",
    			"Description": "",
    			"FirstName": "Shruthi",
    			"LastName": "Py",
    			"MiddleName": "ma",
    			"Position": "",
    			"CompanyName1": "Trimble",
    			"CompanyName2": "",
    			"Reference1": "",
    			"Reference2": "",
    			"Analysis1": "",
    			"Analysis2": "",
    			"Inactive": false,
    			"Addresses": [
    								{
    								"Xmin": 2965,
    								"Id": "9783ed82-362c-44e4-a2bf-98b6d8c965d6",
    								"ContactId": "63c6f419-c30a-4afc-b4e1-8cacc4bc0d65",
    								"AddressTypeId": "82dda8a0-1dcf-44ce-853f-a81b88ffc410",
    								"Format": "GB",
    								"AddressLine1": "GANDHI",
    								"AddressLine2": "VIDHI",
    								"AddressLine3": "",
    								"AddressLine4": "",
    								"AddressLine5": "",
    								"AddressLine6": "",
    								"AddressLine7": "",
    								"AddressLine8": ""
    								}
    						   ],
    			"Connections": null,
    			"CategoryLinks": null
                }

```

**C# code for mapping**

```
public override Fields Fields
    {
        get
        {
            return Infer.Field<GetContactViewModel>(e => e.FirstName)
                .And(Infer.Field<GetContactViewModel>(e => e.LastName))
                .And(Infer.Field<GetContactViewModel>(e => e.Position))
                .And(Infer.Field<GetContactViewModel>(e => e.CompanyName1))
                .And(Infer.Field<GetContactViewModel>(e => e.CompanyName2))
                .And(Infer.Field<GetContactViewModel>(e => e.Reference1))
                .And(Infer.Field<GetContactViewModel>(e => e.Reference2))
                .And(Infer.Field<GetContactViewModel>(e => e.Analysis1))
                .And(Infer.Field<GetContactViewModel>(e => e.Analysis2))
                .And(Infer.Field<GetContactViewModel>(e => e.Addresses.First().AddressLine1))
                .And(Infer.Field<GetContactViewModel>(e => e.Addresses.First().AddressLine2))
                .And(Infer.Field<GetContactViewModel>(e => e.Addresses.First().AddressLine3))
                .And(Infer.Field<GetContactViewModel>(e => e.Addresses.First().AddressLine4))
                .And(Infer.Field<GetContactViewModel>(e => e.Addresses.First().AddressLine5))
                .And(Infer.Field<GetContactViewModel>(e => e.Addresses.First().AddressLine6))
                .And(Infer.Field<GetContactViewModel>(e => e.Addresses.First().AddressLine7))
                .And(Infer.Field<GetContactViewModel>(e => e.Addresses.First().AddressLine8))
                .And(Infer.Field<GetContactViewModel>(e => e.Connections.First().Data));
        }
    }

    protected override Func<MappingsDescriptor, IPromise<IMappings>> GetMappings()
    {
        return md => md
            .Map<GetContactViewModel>(m => m
                .AutoMap()
                .Properties(pd => pd
                    .Keyword(kpd => kpd.Name(e => e.Id).Index(false))
                    .Keyword(kpd => kpd.Name(e => e.SalutationId).Index(false))
                    .Number(kpd => kpd.Name(e => e.Xmin).Type(NumberType.Integer).Index(false))
                    .Boolean(kpd => kpd.Name(e => e.Inactive).Index(false))
                    .Keyword(kpd => kpd.Name(e => e.Type).Index(false))
                    .Text(kpd => kpd.Name(e => e.Description).Index(false))

                    .Nested<GetCategoryLinkViewModel>(n => n
                        .Name(c => c.CategoryLinks)
                        .AutoMap()
                        .Properties(npd => npd
                            .Keyword(kpd => kpd.Name(cl => cl.Id).Index(false))
                            .Keyword(kpd => kpd.Name(cl => cl.ContactId).Index(false))
                            .Keyword(kpd => kpd.Name(cl => cl.CategoryId).Index(false))
                        )
                    )

                    .Nested<GetAddressViewModel>(n => n
                        .Name(c => c.Addresses)
                        .AutoMap()
                        .Properties(npd => npd
                            .Number(kpd => kpd.Name(a => a.Xmin).Type(NumberType.Integer).Index(false))
                            .Keyword(kpd => kpd.Name(a => a.Id).Index(false))
                            .Keyword(kpd => kpd.Name(a => a.AddressTypeId).Index(false))
                            .Keyword(kpd => kpd.Name(a => a.ContactId).Index(false))
                            .Keyword(kpd => kpd.Name(a => a.Format).Index(false))
                        )
                    )

                    .Nested<GetConnectionViewModel>(n => n
                        .Name(c => c.Connections)
                        .AutoMap()
                        .Properties(npd => npd
                            .Number(kpd => kpd.Name(c => c.Xmin).Type(NumberType.Integer).Index(false))
                            .Keyword(kpd => kpd.Name(c => c.Id).Index(false))
                            .Keyword(kpd => kpd.Name(c => c.ConnectionTypeId).Index(false))
                            .Keyword(kpd => kpd.Name(c => c.ContactId).Index(false))
                            .Text(kpd => kpd.Name(c => c.Code1).Index(false))
                            .Text(kpd => kpd.Name(c => c.Code2).Index(false))
                        )
                    )

                )
            );
    }
```

---

<div class="post-metadata">

**Author:** ![paulkeogh](https://avatars.discourse-cdn.com/v4/letter/p/8dc957/32.png) [@paulkeogh](https://discuss.elastic.co/u/paulkeogh)\
**Post date:** [August 7, 2018, 10:40am UTC](https://discuss.elastic.co/t/not-able-to-search-on-nested-data/143296/2 "2018-08-07T10:40:23Z")

</div>

Have you mapped your data as described here ?

[https://www.elastic.co/guide/en/elasticsearch/reference/current/nested.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/nested.html)

If so, you should be able to search using a nested query as described here;

[https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-nested-query.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-nested-query.html)

---

<div class="post-metadata">

**Author:** ![shruthi1](https://avatars.discourse-cdn.com/v4/letter/s/ecd19e/32.png) [@shruthi1](https://discuss.elastic.co/u/shruthi1)\
**Post date:** [August 7, 2018, 12:49pm UTC](https://discuss.elastic.co/t/not-able-to-search-on-nested-data/143296/3 "2018-08-07T12:49:34Z")

</div>

yes i have added nested type , IncludeInParent = true for nested objects seems solving the issue.  
Thankyou

---

<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:** [September 4, 2018, 12:59pm UTC](https://discuss.elastic.co/t/not-able-to-search-on-nested-data/143296/4 "2018-09-04T12:59:20Z")

</div>

Closed on September 4, 2018, 12:59pm UTC

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
