Skip to content

Commit 6287563

Browse files
committed
fix docs
1 parent ea50aab commit 6287563

File tree

4 files changed

+66
-47
lines changed

4 files changed

+66
-47
lines changed

docs/reference/search/request/track-total-hits.asciidoc

Lines changed: 40 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,17 @@ be equal to `"eq"` when `track_total_hits is set to true).
1010

1111
[source,js]
1212
--------------------------------------------------
13-
GET /_search
13+
GET twitter/_search
1414
{
1515
"track_total_hits": true,
16-
"query": {
17-
"term": {
18-
"title": "fast"
16+
"query": {
17+
"match" : {
18+
"message" : "Elasticsearch"
1919
}
20-
}
20+
}
2121
}
2222
--------------------------------------------------
23+
// TEST[setup:twitter]
2324
// CONSOLE
2425

2526
\... returns:
@@ -28,19 +29,23 @@ GET /_search
2829
--------------------------------------------------
2930
{
3031
"_shards": ...
31-
"hits" : {
32+
"timed_out": false,
33+
"took": 100,
34+
"hits": {
35+
"max_score": 1.0,
3236
"total" : {
3337
"value": 2048, <1>
3438
"relation": "eq" <2>
3539
},
36-
"max_score" : 1.0,
37-
"hits" : []
40+
"hits": ...
3841
}
3942
}
4043
--------------------------------------------------
4144
// TESTRESPONSE[s/"_shards": \.\.\./"_shards": "$body._shards",/]
45+
// TESTRESPONSE[s/"took": 100/"took": $body.took/]
4246
// TESTRESPONSE[s/"max_score": 1\.0/"max_score": $body.hits.max_score/]
43-
// TESTRESPONSE[s/"value": 2048/"total": $body.hits.total.value/]
47+
// TESTRESPONSE[s/"value": 2048/"value": $body.hits.total.value/]
48+
// TESTRESPONSE[s/"hits": \.\.\./"hits": "$body.hits.hits"/]
4449

4550
<1> The total number of hits that match the query.
4651
<2> The count is accurate (e.g. `"eq"` means equals).
@@ -51,32 +56,37 @@ non-competitive hits because it doesn't need to count all matches:
5156

5257
[source,js]
5358
--------------------------------------------------
54-
GET /_search
59+
GET twitter/_search
5560
{
5661
"track_total_hits": false,
57-
"query": {
58-
"term": {
59-
"title": "fast"
62+
"query": {
63+
"match" : {
64+
"message" : "Elasticsearch"
6065
}
61-
}
66+
}
6267
}
6368
--------------------------------------------------
6469
// CONSOLE
70+
// TEST[continued]
6571

6672
\... returns:
6773

6874
[source,js]
6975
--------------------------------------------------
7076
{
7177
"_shards": ...
78+
"timed_out": false,
79+
"took": 10,
7280
"hits" : { <1>
73-
"max_score" : 1.0,
74-
"hits" : []
81+
"max_score": 1.0,
82+
"hits": ...
7583
}
7684
}
7785
--------------------------------------------------
7886
// TESTRESPONSE[s/"_shards": \.\.\./"_shards": "$body._shards",/]
87+
// TESTRESPONSE[s/"took": 10/"took": $body.took/]
7988
// TESTRESPONSE[s/"max_score": 1\.0/"max_score": $body.hits.max_score/]
89+
// TESTRESPONSE[s/"hits": \.\.\./"hits": "$body.hits.hits"/]
8090

8191
<1> The total number of hits is unknown.
8292

@@ -94,17 +104,18 @@ the query accurately up to 100 documents:
94104

95105
[source,js]
96106
--------------------------------------------------
97-
GET /_search
107+
GET twitter/_search
98108
{
99109
"track_total_hits": 100,
100-
"query": {
101-
"term": {
102-
"title": "fast"
110+
"query": {
111+
"match" : {
112+
"message" : "Elasticsearch"
103113
}
104-
}
114+
}
105115
}
106116
--------------------------------------------------
107117
// CONSOLE
118+
// TEST[continued]
108119

109120
The `hits.total.relation` in the response will indicate if the
110121
value returned in `hits.total.value` is accurate (`eq`) or a lower
@@ -116,19 +127,23 @@ For instance the following response:
116127
--------------------------------------------------
117128
{
118129
"_shards": ...
130+
"timed_out": false,
131+
"took": 30,
119132
"hits" : {
133+
"max_score": 1.0,
120134
"total" : {
121135
"value": 42, <1>
122136
"relation": "eq" <2>
123137
},
124-
"max_score" : 1.0,
125-
"hits" : []
138+
"hits": ...
126139
}
127140
}
128141
--------------------------------------------------
129142
// TESTRESPONSE[s/"_shards": \.\.\./"_shards": "$body._shards",/]
143+
// TESTRESPONSE[s/"took": 30/"took": $body.took/]
130144
// TESTRESPONSE[s/"max_score": 1\.0/"max_score": $body.hits.max_score/]
131145
// TESTRESPONSE[s/"value": 42/"value": $body.hits.total.value/]
146+
// TESTRESPONSE[s/"hits": \.\.\./"hits": "$body.hits.hits"/]
132147

133148
<1> 42 documents match the query
134149
<2> and the count is accurate (`"eq"`)
@@ -145,12 +160,12 @@ will indicate that the returned value is a lower bound:
145160
{
146161
"_shards": ...
147162
"hits" : {
163+
"max_score": 1.0,
148164
"total" : {
149165
"value": 100, <1>
150166
"relation": "gte" <2>
151167
},
152-
"max_score" : 1.0,
153-
"hits" : []
168+
"hits": ...
154169
}
155170
}
156171
--------------------------------------------------

server/src/main/java/org/elasticsearch/action/search/SearchPhaseController.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -738,7 +738,8 @@ InitialSearchPhase.ArraySearchPhaseResults<SearchPhaseResult> newSearchPhaseResu
738738
// no incremental reduce if scroll is used - we only hit a single shard or sometimes more...
739739
if (request.getBatchedReduceSize() < numShards) {
740740
// only use this if there are aggs and if there are more shards than we should reduce at once
741-
return new QueryPhaseResultConsumer(this, numShards, request.getBatchedReduceSize(), hasTopDocs, hasAggs, trackTotalHitsUpTo);
741+
return new QueryPhaseResultConsumer(this, numShards, request.getBatchedReduceSize(),
742+
hasTopDocs, hasAggs, trackTotalHitsUpTo);
742743
}
743744
}
744745
return new InitialSearchPhase.ArraySearchPhaseResults(numShards) {

server/src/main/java/org/elasticsearch/search/SearchHits.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -278,12 +278,17 @@ private static String printRelation(Relation relation) {
278278
private static class Total implements Writeable, ToXContentFragment {
279279
final TotalHits in;
280280

281+
Total(TotalHits in) {
282+
this.in = Objects.requireNonNull(in);
283+
}
284+
281285
Total(StreamInput in) throws IOException {
282286
this.in = Lucene.readTotalHits(in);
283287
}
284288

285-
Total(TotalHits in) {
286-
this.in = Objects.requireNonNull(in);
289+
@Override
290+
public void writeTo(StreamOutput out) throws IOException {
291+
Lucene.writeTotalHits(out, in);
287292
}
288293

289294
@Override
@@ -300,11 +305,6 @@ public int hashCode() {
300305
return Objects.hash(in.value, in.relation);
301306
}
302307

303-
@Override
304-
public void writeTo(StreamOutput out) throws IOException {
305-
Lucene.writeTotalHits(out, in);
306-
}
307-
308308
@Override
309309
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
310310
builder.field("value", in.value);

server/src/main/java/org/elasticsearch/search/query/TopDocsCollectorContext.java

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -189,11 +189,11 @@ private static TopDocsCollector<?> createCollector(@Nullable SortAndFormats sort
189189
}
190190
}
191191

192-
private final @Nullable SortAndFormats sortAndFormats;
193192
private final Collector collector;
194-
private final Supplier<TotalHits> totalHitsSupplier;
195-
private final Supplier<TopDocs> topDocsSupplier;
196-
private final Supplier<Float> maxScoreSupplier;
193+
protected final @Nullable SortAndFormats sortAndFormats;
194+
protected final Supplier<TotalHits> totalHitsSupplier;
195+
protected final Supplier<TopDocs> topDocsSupplier;
196+
protected final Supplier<Float> maxScoreSupplier;
197197

198198
/**
199199
* Ctr
@@ -266,7 +266,8 @@ Collector create(Collector in) {
266266
void postProcess(QuerySearchResult result) throws IOException {
267267
final TopDocs topDocs = topDocsSupplier.get();
268268
topDocs.totalHits = totalHitsSupplier.get();
269-
result.topDocs(new TopDocsAndMaxScore(topDocs, maxScoreSupplier.get()), sortAndFormats == null ? null : sortAndFormats.formats);
269+
result.topDocs(new TopDocsAndMaxScore(topDocs, maxScoreSupplier.get()),
270+
sortAndFormats == null ? null : sortAndFormats.formats);
270271
}
271272
}
272273

@@ -291,26 +292,28 @@ private ScrollingTopDocsCollectorContext(IndexReader reader,
291292

292293
@Override
293294
void postProcess(QuerySearchResult result) throws IOException {
294-
super.postProcess(result);
295-
final TopDocsAndMaxScore topDocs = result.topDocs();
295+
final TopDocs topDocs = topDocsSupplier.get();
296+
topDocs.totalHits = totalHitsSupplier.get();
297+
float maxScore = maxScoreSupplier.get();
296298
if (scrollContext.totalHits == null) {
297299
// first round
298-
scrollContext.totalHits = topDocs.topDocs.totalHits;
299-
scrollContext.maxScore = topDocs.maxScore;
300+
scrollContext.totalHits = topDocs.totalHits;
301+
scrollContext.maxScore = maxScore;
300302
} else {
301303
// subsequent round: the total number of hits and
302304
// the maximum score were computed on the first round
303-
topDocs.topDocs.totalHits = scrollContext.totalHits;
304-
topDocs.maxScore = scrollContext.maxScore;
305+
topDocs.totalHits = scrollContext.totalHits;
306+
maxScore = scrollContext.maxScore;
305307
}
306308
if (numberOfShards == 1) {
307309
// if we fetch the document in the same roundtrip, we already know the last emitted doc
308-
if (topDocs.topDocs.scoreDocs.length > 0) {
310+
if (topDocs.scoreDocs.length > 0) {
309311
// set the last emitted doc
310-
scrollContext.lastEmittedDoc = topDocs.topDocs.scoreDocs[topDocs.topDocs.scoreDocs.length - 1];
312+
scrollContext.lastEmittedDoc = topDocs.scoreDocs[topDocs.scoreDocs.length - 1];
311313
}
312314
}
313-
result.topDocs(topDocs, result.sortValueFormats());
315+
result.topDocs(new TopDocsAndMaxScore(topDocs, maxScore),
316+
sortAndFormats == null ? null : sortAndFormats.formats);
314317
}
315318
}
316319

0 commit comments

Comments
 (0)