elastic search|April 18, 2018|2 min read

Common used Elastic Search queries

Common used Elastic Search queries

Listing down the commonly used Elastic Search queries.

To see all available Index names

GET /_cat/indices?v

Output:

``` health status index uuid pri rep docs.count docs.deleted store.size pri.store.size green open .kibana HObWTCY_SZOWqShWiQ1vRA 1 1 8 3 55.5kb 27.7kb ```

To see all the Documents for an Index

GET INDEX_NAME/_search
{
  "query": {
    "match_all": {}
  }
}

To see all the Documents for an Index and a type

GET INDEX_NAME/TYPE_NAME/_search
{
  "query": {
    "match_all": {}
  }
}

Insert/Update a document

PUT INDEX_NAME/TYPE_NAME/6
{
  "name": "name2",
  "createdAt": "2017-07-27T08:38:48.276Z"
}

Delete a document by Id

DELETE INDEX_NAME/TYPE_NAME/ID

Get only specified attributes of document in result

GET INDEX_NAME/TYPE_NAME/_search
{
  "query": {
    "match_all": {}
  },
  "_source": ["ATTRIBUTE_NAME1", "ATTRIBUTE_NAME2"]
}

Get result within Date ranges specified

You can get search results within two dates specified.

Assuming: createdAt is the attribute which has date of creation of document.

Here,
gte means, greater than or equal to
lte means, less than or equal to

GET INDEX_NAME/TYPE_NAME/_search
{
  "query": {
    "range": {
      "createdAt": {
        "gte": "24/07/2017-13/02/12",
        "lte": "26/07/2017-15/02/05",
        "format": "dd/MM/yyyy-HH/mm/ss"
      }
    }
  }
}

To get last updated record based on date

You have number of records. When you search by a unique identifier, you get all of them. But, in many cases, you might be needing only latest one or last few records.

GET INDEX_NAME/TYPE_NAME/_search
{
  "query": {
    "match": {
      "NAME_ATTRIBUTE": "My identifier for name attribute"
    }
  },
  "size": 10,
  "sort": [
    {
      "createdAt": {
        "order": "desc"
      }
    }
  ]
}

Above code will give 10 records of criteria mentioned. If I need only latest one record. I need to mention size to 1.

GET INDEX_NAME/TYPE_NAME/_search
{
  "query": {
    "match": {
      "NAME_ATTRIBUTE": "My identifier for name attribute"
    }
  },
  "size": 1,
  "sort": [
    {
      "createdAt": {
        "order": "desc"
      }
    }
  ]
}

Search on nested attribute

GET INDEX_NAME/TYPE_NAME/_search
{
  "query": {
    "match": {
      "data.key": "My value"
    }
  }
}

Search on nested attribute, and get latest one

GET INDEX_NAME/TYPE_NAME/_search
{
  "query": {
    "match": {
      "data.key": "My value"
    }
  },
  "size": 1,
  "sort": [
    {
      "createdAt": {
        "order": "desc"
      }
    }
  ]
}

Delete all documents from a Type

DELETE INDEX_NAME/TYPE_NAME/

Delete an Index completely

DELETE INDEX_NAME

Search for the Existence of a Particular Attribute

You want to search for all the documents where this particular attribute is not present.

Note: I’m not talking about its value. Just presence.

GET /index_name/type_name/_search
{
  "query" : {
        "bool": {
          "must_not": [
            { "exists": { "field": "data.attrs.syncIssue" }}
          ]
        }
    }
}

Related Posts

Resolving Checkmarx issues reported

Resolving Checkmarx issues reported

So, here we are using input variable String[] args without any validation…

Azure Storage Blob - How to List and Download Blob from Azure Storage container in Python (No Azure library)

Azure Storage Blob - How to List and Download Blob from Azure Storage container in Python (No Azure library)

Introduction In this tutorial we will see, How to list and download storage…

ReactJS - How to restrict data type for different kind of data

ReactJS - How to restrict data type for different kind of data

Introduction Javascript is not a strict type language. We can use a variable to…

How to show a block of html or div to the bottom of a web page

How to show a block of html or div to the bottom of a web page

To download this code from git: See: Gyanblog Github

Drupal: How to block a user by email programatically

Drupal: How to block a user by email programatically

Many times, while administering your drupal website, you must have encountered…

Drupal 8 - How to show view user data to owner user only

Drupal 8 - How to show view user data to owner user only

Introduction to Problem I have some data related to users. For example: which…

Latest Posts

Jenkins Pipeline with Jenkinsfile - How To Schedule Job on Cron and Not on Code Commit

Jenkins Pipeline with Jenkinsfile - How To Schedule Job on Cron and Not on Code Commit

Introduction In this post we will see following: How to schedule a job on cron…

How to Git Clone Another Repository from Jenkin Pipeline in Jenkinsfile

How to Git Clone Another Repository from Jenkin Pipeline in Jenkinsfile

Introduction There are some cases, where I need another git repository while…

How to Fetch Multiple Credentials and Expose them in Environment using Jenkinsfile pipeline

How to Fetch Multiple Credentials and Expose them in Environment using Jenkinsfile pipeline

Introduction In this post, we will see how to fetch multiple credentials and…

Jenkins Pipeline - How to run Automation on Different Environment (Dev/Stage/Prod), with Credentials

Jenkins Pipeline - How to run Automation on Different Environment (Dev/Stage/Prod), with Credentials

Introduction I have an automation script, that I want to run on different…

Jenkinsfile - How to Create UI Form Text fields, Drop-down and Run for Different Conditions

Jenkinsfile - How to Create UI Form Text fields, Drop-down and Run for Different Conditions

Introduction I had to write a CICD system for one of our project. I had to…

Java Log4j Logger - Programmatically Initialize JSON logger with customized keys in json logs

Java Log4j Logger - Programmatically Initialize JSON logger with customized keys in json logs

Introduction Java log4j has many ways to initialize and append the desired…