1. Search API
- sending search parameters through the REST request URI
- sending search parameters through the REST request body
Request UI
The REST API for search is accessible from the _search endpoint. This example returns all documents in the bank index:
|
|
- _search : search in the bank index
- q=* : to match all documents in the index
- sort=account_number:asc : sort the results using the account_number field of each document in an ascending order
- pretty : return pretty-printed JSON results
Request Body
|
|
2. Query language
Search all
size defaults to 10
|
|
from is 0-based
|
|
Sort
|
|
3. Populate Search
Return two fields, account_number and balance (inside of _source)
|
|
Match Query
Returns the account numbered 20
|
|
Returns all accounts containing the term “mill”
|
|
Returns all accounts containing the term “mill” or “lane” in the address
|
|
A variant of match (match_phrase)
Returns all accounts containing the phrase “mill lane” in the address
|
|
Bool query
The bool query allows us to compose smaller queries into bigger queries using boolean logic.
the bool must clause specifies all the queries that must be true for a document to be considered a match
|
|
composes two match queries and returns all accounts containing “mill” or “lane” in the address
|
|
Composes two match queries and returns all accounts that contain neither “mill” nor “lane” in the address
|
|
returns all accounts of anybody who is 40 years old but doesn’t live in ID(aho)
|
|
4. Filter
|
|
5. Aggregations
We set size=0 to not show search hits because we only want to see the aggregation results in the response.
|
|
calculates the average account balance by state
only for the top 10 states sorted by count in descending order
sort on the average balance in descending order
|
|
group by age brackets (ages 20-29, 30-39, and 40-49), then by gender, and then finally get the average account balance, per age bracket, per gender
|
|