# How to query nested docs using DSL?

**URL:** <https://discuss.elastic.co/t/how-to-query-nested-docs-using-dsl/67053>\
**Category:** Elasticsearch\
**Created:** [November 24, 2016, 5:33am UTC](https://discuss.elastic.co/t/how-to-query-nested-docs-using-dsl/67053 "2016-11-24T05:33:57Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![madhan](https://avatars.discourse-cdn.com/v4/letter/m/aca169/32.png) [@madhan](https://discuss.elastic.co/u/madhan)\
**Post date:** [November 24, 2016, 5:33am UTC](https://discuss.elastic.co/t/how-to-query-nested-docs-using-dsl/67053/1 "2016-11-24T05:33:57Z")

</div>

HI,

public class Mainsearch  
{  
public int id{get;set;}  
public string name {get;set;}  
public list tblref{get;set;}  
}  
public class tblreference  
{  
public int Id{get;set;}  
public int RefId{get;set}  
public string Name {get;set;}  
}

I had created an index with above mentioned. i'm using querystring for search.

Now am searching for nested docs where i will pass querystring as Mainsearch.name='X', the tblreference.RefId contains list of docs... i need to get that tblreference.RefId's and pass to [Mainsearch.Id](http://Mainsearch.Id) and need to returns the results that should equals [Mainsearch.Id](http://Mainsearch.Id) = tblreference.RefId.

---

<div class="post-metadata">

**Author:** ![forloop](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/forloop/32/9021_2.png) [@forloop](https://discuss.elastic.co/u/forloop)\
**Post date:** [November 24, 2016, 7:15am UTC](https://discuss.elastic.co/t/how-to-query-nested-docs-using-dsl/67053/2 "2016-11-24T07:15:27Z")

</div>

what have you tried so far?

---

<div class="post-metadata">

**Author:** ![madhan](https://avatars.discourse-cdn.com/v4/letter/m/aca169/32.png) [@madhan](https://discuss.elastic.co/u/madhan)\
**Post date:** [November 24, 2016, 9:47am UTC](https://discuss.elastic.co/t/how-to-query-nested-docs-using-dsl/67053/3 "2016-11-24T09:47:53Z")

</div>

Thanks for ur response.

What i did is,

var response = client.Search(s =\> s  
.Index("Mainsearch")  
.From(from)  
.Size(size)  
.QueryString(qry));

so i get list of docs from that i'll get tblreference.RefId and pass that RefId to my main search.

but if RefId count is more than 1024, i get max\_clause count error...

how to fix this.. i'm new to elastic search and i dont know how to query nested doc..

---

<div class="post-metadata">

**Author:** ![forloop](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/forloop/32/9021_2.png) [@forloop](https://discuss.elastic.co/u/forloop)\
**Post date:** [November 24, 2016, 10:04am UTC](https://discuss.elastic.co/t/how-to-query-nested-docs-using-dsl/67053/4 "2016-11-24T10:04:13Z")

</div>

you can format code by highlighting it and pressing `</>` in the editor bar (or fencing it in 3 backticks at start and end).

> so i get list of docs from that i'll get tblreference.RefId and pass that RefId to my main search.

I'm not sure what you are trying to achieve; perhaps if you provide some more detail on the problem you're trying to solve, I may be able to help.

Assuming that your POCOs looks like the following

```auto
public class Mainsearch
{
    public int id { get; set; }
    public string name { get; set; }
    public List<tblreference> tblref { get; set; }
}

public class tblreference
{
    public int Id { get; set; }
    public int RefId { get; set; }
    public string Name { get; set; }
}

```

And assuming that tblreference has been mapped as a nested type, a [`nested` query](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-nested-query.html) on it would be

```auto
client.Search<Mainsearch>(s => s
    .Query(q => q
        .Nested(n => n
            .Path(p => p.tblref)
            .Query(nq => nq
                .Match(m => m
                    // The expression here is simply to get a reference to the `RefId` field
                    // on tblref types.
                    .Field(f => f.tblref.First().RefId)
                    .Query("reference_id")
                )
            )
        )
    )
);

```

which is the following query in json

```json
{
  "query": {
    "nested": {
      "query": {
        "match": {
          "tblref.refId": {
            "query": "reference_id"
          }
        }
      },
      "path": "tblref"
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![madhan](https://avatars.discourse-cdn.com/v4/letter/m/aca169/32.png) [@madhan](https://discuss.elastic.co/u/madhan)\
**Post date:** [November 24, 2016, 10:40am UTC](https://discuss.elastic.co/t/how-to-query-nested-docs-using-dsl/67053/5 "2016-11-24T10:40:31Z")

</div>

```
{
_index: idx
_type: MainSearch
_id: 1
_1
_source: {
id: 1
name: XXX
tblreference: [
{
Id: 1
RefId: 10
name: zzzz
}
{
Id: 1
RefId: 1001
name: yyyy
}
]
},
{
_index: idx
_type: MainSearch
_id: 2
_1
_source: {
id: 2
name: yyy
tblreference: [
{
Id: 10
RefId: 1
name: zzzz
}
{
Id: 10
RefId: 200
name: yyyy
}
]
}

```

This is my json data. Now i'm passing querystring as [MainSearch.name](http://MainSearch.name)('yyy')

so i'll get

```
{
_index: idx
_type: MainSearch
_id: 2
_1
_source: {
id: 2
name: yyy
tblreference: [
{
Id: 10
RefId: 1
name: zzzz
}
{
Id: 10
RefId: 200
name: yyyy
}
]
}

```

from this i'll get RefId (1,200) and finally i pass this id for search querysting as [MainSearch.Id](http://MainSearch.Id):(1,200).

my expecting result should be like this

```
{
_index: idx
_type: MainSearch
_id: 1
_1
_source: {
id: 1
name: XXX
tblreference: [
{
Id: 1
RefId: 10
name: zzzz
}
{
Id: 1
RefId: 1001
name: yyyy
}
]
}

```

..

I don't know whether i'm going in right way.. Is there any alternate to achieve this result.

---

<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:** [December 22, 2016, 10:40am UTC](https://discuss.elastic.co/t/how-to-query-nested-docs-using-dsl/67053/6 "2016-12-22T10:40:38Z")

</div>

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