06 - Introduction to term level queries

Searching for a Term

  • Mostly used for quering structured data.
  • Term level queries are used to find exact matches.
GET /products/_search
{
  "query": {
    "term": {
      "is_active": true
    }
  }
}
GET /products/_search
{
  "query": {
    "term": {
      "is_active": true
        "value": true
      }
    }
  }
}

Searching Multiple Terms

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

Retrieving document based on Ids

GET /products/_search
{
  "query": {
    "ids": {
      "values": ["10","11","12"]
    }
  }
}

Matching Documents with Range

GET /products/_search
{
  "query": {
    "range": {
      "in_stock": {
        "gte": 1,
        "lte": 5
      }
    }
  }
}

Range query with dates

GET /products/_search
{
  "query": {
    "range": {
      "created": {
        "gte": "2010/01/01",
        "lte": "2010/12/31"
      }
    }
  }
}

Working with relative dates

Date Math

# Get Products created within last 5 Years
GET /products/_search
{
  "query": {
    "range": {
      "created": {
        "gte": "now-5y"
      }
    }
  }
}

Matching documents with Non-Null values

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

Matching based on Prefix

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

Wildcard Query

GET /products/_search
{
  "query": {
    "wildcard": {
      "tags.keyword": {
        "value": "Be*"
      }
    }
  }
}

Regex Query

GET /products/_search
{
  "query": {
    "regexp": {
      "tags.keyword": {
        "value": "Veget[a-zA-Z]+ble"
      }
    }
  }
}