Search

Basic Forms

GET /<idx>/_search
{
    "query": {
        "match": {
            "<field>": "<value>"
        }
    }
}

OR

GET /<idx>/_search?q=<field>:<value>

OR

GET /<idx>/_search
{
    "query": {
        "query_string": {
            "query": "<field>:<value>"
        }
    }
}

Searching with request URI

GET /<index>/_search?q=*
GET /<index>/_search?q=f:v AND f2:v2

Query DSL

GET /<index>/_search
{
    "query": {
        "match_all": {}
    }
}

Debugging unexpected search results

e.g.

GET /<idx>/1/_explain
{
    "query": {
        "term": {
            "<field>": "<value>"
        }
    }
}

Full text queries vs. Term level queries

  • Term Query --> Inverted Index

    • "chris" will match with "chris" in Inverted Index

    • "Chris" will not match

  • Match Query --> Analysis --> Inverted Index

    • "Chris" will be converted to "chris", which has a match in Inverted Index

Full Text Queries

Search for a term

GET /pproducts/_search
{
  "query": {
    "term": {
      "is_active": true
    }
  }
}

Search for multiple terms

GET /pproducts/_search
{
  "query": {
    "terms": {
      "tags.keyword": [ "Soup", "Cake" ]
    }
  }
}

Search by document IDs

GET /pproducts/_search
{
  "query": {
    "ids": {
      "values": [1,2,3]
    }
  }
}

Matching documents with range values

GET /pproducts/_search
{
  "query": {
    "range": {
      "in_stock": {
        "gte": 1,
        "lte": 5
      }
    }
  }
}
GET /pproducts/_search
{
  "query": {
    "range": {
      "created": {
        "gte": "2010/01/01",
        "lte": "2010/12/31"
      }
    }
  }
}


# With customized date format
GET /pproducts/_search
{
  "query": {
    "range": {
      "created": {
        "gte": "01-01-2010",
        "lte": "31-12-2010",
        "format": "dd-MM-yyyy"
      }
    }
  }
}

Relative Dates (date math)

https://www.elastic.co/guide/en/elasticsearch/reference/current/common-options.html#date-math

GET /pproducts/_search
{
  "query": {
    "range": {
      "created": {
        "gte": "2010/01/01||-1y"
      }
    }
  }
}

GET /pproducts/_search
{
  "query": {
    "range": {
      "created": {
        "gte": "2010/01/01||-1y-1d"
      }
    }
  }
}

Match documents with non-null values

GET /pproducts/_search
{
    "query": {
        "exists": {
            "field": "tags"
        }
    }
}

Match based on prefixes

GET /pproducts/_search
{
    "query": {
        "prefix": {
            "tags.keyword": "Vege"
        }
    }
}

Searching with wildcards

  • Any characters *

  • Single wildcard character *

  • Wildcard could be slower

GET /pproducts/_search
{
    "query": {
        "wildcard": {
            "tags.keyword": "Ve*"
        }
    }
}

Searching with regex

GET /pproducts/_search
{
    "query": {
        "regexp": {
            "tags.keyword": "Ve[a-zA-Z]+able"
        }
    }
}

Full Text Queries

Flexible matching with the match query

GET /recipe/_search
{
  "query": {
    "match": {
      "title": "spaghetti with pasta or spaghetti"
    }
  }
}

All words should exist:

GET /recipe/_search
{
  "query": {
    "match": {
        "title": {
          "query": "spaghetti with pasta or spaghetti",
          "operator": "and"
        }
    }
  }
}

Matching phrases

  • Similar to searching "spaghetti puttanesca"

GET /recipe/_search
{
  "query": {
    "match_phrase": {
      "title": "spaghetti puttanesca"
    }
  }
}

Searching multiple fields

  • Either title or description has the term "pasta"

GET /recipe/_search
{
  "query": {
    "multi_match": {
      "query": "pasta",
      "fields": [ "title", "description" ]
    }
  }
}

Last updated