How to do it...

To execute the search and view the results, we will perform the following steps:

  1. From the command line, we can execute a search as follows:
GET /mybooks/_search
{
"query": {
"match_all": {}
}
}

In this case, we have used a match_all query that returns all the documents. We'll discuss this kind of query in the Matching all the documents recipe in this chapter.

  1. If everything works, the command will return the following:
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 3,
"max_score" : 1.0,
"hits" : [
...
...
"_index" : "mybooks",
"_type" : "_doc",
"_id" : "3",
"_score" : 1.0,
"_source" : {...truncated...}
}
]
}
}

These results contain the following information:

  • took is the milliseconds of time required to execute the query.
  • time_out indicates whether a timeout occurred during the search. This is related to the timeout parameter of the search. If a timeout occurs, you will get partial or no results.
  • _shards is the status of shards divided into the following sections:
    • total, which is the number of shards
    • successful, which is the number of shards in which the query was successful
    • skipped, which is the number of shards that are skipped during the search (for example, if you are searching more than 720 shards simultaneously)
    • failed, which is the number of shards in which the query failed, because some error or exception occurred during the query
  •  hits are the results, and are composed of the following:
    • total is the number of documents that match the query.
    • max_score is the match score of first document. It is usually one if no match scoring was computed, for example, in sorting or filtering.
    • hits, which is a list of result documents.

The resulting document has a lot of fields that are always available and others that depend on search parameters. The most important fields are as follows:

  • _index: The index that contains the document.
  • _type: The type of the document (that is, _doc). It will disappear in future ES versions.
  • _id: The ID of the document.
  • _source: The document source—the original json sent to Elasticsearch.
  • _score: Query score of the document (if the query doesn't require a score, it's 1.0).
  • sort: If the document is sorted, values that are used for sorting.
  • highlight: Highlighted segments if highlighting was requested.
  • stored_fields: Some fields can be retrieved without needing to fetch the source object.
  • script_fields: Some fields that can be computed using scripting.