-
Notifications
You must be signed in to change notification settings - Fork 25.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add the ability to set the number of hits to track accurately #36357
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
de4dcdc
Add the ability to set the number of hits to track accurately
jimczi 0ff9521
fix wrong assignement when track_total_hits is disabled
jimczi 61c176d
update docs
jimczi d070f1a
fix trackTotalHitsUpTo propagation
jimczi 4582c6f
address review
jimczi 36870f7
address review bis
jimczi b775ddc
optimize the logic for track total hits on scroll queries
jimczi ea50aab
LUCENE-8592: Fix index sorting corruption due to numeric overflow
jimczi 6287563
fix docs
jimczi 60eccb3
Merge branch 'master' into track_total_hits_lower_bound
jimczi f54276e
Merge branch 'master' into track_total_hits_lower_bound
jimczi 67f1dea
fix ut
jimczi e865f8d
Merge branch 'master' into track_total_hits_lower_bound
jimczi d1de0fa
Merge branch 'master' into track_total_hits_lower_bound
jimczi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
176 changes: 176 additions & 0 deletions
176
docs/reference/search/request/track-total-hits.asciidoc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,176 @@ | ||
[[search-request-track-total-hits]] | ||
=== Track total hits | ||
|
||
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). | ||
|
||
[source,js] | ||
-------------------------------------------------- | ||
GET twitter/_search | ||
{ | ||
"track_total_hits": true, | ||
"query": { | ||
"match" : { | ||
"message" : "Elasticsearch" | ||
} | ||
} | ||
} | ||
-------------------------------------------------- | ||
// TEST[setup:twitter] | ||
// CONSOLE | ||
|
||
\... returns: | ||
|
||
[source,js] | ||
-------------------------------------------------- | ||
{ | ||
"_shards": ... | ||
"timed_out": false, | ||
"took": 100, | ||
"hits": { | ||
"max_score": 1.0, | ||
"total" : { | ||
"value": 2048, <1> | ||
"relation": "eq" <2> | ||
}, | ||
"hits": ... | ||
} | ||
} | ||
-------------------------------------------------- | ||
// TESTRESPONSE[s/"_shards": \.\.\./"_shards": "$body._shards",/] | ||
// TESTRESPONSE[s/"took": 100/"took": $body.took/] | ||
// TESTRESPONSE[s/"max_score": 1\.0/"max_score": $body.hits.max_score/] | ||
// TESTRESPONSE[s/"value": 2048/"value": $body.hits.total.value/] | ||
// TESTRESPONSE[s/"hits": \.\.\./"hits": "$body.hits.hits"/] | ||
|
||
<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: | ||
|
||
[source,js] | ||
-------------------------------------------------- | ||
GET twitter/_search | ||
{ | ||
"track_total_hits": 100, | ||
"query": { | ||
"match" : { | ||
"message" : "Elasticsearch" | ||
} | ||
} | ||
} | ||
-------------------------------------------------- | ||
// CONSOLE | ||
// 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`). | ||
|
||
For instance the following response: | ||
|
||
[source,js] | ||
-------------------------------------------------- | ||
{ | ||
"_shards": ... | ||
"timed_out": false, | ||
"took": 30, | ||
"hits" : { | ||
"max_score": 1.0, | ||
"total" : { | ||
"value": 42, <1> | ||
"relation": "eq" <2> | ||
}, | ||
"hits": ... | ||
} | ||
} | ||
-------------------------------------------------- | ||
// TESTRESPONSE[s/"_shards": \.\.\./"_shards": "$body._shards",/] | ||
// TESTRESPONSE[s/"took": 30/"took": $body.took/] | ||
// TESTRESPONSE[s/"max_score": 1\.0/"max_score": $body.hits.max_score/] | ||
// TESTRESPONSE[s/"value": 42/"value": $body.hits.total.value/] | ||
// TESTRESPONSE[s/"hits": \.\.\./"hits": "$body.hits.hits"/] | ||
|
||
<1> 42 documents match the query | ||
<2> and the count is accurate (`"eq"`) | ||
|
||
\... indicates that the number of hits returned in the `total` | ||
is accurate. | ||
|
||
If the total number of his that match the query is greater than the | ||
value set in `track_total_hits`, the total hits in the response | ||
will indicate that the returned value is a lower bound: | ||
|
||
[source,js] | ||
-------------------------------------------------- | ||
{ | ||
"_shards": ... | ||
"hits" : { | ||
"max_score": 1.0, | ||
"total" : { | ||
"value": 100, <1> | ||
"relation": "gte" <2> | ||
}, | ||
"hits": ... | ||
} | ||
} | ||
-------------------------------------------------- | ||
// TESTRESPONSE | ||
// 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`). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jimczi , this is problematic in strongly-typed languages, since the
track_total_hits
parameter is defined as aboolean
, and eg. the Go test suite fails withcannot use 4 (type int) as type bool in field value
:elasticsearch/rest-api-spec/src/main/resources/rest-api-spec/test/search/220_total_hits_object.yml
Line 117 in b34e7d4
I understand the motivation here, but we should at least revisit the parameter definitions, and support something like
"type" : ["boolean","number]"
, so the code generators can make decisions here.