Tuesday, 6 October 2015

Elasticsearch: Search for documents

 In this post I am going to explain simple search queries.

GET /_search

By default _search will return all the documents in elastic search cluster. Search result contains all kind of documents from different index, types.

If you want to search in specific index, you can mention it in search url.

Get all employee documents
By using _search endpoint, we can retrieve all employees.

GET /xyz/employees/_search

Above statement returns all employee documents at index xyz, type employees.

{
   "took": 6,
   "timed_out": false,
   "_shards": {
      "total": 5,
      "successful": 5,
      "failed": 0
   },
   "hits": {
      "total": 3,
      "max_score": 1,
      "hits": [
         {
            "_index": "xyz",
            "_type": "employees",
            "_id": "1",
            "_score": 1,
            "_source": {
               "firstName": "Phalgun",
               "lastName": "Garimella",
               "hobbies": [
                  "Watching movies",
                  "Stamp collection",
                  "Reading books",
                  "Playing Cricket"
               ],
               "age": 30
            }
         },
         {
            "_index": "xyz",
            "_type": "employees",
            "_id": "2",
            "_score": 1,
            "_source": {
               "firstName": "Sankalp",
               "lastName": "Dubey",
               "hobbies": [
                  "Shopping",
                  "Swimming",
                  "Reading books"
               ],
               "age": 32
            }
         },
         {
            "_index": "xyz",
            "_type": "employees",
            "_id": "3",
            "_score": 1,
            "_source": {
               "firstName": "Arpan",
               "lastName": "Debroy",
               "hobbies": [
                  "Tattoos",
                  "Fencing",
                  "Shopping"
               ],
               "age": 28
            }
         }
      ]
   }
}

Get employees whose last name is Debroy
By passing query parameters to search end point, we can get all employees, whose last name is Debroy.

GET /xyz/employees/_search?q=lastName:Debroy

Above request returns following response.

{
   "took": 3,
   "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
            }
         }
      ]
   }
}


Important section of the response is “hits”, which contains matched results, “total” represents total number of documents matched to this query.

As you observe, each result in “hits” array contains some metadata like _index, _type, _id, _score. ‘_score” field specifies, how relevant this document for search query. ‘_source’ field contains actual content of the document.

By default search query returns all the matched documents in ascending order of _score.

“took” field specifies how many seconds the search request takes to execute.

“_shards” field specifies total number of shard involved to process the request, “successful” specifies how many shards were successful, “failed” specifies how many shards were failed.

“timed_out” specifies whether query timed out or not. By default there is no time out for search queries. If you want to specify timeout for your search query, you can specify it in query parameter like below.

GET /_search?timeout=10ms

Search for documents in more than one index
GET /xyz,abc/_search

Above query returns all the documents in indexes xyz, abc.

{
   "took": 2,
   "timed_out": false,
   "_shards": {
      "total": 10,
      "successful": 10,
      "failed": 0
   },
   "hits": {
      "total": 7,
      "max_score": 1,
      "hits": [
         {
            "_index": "xyz",
            "_type": "employees",
            "_id": "4",
            "_score": 1,
            "_source": {
               "firstName": "Tushar",
               "lastName": "Goyal",
               "hobbies": [
                  "Reading Novels",
                  "Playing football"
               ],
               "age": 23
            }
         },
         {
            "_index": "abc",
            "_type": "employees",
            "_id": "1",
            "_score": 1,
            "_source": {
               "firstName": "Hari Krishna",
               "lastName": "Gurram"
            }
         },
         {
            "_index": "xyz",
            "_type": "products",
            "_id": "1",
            "_score": 1,
            "_source": {
               "price": "2560.19",
               "items_available": "131"
            }
         },
         {
            "_index": "xyz",
            "_type": "employees",
            "_id": "6",
            "_score": 1,
            "_source": {
               "firstName": "Ranganath",
               "lastName": "ranga",
               "age": "30"
            }
         },
         {
            "_index": "xyz",
            "_type": "employees",
            "_id": "2",
            "_score": 1,
            "_source": {
               "firstName": "Sankalp",
               "lastName": "Dubey",
               "hobbies": [
                  "Shopping",
                  "Swimming",
                  "Reading books"
               ],
               "age": 32
            }
         },
         {
            "_index": "xyz",
            "_type": "products",
            "_id": "2",
            "_score": 1,
            "_source": {
               "items_available": "23"
            }
         },
         {
            "_index": "xyz",
            "_type": "employees",
            "_id": "3",
            "_score": 1,
            "_source": {
               "firstName": "Arpan",
               "lastName": "Debroy",
               "hobbies": [
                  "Tattoos",
                  "Fencing",
                  "Shopping"
               ],
               "age": 28
            }
         }
      ]
   }
}

Search all types in any indices beginning with x or beginning with z
GET /x*,a*/_search

Search types employees, products in index xyz
GET /xyz/employees,products/_search

Search types employees, products in indexes xyz, abc
GET /xyz,abc/employees,products/_search

Search types employees, products in all indexes
GET /_all/employees,products/_search

Note
a. By default search returns top 10 results.





Prevoius                                                 Next                                                 Home

No comments:

Post a Comment