Elasticsearch Example Queries Cheat Sheet

A Programming › API cheat sheet in English by GregFinzer

Example queries for Elasticsearch and Lucene.

5,600 views 1,426 downloads 28 Sep 2023 History

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.

Cheatography n. The art of mapping complex subjects into clear, instantly navigable reference guides.