Query Lite
Overview
Query Lite provides a simple way to search Elasticsearch using query parameters. It is useful for quick searches without requiring JSON bodies.
- Easy to use for basic searches
- Reduces the need for complex query structures
Query Lite is previously known as URI Search.
Simpler Queries
As an example, we can search for documents with a specific title by appending a query string:
/movies/_search?q=title:star
Similarly, to query for movies released after 2010 with a title containing "Trek":
/movies/_search?q=+year:>2010+title:Trek
Take note that spaces and special characters must be URL encoded before being sent:
/movies/_search?q=+year:>2010+title:trek
The above query will be encoded as:
/movies/_search?q=%2Byear%3A%3E2010+%2Btitle%3Atrek
Using simple queries is convenient for experimenting, but they come with risks:
- Difficult to debug when issues arise
- Potential security risks if exposed to end users
- Prone to errors – a single mistake can cause failure
Examples
-
For the following examples, download the datasets:
-
First, store the Elasticsearch endpoint and credentials in variables:
ELASTIC_ENDPOINT="https://your-elasticsearch-endpoint"
ELASTIC_USER="your-username"
ELASTIC_PW="your-password" -
Import the dataset into Elasticsearch:
curl -u $ELASTIC_USER:$ELASTIC_PW \
-H 'Content-Type: application/json' \
--data-binary @movies.json \
-XPUT $ELASTIC_ENDPOINT:9200/_bulk?pretty
Start using the query lite:
-
Run the command below to search for movies with "Star" in the title:
curl -u $ELASTIC_USER:$ELASTIC_PW \
-H 'Content-Type: application/json' \
-XGET "$ELASTIC_ENDPOINT:9200/movies/_search?q=title:star&pretty"Output:
{
"took" : 7,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 2,
"relation" : "eq"
},
"max_score" : 0.8025915,
"hits" : [
{
"_index" : "movies",
"_id" : "135569",
"_score" : 0.8025915,
"_source" : {
"id" : "135569",
"title" : "Star Trek Beyond",
"year" : 2016,
"genre" : [
"Action",
"Adventure",
"Sci-Fi"
]
}
},
{
"_index" : "movies",
"_id" : "122886",
"_score" : 0.56478655,
"_source" : {
"id" : "122886",
"title" : "Star Wars: Episode VII - The Force Awakens",
"year" : 2015,
"genre" : [
"Action",
"Adventure",
"Fantasy",
"Sci-Fi",
"IMAX"
]
}
}
]
}
} -
Now search for movies released after 2010 with "Trek" in the title
curl -u $ELASTIC_USER:$ELASTIC_PW \
-H 'Content-Type: application/json' \
-XGET "$ELASTIC_ENDPOINT:9200/movies/_search?q=+year>2010+title:trek&pretty"Output:
{
"took" : 7,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 1.3940738,
"hits" : [
{
"_index" : "movies",
"_id" : "135569",
"_score" : 1.3940738,
"_source" : {
"id" : "135569",
"title" : "Star Trek Beyond",
"year" : 2016,
"genre" : [
"Action",
"Adventure",
"Sci-Fi"
]
}
}
]
}
}