准备测试数据集
- 下载测试数据
curl -O https://raw.githubusercontent.com/elastic/elasticsearch/master/docs/src/test/resources/accounts.json* - 通过下面的命令导入数据
curl -H "Content-Type: application/json" -XPOST 'localhost:9200/bank/account/_bulk?pretty&refresh' --data-binary "@accounts.json"
Introducing the Query Language
Elasticsearch provides a JSON-style domain-specific language that you can use to execute queries. This is referred to as the Query DSL. The query language is quite comprehensive and can be intimidating at first glance but the best way to actually learn it is to start with a few basic examples.
Going back to our last example, we executed this query:
GET /bank/_search{ "query": { "match_all": {} }}
COPY AS CURLVIEW IN CONSOLE
Dissecting the above, the query
part tells us what our query definition is and the match_all
part is simply the type of query that we want to run. The match_all
query is simply a search for all documents in the specified index.
In addition to the query
parameter, we also can pass other parameters to influence the search results. In the example in the section above we passed in sort
, here we pass in size
:
GET /bank/_search{ "query": { "match_all": {} }, "size": 1}
COPY AS CURLVIEW IN CONSOLE
Note that if size
is not specified, it defaults to 10.
This example does a match_all
and returns documents 11 through 20:
GET /bank/_search{ "query": { "match_all": {} }, "from": 10, "size": 10}
COPY AS CURLVIEW IN CONSOLE
The from
parameter (0-based) specifies which document index to start from and the size
parameter specifies how many documents to return starting at the from parameter. This feature is useful when implementing paging of search results. Note that if from
is not specified, it defaults to 0.
This example does a match_all
and sorts the results by account balance in descending order and returns the top 10 (default size) documents.
GET /bank/_search{ "query": { "match_all": {} }, "sort": { "balance": { "order": "desc" } }}
match_all, from,size、sort查询关键字
Executing Searches
GET /bank/_search
{
"query": { "match_all": {} },
"_source": ["account_number", "balance"]
}
We can combine must, should, and must_not clauses simultaneously inside a bool query. Furthermore, we can compose bool queries inside any of these bool clauses to mimic any complex multi-level boolean logic.
This example returns all accounts of anybody who is 40 years old but doesn’t live in ID(aho):
GET /bank/_search
{
"query": {
"bool": {
"must": [
{ "match": { "age": "40" } }
],
"must_not": [
{ "match": { "state": "ID" } }
]
}
}
}
这里我们初步学习了基本条件过滤查询,match_all match bool mast must_not should等
网友评论