|
@@ -4,9 +4,20 @@
|
|
|
Generally the total hit count can't be computed accurately without visiting all
|
|
|
matches, which is costly for queries that match lots of documents. The
|
|
|
`track_total_hits` parameter allows you to control how the total number of hits
|
|
|
-should be tracked. When set to `true` the search response will always track the
|
|
|
-number of hits that match the query accurately (e.g. `total.relation` will always
|
|
|
-be equal to `"eq"` when `track_total_hits is set to true).
|
|
|
+should be tracked.
|
|
|
+Given that it is often enough to have a lower bound of the number of hits,
|
|
|
+such as "there are at least 10000 hits", the default is set to `10,000`.
|
|
|
+This means that requests will count the total hit accurately up to `10,000` hits.
|
|
|
+It's is a good trade off to speed up searches if you don't need the accurate number
|
|
|
+of hits after a certain threshold.
|
|
|
+
|
|
|
+When set to `true` the search response will always track the number of hits that
|
|
|
+match the query accurately (e.g. `total.relation` will always be equal to `"eq"`
|
|
|
+when `track_total_hits is set to true). Otherwise the `"total.relation"` returned
|
|
|
+in the `"total"` object in the search response determines how the `"total.value"`
|
|
|
+should be interpreted. A value of `"gte"` means that the `"total.value"` is a
|
|
|
+lower bound of the total hits that match the query and a value of `"eq"` indicates
|
|
|
+that `"total.value"` is the accurate count.
|
|
|
|
|
|
[source,js]
|
|
|
--------------------------------------------------
|
|
@@ -50,57 +61,9 @@ GET twitter/_search
|
|
|
<1> The total number of hits that match the query.
|
|
|
<2> The count is accurate (e.g. `"eq"` means equals).
|
|
|
|
|
|
-If you don't need to track the total number of hits you can improve query times
|
|
|
-by setting this option to `false`. In such case the search can efficiently skip
|
|
|
-non-competitive hits because it doesn't need to count all matches:
|
|
|
-
|
|
|
-[source,js]
|
|
|
---------------------------------------------------
|
|
|
-GET twitter/_search
|
|
|
-{
|
|
|
- "track_total_hits": false,
|
|
|
- "query": {
|
|
|
- "match" : {
|
|
|
- "message" : "Elasticsearch"
|
|
|
- }
|
|
|
- }
|
|
|
-}
|
|
|
---------------------------------------------------
|
|
|
-// CONSOLE
|
|
|
-// TEST[continued]
|
|
|
-
|
|
|
-\... returns:
|
|
|
-
|
|
|
-[source,js]
|
|
|
---------------------------------------------------
|
|
|
-{
|
|
|
- "_shards": ...
|
|
|
- "timed_out": false,
|
|
|
- "took": 10,
|
|
|
- "hits" : { <1>
|
|
|
- "max_score": 1.0,
|
|
|
- "hits": ...
|
|
|
- }
|
|
|
-}
|
|
|
---------------------------------------------------
|
|
|
-// TESTRESPONSE[s/"_shards": \.\.\./"_shards": "$body._shards",/]
|
|
|
-// TESTRESPONSE[s/"took": 10/"took": $body.took/]
|
|
|
-// TESTRESPONSE[s/"max_score": 1\.0/"max_score": $body.hits.max_score/]
|
|
|
-// TESTRESPONSE[s/"hits": \.\.\./"hits": "$body.hits.hits"/]
|
|
|
-
|
|
|
-<1> The total number of hits is unknown.
|
|
|
-
|
|
|
-Given that it is often enough to have a lower bound of the number of hits,
|
|
|
-such as "there are at least 1000 hits", it is also possible to set
|
|
|
-`track_total_hits` as an integer that represents the number of hits to count
|
|
|
-accurately. The search can efficiently skip non-competitive document as soon
|
|
|
-as collecting at least $`track_total_hits` documents. This is a good trade
|
|
|
-off to speed up searches if you don't need the accurate number of hits after
|
|
|
-a certain threshold.
|
|
|
-
|
|
|
-
|
|
|
-For instance the following query will track the total hit count that match
|
|
|
-the query accurately up to 100 documents:
|
|
|
+It is also possible to set `track_total_hits` to an integer.
|
|
|
+For instance the following query will accurately track the total hit count that match
|
|
|
+the query up to 100 documents:
|
|
|
|
|
|
[source,js]
|
|
|
--------------------------------------------------
|
|
@@ -118,8 +81,8 @@ GET twitter/_search
|
|
|
// TEST[continued]
|
|
|
|
|
|
The `hits.total.relation` in the response will indicate if the
|
|
|
-value returned in `hits.total.value` is accurate (`eq`) or a lower
|
|
|
-bound of the total (`gte`).
|
|
|
+value returned in `hits.total.value` is accurate (`"eq"`) or a lower
|
|
|
+bound of the total (`"gte"`).
|
|
|
|
|
|
For instance the following response:
|
|
|
|
|
@@ -173,4 +136,46 @@ will indicate that the returned value is a lower bound:
|
|
|
// TEST[skip:response is already tested in the previous snippet]
|
|
|
|
|
|
<1> There are at least 100 documents that match the query
|
|
|
-<2> This is a lower bound (`gte`).
|
|
|
+<2> This is a lower bound (`"gte"`).
|
|
|
+
|
|
|
+If you don't need to track the total number of hits at all you can improve query
|
|
|
+times by setting this option to `false`:
|
|
|
+
|
|
|
+[source,js]
|
|
|
+--------------------------------------------------
|
|
|
+GET twitter/_search
|
|
|
+{
|
|
|
+ "track_total_hits": false,
|
|
|
+ "query": {
|
|
|
+ "match" : {
|
|
|
+ "message" : "Elasticsearch"
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+--------------------------------------------------
|
|
|
+// CONSOLE
|
|
|
+// TEST[continued]
|
|
|
+
|
|
|
+\... returns:
|
|
|
+
|
|
|
+[source,js]
|
|
|
+--------------------------------------------------
|
|
|
+{
|
|
|
+ "_shards": ...
|
|
|
+ "timed_out": false,
|
|
|
+ "took": 10,
|
|
|
+ "hits" : { <1>
|
|
|
+ "max_score": 1.0,
|
|
|
+ "hits": ...
|
|
|
+ }
|
|
|
+}
|
|
|
+--------------------------------------------------
|
|
|
+// TESTRESPONSE[s/"_shards": \.\.\./"_shards": "$body._shards",/]
|
|
|
+// TESTRESPONSE[s/"took": 10/"took": $body.took/]
|
|
|
+// TESTRESPONSE[s/"max_score": 1\.0/"max_score": $body.hits.max_score/]
|
|
|
+// TESTRESPONSE[s/"hits": \.\.\./"hits": "$body.hits.hits"/]
|
|
|
+
|
|
|
+<1> The total number of hits is unknown.
|
|
|
+
|
|
|
+Finally you can force an accurate count by setting `"track_total_hits"`
|
|
|
+to `true` in the request.
|