Show Menu
Cheatography

Elasticsearch Example Queries Cheat Sheet by

Example queries for Elasticsearch and Lucene.

Count items in an index

GET customer_index/_count
{
	"query" : {
		"match_all" : {}
	}
}

Return the top 200 products that are movies

GET product_index/_search
{
	"size" : 200,
	"query" : {
		"bool" : {
			"must" : [
				{
					"match" : {
						"category" : "Movie"
					}
				}			
			]
		}
	}
}

Get an aggregate list of product categories

GET product_index/_search
{
	"size" : 0,
	"aggs" : {
		"category" : {
			"terms" : {
				"field" : "category.keyword",
				"order" : {
					"_key" : "asc"
				}
			}
		}
	}
}

Get a list of products that do not have a category

GET product_index/_search
{
	"size" : 200,
	"query" : {
		"bool" : {
			"must_not" : [
				{
					"exists" : {
						"field" : "category"
					}
				}
			],
			"minimum_should_match" : 1,
			"should" : [
				{
					"match" : {
						"product_type" : {
							"query" : "Physical"
						}
					}
				}
			]
		}
	}
}

Group by Aggregate

GET product_index/_search
{
    "size" : 0, 
    "query" : {
        "bool" : {
            "must" : [
                {
                    "match" : {
                        "product_type" : "Physical"
                    }
                },
                {
                    "wildcard" : {
                        "category.keyword" : {
                            "value" : "DVD"
                        }
                    }
                }
                
            ]
        }
    },
    "aggs" : {
        "group_by_column" : {
            "terms" : {
                "field" : "category.keyword",
                "size" : 10000
            }
        }
    
    }
}
 

Get top 200 from the custom­er_­index

GET customer_index/_search
{
	"size" : 200,
	"query" : {
		"match_all" : {}
	}
}

Search that have DVD in the name with a wildcard

GET product_index/_search
{
	"size" : 200,
	"query" : {
		"wildcard" : {
			"name" : {
				"value" : "DVD"
			}
		}
	}
}

Perform a search and order by using functions

GET product_index/_search
{
  "size": 200,
  "query": {
    "function_score": {
      "query": {
        "bool": {
          "must": [
            {
              "bool": {
                "minimum_should_match": 1,
                "should": [
                  {
                    "term": {
                      "category.keyword": "Books"
                    }
                  },
                  {
                    "term": {
                      "category.keyword": "Movies"
                    }
                  }
                ]
              }
            }
          ],
          "minimum_should_match": 1,
          "should": [
            {
              "match_phrase": {
                "nameLowercase": "journey"
              }
            },
            {
              "match_phrase": {
                "descriptionLowercase": "journey"
              }
            }
          ]
        }
      },
      "functions": [
        {
          "filter": {
            "bool": {
              "must": [
                {
                  "term": {
                    "nameLowercase.keyword": "journey"
                  }
                }
              ]
            }
          },
          "weight": 3
        },
        {
          "filter": {
            "bool": {
              "must": [
                {
                  "match_phrase": {
                    "nameLowercase": "journey"
                  }
                }
              ]
            }
          },
          "weight": 2
        },
        {
          "filter": {
            "bool": {
              "must": [
                {
                  "match_phrase": {
                    "descriptionLowercase": "journey"
                  }
                }
              ]
            }
          },
          "weight": 1
        }
      ],
      "score_mode": "first",
      "boost_mode": "replace"
    }
  },
  "sort": {
    "_score": {
      "order": "desc"
    }
  }
}
       
 

Comments

No comments yet. Add yours below!

Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.

          Related Cheat Sheets

          Lucene Query Syntax Cheat Sheet
          AWS Services by Nir Elbaz
          AWS Services Cheat Sheet

          More Cheat Sheets by GregFinzer

          C# Naming Conventions Cheat Sheet
          Angular CLI Cheat Sheet
          Gremlin Cheat Sheet