Search code examples
grailselasticsearchgrails-ormelasticsearch-plugin

Query nested fields with elasticsearch in grails


I'm new to elasticseach and I'm having some issues to add filters to my queries.

My domain classes are like this:

Class A {
    String name
    A_Status status

    static searchable =  {
        status component: true
    }
}

Class AImpl extends A {

}

Class A_Status {
    String name
    static searchable = { 
        root : false
        only = 'name'
    }
}

On my controller the query I'm doing is:

def res = elasticSearchService.search()
{
    bool {
        must {
            term("status.name": "ACTIVE")
        }
    }
 }

I tried changing the searchable field to AImpl or putting "searchable = true", but had the same results, the query is empty always and it should get 4 results.

Another odd thing I found is that doing the uri search gives me the expected result, but the body query didn't.

curl -XGET 'http://localhost:9200/com.sisconline.entities/_search?q=status.name=ACTIVE'

This has 4 hits.

curl -XPOST 'http://localhost:9200/com.sisconline.entities/_search' -d '{
"query" : {
      "term":{ "status.name":"ACTIVE"}
      }
}'

This gets 0 hits.

I'm using Grails 2.3.4 and elasticsearch plugin 0.0.3.5.

Regards.


Solution

  • I finally managed to solve problem by doing :

    must { 
      nested { 
         path = "status" 
         query { 
             bool { 
                must { 
                   term("status.name": "active") } 
                } 
             } 
         } 
    } 
    

    Hope it helps others.