Empty Search
GET /_search
{}
GET /index_2014*/type1,type2/_search
{}
GET /_search
{
"from": 30,
"size": 10
}
The search API also accepts POST requests,
So, we can replace GET request with POST
Query DSL
Structure of a Query Clause
{
QUERY_NAME: {
ARGUMENT: VALUE,
ARGUMENT: VALUE,...
}
}
A query clause typically has
this structure:
If it references one particular field, it
has this structure:
GET /_search
{
"query": {
"match": {
"tweet": "elasticsearch"
}
}
}
{
QUERY_NAME: {
FIELD_NAME: {
ARGUMENT: VALUE,
ARGUMENT: VALUE,...
}
}
}
For instance, you can use a match query clause to find tweets that
mention elasticsearch in thetweet field:
{
"match": {
"tweet": "elasticsearch"
}
}
Query DSL
Combining Multiple Clauses
● Query clauses are simple building blocks that can be combined with each other
to create complex queries. Clauses can be as follows:
● Leaf clauses (like the match clause) that are used to compare a field (or fields) to a query
string
● Compound clauses that are used to combine other query clauses. For instance, a bool
clause allows you to combine other clauses that either must match, must_not match, or
should match if possible. They can also include non-scoring, filters for structured search:
{
"bool": {
"must": { "match": { "tweet": "elasticsearch" }},
"must_not": { "match": { "name": "mary" }},
"should": { "match": { "tweet": "full text" }},
"filter": { "range": { "age" : { "gt" : 30 }} }
}
}
{
"bool": {
"must": { "match": { "email": "business opportunity" }},
"should": [
{ "match": { "starred": true }},
{ "bool": {
"must": { "match": { "folder": "inbox" }},
"must_not": { "match": { "spam": true }}
}}
],
"minimum_should_match": 1
}
}
Queries and Filters
● Filtering context (non-scoring query)
● Is the created date in the range 2013 – 2014?
● Does the status field contain the term published?
● Is the lat_lon field within 10km of a specified point?
● Query context. (scoring query)
● Best matching the words full text search
● Containing the word run, but maybe also matching runs, running, jog, or sprint
● Containing the words quick, brown, and fox—the closer together they are, the
more relevant the document
● Tagged with lucene, search, or java—the more tags, the more relevant the
document