How to query nested docs using DSL?

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 and need to returns the results that should equals Mainsearch.Id = tblreference.RefId.

what have you tried so far?

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

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

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 on it would be

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

{
  "query": {
    "nested": {
      "query": {
        "match": {
          "tblref.refId": {
            "query": "reference_id"
          }
        }
      },
      "path": "tblref"
    }
  }
}
{
_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('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:(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.

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