Now let’s start with some simple searches. There are two basic ways to run searches: one is by sending search parameters through the REST request URIand the other by sending them through the REST request body. The request body method allows you to be more expressive and also to define your searches in a more readable JSON format. We’ll try one example of the request URI method but for the remainder of this tutorial, we will exclusively be using the request body method.html
_search
endpoint. This example returns all documents in the bank index:
curl -X GET "localhost:9200/bank/_search?q=*&sort=account_number:asc&pretty"
Let’s first dissect the search call. We are searching (_search
endpoint) in the bank index, and the q=*
parameter instructs Elasticsearch to match all documents in the index. The sort=account_number:asc
parameter indicates to sort the results using the account_number
field of each document in an ascending order. The pretty
parameter, again, just tells Elasticsearch to return pretty-printed JSON results.express
{ "took" : 63, "timed_out" : false, "_shards" : { "total" : 5, "successful" : 5, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : 1000, "max_score" : null, "hits" : [ { "_index" : "bank", "_type" : "_doc", "_id" : "0", "sort": [0], "_score" : null, "_source" : {"account_number":0,"balance":16623,"firstname":"Bradshaw","lastname":"Mckenzie","age":29,"gender":"F","address":"244 Columbus Place","employer":"Euron","email":"bradshawmckenzie@euron.com","city":"Hobucken","state":"CO"} }, { "_index" : "bank", "_type" : "_doc", "_id" : "1", "sort": [1], "_score" : null, "_source" : {"account_number":1,"balance":39225,"firstname":"Amber","lastname":"Duke","age":32,"gender":"M","address":"880 Holmes Lane","employer":"Pyrami","email":"amberduke@pyrami.com","city":"Brogan","state":"IL"} }, ... ] } }
As for the response, we see the following parts:json
四、 hits – search results
搜索結果
五、hits.hits – actual array of search results (defaults to first 10 documents) 數組
實際的搜索結果數組(默認爲前10個文檔) 服務器
六、hits.sort - sort key for results (missing if sorting by score) app
對結果進行排序(若是按分數排序則丟失)curl
hits._score
and
max_score
- ignore these fields for now
curl -X GET "localhost:9200/bank/_search" -H 'Content-Type: application/json' -d' { "query": { "match_all": {} }, "sort": [ { "account_number": "asc" } ] } '
The difference here is that instead of passing q=*
in the URI, we provide a JSON-style query request body to the _search
API. We’ll discuss this JSON query in the next section.elasticsearch