Continue to section 9
2、Query DSL
The form used in the previous section is
GET /bank/_search { "query": { "match_all": { } }, "sort": [ { "account_number": "asc" }, { "balance": "desc" } ] }
The query language style, we call it
Query DSL
.
1), basic syntax format
Elastisearch provides a Json style DSl (domain-specific language) that can perform queries. This is called Query DSL.
The query language is very comprehensive and feels a bit complicated at the beginning. The way to really learn it is to start with some basic examples.
- Typical structure of a query statement
{
QUERY_NAME:{
ARGUMENT: VALUE,
ARGUMENT: VALUE,
...
}
}
E.g:
GET /bank/_search
{
"query": {
"match_all": {}
}
}
- If it is for a certain field, its structure is as follows:
{
QUERY_NAME:{
FIELD_NAME:{
ARGUMENT: VALUE,
ARGUMENT: VALUE,
...
}
}
}
E.g:
GET /bank/_search { “query”: { “match_all”: {} }, “sort”: [ { “balance”: { “order”: “desc” } } ], “from”: 0, “size”: 5 } |
---|
-query defines how to query; -match_all query type [represents all of the query], es can combine a lot of query types in query to complete complex queries -in addition to query parameters, we can also pass other parameters to change the query result. Such as sort, size; -from+size limit, complete the paging function; -sort sort, multi-field sorting, the subsequent fields will be sorted internally when the previous fields are equal, otherwise the previous order prevails |
2), return some fields
GET /bank/_search
{
"query": {
"match_all": {
}
},
"sort": [
{
"balance": {
"order": "desc"
}
}
],
"from": 0,
"size": 5,
"_source": ["balance","firstname"]
}
Returns only _source
in the specified field, similar in MySQLselect field_1,field_2,... from table
reference:
Getting started with the full-text search engine Elasticsearch