search querystring for combining parent and child docs

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

search querystring for combining parent and child docs

Ganesan Marappan
Hi Friends

I'm new in Elastic Search. i'm developing asp.net(c#) search application using ES.

i have used two tables. First one is Parent table and another one is Child Table. i can able to retrieve parent records using HasChildQuery. what my question is , i want to combine the parent and child table queries in single search query.

Ex:

Parent Table : [parent_type]

PC1  | PC2 | PC3  | PC4
------------------------
R1      A      B         C
R2      AA    BB        CC
R3      AAA  BBB      CCC


Child Table : [child_type]

PC1  | CC1 | CC2  | CC3
------------------------
R1      X      Y         Z
R1      XX    YY        ZZ
R2      XXX  YYY      ZZZ


I need the query string like this.  

( PC1:"R1" AND [child_type].CC1:"XX" )

Please provide the querystring for the above.

Note: Earlier i have used Lucene.Net. Now i converted into Elastic Search and using the Lucene parcer.
Reply | Threaded
Open this post in threaded view
|

Re: search querystring for combining parent and child docs

Martijn v Groningen
You can nest your HasChildQuery inside a bool query and also add any
parent clauses to the bool query:
http://www.elasticsearch.org/guide/reference/query-dsl/bool-query.html

Something like this should work:
"query" : {
  "bool" : {
        "must" : [
            { "term" : { "PC1" : "R1" } },
            { "has_child" : {"type" : "child_type", "query" : {"term"
: {"CC1" : "XX"}}}}
        ]
    }
}

The above query should return R1.

Martijn

On 16 October 2012 12:25, ganesanmarappan <[hidden email]> wrote:

> Hi Friends
>
> I'm new in Elastic Search. i'm developing asp.net(c#) search application
> using ES.
>
> i have used two tables. First one is Parent table and another one is Child
> Table. i can able to retrieve parent records using HasChildQuery. what my
> question is , i want to combine the parent and child table queries in single
> search query.
>
> Ex:
>
> Parent Table : [parent_type]
>
> PC1  | PC2 | PC3  | PC4
> ------------------------
> R1      A      B         C
> R2      AA    BB        CC
> R3      AAA  BBB      CCC
>
>
> Child Table : [child_type]
>
> PC1  | CC1 | CC2  | CC3
> ------------------------
> R1      X      Y         Z
> R1      XX    YY        ZZ
> R2      XXX  YYY      ZZZ
>
>
> I need the query string like this.
>
> ( PC1:"R1" AND [child_type].CC1:"XX" )
>
> Please provide the querystring for the above.
>
> Note: Earlier i have used Lucene.Net. Now i converted into Elastic Search
> and using the Lucene parcer.
>
>
>
> --
> View this message in context: http://elasticsearch-users.115913.n3.nabble.com/search-querystring-for-combining-parent-and-child-docs-tp4024046.html
> Sent from the ElasticSearch Users mailing list archive at Nabble.com.
>
> --
>
>



--
Met vriendelijke groet,

Martijn van Groningen

--


Reply | Threaded
Open this post in threaded view
|

Re: search querystring for combining parent and child docs

Ganesan Marappan
Thanks Martijn. The given seneario we have implemented and working fine for simple queries. if the query is complicated like below..

( PC1:"R1" AND [child_type].CC1:"XX" ) OR (( PC1:"R2" OR [child_type].CC1:"XXX" ) AND PC4:"CCC")

we are unable to build the query using HasChild.

Please let us know if query can be framed like [child_type].CC1:"XX" in querystring search.

Reply | Threaded
Open this post in threaded view
|

Re: search querystring for combining parent and child docs

Ganesan Marappan
In reply to this post by Martijn v Groningen
Thanks Martijn. The given seneario we have implemented and working fine for simple queries. if the query is complicated like below..

( PC1:"R1" AND [child_type].CC1:"XX" ) OR (( PC1:"R2" OR [child_type].CC1:"XXX" ) AND PC4:"CCC")

we are unable to build the query using HasChild.

Please let us know if query can be framed like [child_type].CC1:"XX" in querystring search.
Reply | Threaded
Open this post in threaded view
|

Re: search querystring for combining parent and child docs

Clinton Gormley-2
In reply to this post by Ganesan Marappan
On Tue, 2012-10-16 at 04:25 -0700, Ganesan Marappan wrote:

> Thanks Martijn. The given seneario we have implemented and working fine for
> simple queries. if the query is complicated like below..
>
> ( PC1:"R1" AND [child_type].CC1:"XX" ) OR (( PC1:"R2" OR
> [child_type].CC1:"XXX" ) AND PC4:"CCC")
>
> we are unable to build the query using HasChild.
>
> Please let us know if query can be framed like [child_type].CC1:"XX" in
> querystring search.

Don't try to express your entire query in the query_string - it is
complicated, difficult to debug, and prone to error.

The Query DSL is your friend.

clint


--