Thursday 8 October 2015

Elastic search: Query DSL

In previous post, I explained how to search for documents using query parameters. One problem with query parameters is, it is difficult to write complex queries. To solve this problem, Elasticsearch provides its own query language called the query DSL (Domain specific language), used to build complex queries.

DSL is specified using JSON request body.

Get all employees whose last name is Debroy
GET /xyz/employees/_search
{
  "query":{
    "match": {
      "lastName": "Debroy"
    }
  }
}

Above query returns all documents, whose lastName is Debroy.

{
   "took": 1,
   "timed_out": false,
   "_shards": {
      "total": 5,
      "successful": 5,
      "failed": 0
   },
   "hits": {
      "total": 1,
      "max_score": 0.30685282,
      "hits": [
         {
            "_index": "xyz",
            "_type": "employees",
            "_id": "3",
            "_score": 0.30685282,
            "_source": {
               "firstName": "Arpan",
               "lastName": "Debroy",
               "hobbies": [
                  "Tattoos",
                  "Fencing",
                  "Shopping"
               ],
               "age": 28
            }
         }
      ]
   }
}


Prevoius                                                 Next                                                 Home

No comments:

Post a Comment