Introduction to Amazon Elasticsearch

   Go back to the Task List

  « 3. Insert Data    5. Update the Data »

4. Search & Query Data

You perform search or query operations in this step. You will use Postman to send your search / query requests.

  1. If you know id of the record, you can make simple query using the GET method. For instance, when you call the following URL with the GET method, it will fetch details of the employee with id = 1. Replace {ES_ENDPOINT_URL} with Elasticsearch Endpoint.

    URL: {ES_ENDPOINT_URL}/organization/employee/1

    Amazon Elasticsearch

  2. You can call search URL with the GET method to list all the records. The following URL will fetch all the records.

    URL: {ES_ENDPOINT_URL}/organization/_search

    Amazon Elasticsearch

  3. You can call search URL with the Post method to list all the records sorted on a particular attribute. The following URL will fetch all records sorted by age. You need to pass the json as the request body.

    URL: {ES_ENDPOINT_URL}/organization/_search

    {
      "query": { "match_all": {} },
      "sort": [
        { "age": "asc" }
      ]
    }
    

    `

    Amazon Elasticsearch

  4. You can also call search URL with the Post method to make queries for the records based on the attributes of the records. The following URL will fetch all records where the department = IT. You need to pass the json as the request body.

    URL: {ES_ENDPOINT_URL}/organization/_search

    {
      "query": { "match": { "department": "IT" } }
    }
    

    `

    Amazon Elasticsearch

  5. You can also apply filter criteria on the search query. The following URL will fetch all records where the department = IT and age < 30. You need to pass the json as the request body.

    URL: {ES_ENDPOINT_URL}/organization/_search

    {
      "query": { 
        "bool": { 
          "must": [
            { "match": { "department":   "IT" }}
          ],
          "filter": [ 
            { "range": { "age": { "lt": 30 }}}
          ]
        }
      }
    }
    

    `

    Amazon Elasticsearch

  6. You can also make aggregation query in Elasticsearch. For instance, the following URL will fetch statistics about the age field. You need to pass the json as the request body.

    URL: {ES_ENDPOINT_URL}/organization/_search

    {
      "size": 0, 
     "aggs": {
      "age_stats": {
        "stats": {
          "field": "age"
        }
      }
    }
    }
    

    `

    Amazon Elasticsearch

  7. You can also make group by queries. For instance, the following URL will fetch average age of the employees based on their duration in the organization. You need to pass the json as the request body.

    URL: {ES_ENDPOINT_URL}/organization/_search

    {
      "aggs": {
        "durationwiseage": {
          "terms": {
            "field": "duration",
            "order": { "avg_age": "desc" }
          },
          "aggs": {
            "avg_age": { "max": { "field": "age" } }
          }    
        }  
       } 
    }
    

    `

    Amazon Elasticsearch

  8. These were some of the query examples. In the next step, you learn about updating the records.