Wednesday, May 25, 2022

Example ElasticSearch Queries

Overview

There are three different match types in ElasticSearch

  • term - the whole field must match.  This has a limit of matching only the first 256 characters.
  • wildcard - ability to search for partial words.  Example: break* will match break and breaking.  This also has a limit of 256 characters.
  • match_phrase - This matches on a word boundary and can go longer than 256 characters.
Case insensitive searches are super slow.  Create a lowercase field in ElasticSearch instead.

Example queries that can be used for ElasticSearch and Lucene

# Count items in an index
GET customer_index/_count
{
    "query" : {
        "match_all" : {}
    }
}

# Get top 200 from the customer_index
GET customer_index/_search
{
    "size" : 200,
    "query" : {
        "match_all" : {}
    }
}

# Return the top 200 products that are movies
GET product_index/_search
{
    "size" : 200,
    "query" : {
        "bool" : {
            "must" : [
                {
                    "match" : {
                        "category" : "Movie"
                    }
                }          
            ]
        }
    }
}

# Search for any products that have DVD in the name with a wildcard
GET product_index/_search
{
    "size" : 200,
    "query" : {
        "wildcard" : {
            "name" : {
                "value" : "*DVD*"
            }
        }
    }
}

# Get an aggregate list of product categories sorted alphabetically
GET product_index/_search
{
    "size" : 0,
    "aggs" : {
        "category" : {
            "terms" : {
                "field" : "category.keyword",
                "order" : {
                    "_key" : "asc"
                }
            }
        }
    }
}

# Get a list of physical 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 _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
            }
        }
   
    }
}


# 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"
    }
  }
}