-
Notifications
You must be signed in to change notification settings - Fork 25k
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 top_hits aggregation #6124
Closed
Closed
Add top_hits aggregation #6124
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
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
147 changes: 147 additions & 0 deletions
147
docs/reference/search/aggregations/bucket/tophits-aggregations.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,147 @@ | ||
[[search-aggregations-bucket-top-hits-aggregation]] | ||
=== Top hits Aggregation | ||
|
||
coming[1.3.0] | ||
|
||
The `top_hits` aggregator keeps track of the most relevant document being aggregated. This aggregator is intended to be | ||
used as a sub aggregator, so that the top matching documents can be aggregated per bucket. | ||
|
||
The `top_hits` aggregator can effectively be used to group result sets by certain fields via a bucket aggregator. | ||
One or more bucket aggregators determines by which properties a result set get sliced into. | ||
|
||
.Options: | ||
* `size` - The maximum number of top matching hits to return per bucket. By default the top three matching hits are returned. | ||
* `sort` - How the top matching hits should be sorted. By default the hits are sorted by the score of the main query. | ||
|
||
.Supported per hit features | ||
|
||
The top_hits aggregation returns regular search hits, because of this many per hit features can be supported: | ||
* {ref}/search-request-highlighting.html[Highlighting] | ||
* {ref}/search-request-explain.html[Explain] | ||
* {ref}/search-request-named-queries-and-filters.html[Named filters and queries] | ||
* {ref}/search-request-source-filtering.html[Source filtering] | ||
* {ref}/search-request-script-fields.html[Script fields] | ||
* {ref}/search-request-fielddata-fields.html[Fielddata fields] | ||
* {ref}/search-request-version.html[Include versions] | ||
|
||
.Example | ||
|
||
In the following example we group the questions by tag and per tag we show the last active question. For each question | ||
only the title field is being included in the source. | ||
|
||
[source,js] | ||
-------------------------------------------------- | ||
{ | ||
"aggs": { | ||
"terms": { | ||
"top-tags": { | ||
"field": "tags", | ||
"size": 3 | ||
}, | ||
"aggs": { | ||
"top_tag_hits": { | ||
"top_hits": { | ||
"sort": [ | ||
{ | ||
"last_activity_date": { | ||
"order": "desc" | ||
} | ||
} | ||
], | ||
"_source": { | ||
"include": [ | ||
"title" | ||
] | ||
}, | ||
"size" : 1 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is the source filtering missing here? |
||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
-------------------------------------------------- | ||
|
||
Possible response snippet: | ||
|
||
[source,js] | ||
-------------------------------------------------- | ||
"aggregations": { | ||
"top-tags": { | ||
"buckets": [ | ||
{ | ||
"key": "windows-7", | ||
"doc_count": 25365, | ||
"top_tags_hits": { | ||
"hits": { | ||
"total": 25365, | ||
"max_score": 1, | ||
"hits": [ | ||
{ | ||
"_index": "stack", | ||
"_type": "question", | ||
"_id": "602679", | ||
"_score": 1, | ||
"_source": { | ||
"title": "Windows port opening" | ||
}, | ||
"sort": [ | ||
1370143231177 | ||
] | ||
} | ||
] | ||
} | ||
} | ||
}, | ||
{ | ||
"key": "linux", | ||
"doc_count": 18342, | ||
"top_tags_hits": { | ||
"hits": { | ||
"total": 18342, | ||
"max_score": 1, | ||
"hits": [ | ||
{ | ||
"_index": "stack", | ||
"_type": "question", | ||
"_id": "602672", | ||
"_score": 1, | ||
"_source": { | ||
"title": "Ubuntu RFID Screensaver lock-unlock" | ||
}, | ||
"sort": [ | ||
1370143379747 | ||
] | ||
} | ||
] | ||
} | ||
} | ||
}, | ||
{ | ||
"key": "windows", | ||
"doc_count": 18119, | ||
"top_tags_hits": { | ||
"hits": { | ||
"total": 18119, | ||
"max_score": 1, | ||
"hits": [ | ||
{ | ||
"_index": "stack", | ||
"_type": "question", | ||
"_id": "602678", | ||
"_score": 1, | ||
"_source": { | ||
"title": "If I change my computers date / time, what could be affected?" | ||
}, | ||
"sort": [ | ||
1370142868283 | ||
] | ||
} | ||
] | ||
} | ||
} | ||
} | ||
] | ||
} | ||
} | ||
-------------------------------------------------- |
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
149 changes: 149 additions & 0 deletions
149
src/main/java/org/elasticsearch/search/aggregations/bucket/tophits/InternalTopHits.java
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,149 @@ | ||
/* | ||
* Licensed to Elasticsearch under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
package org.elasticsearch.search.aggregations.bucket.tophits; | ||
|
||
import org.apache.lucene.search.*; | ||
import org.elasticsearch.ExceptionsHelper; | ||
import org.elasticsearch.common.io.stream.StreamInput; | ||
import org.elasticsearch.common.io.stream.StreamOutput; | ||
import org.elasticsearch.common.io.stream.Streamable; | ||
import org.elasticsearch.common.lucene.Lucene; | ||
import org.elasticsearch.common.xcontent.ToXContent; | ||
import org.elasticsearch.common.xcontent.XContentBuilder; | ||
import org.elasticsearch.search.SearchHits; | ||
import org.elasticsearch.search.aggregations.AggregationStreams; | ||
import org.elasticsearch.search.aggregations.InternalAggregation; | ||
import org.elasticsearch.search.internal.InternalSearchHit; | ||
import org.elasticsearch.search.internal.InternalSearchHits; | ||
|
||
import java.io.IOException; | ||
import java.util.List; | ||
|
||
/** | ||
*/ | ||
public class InternalTopHits extends InternalAggregation implements TopHits, ToXContent, Streamable { | ||
|
||
public static final InternalAggregation.Type TYPE = new Type("top_hits"); | ||
|
||
public static final AggregationStreams.Stream STREAM = new AggregationStreams.Stream() { | ||
@Override | ||
public InternalTopHits readResult(StreamInput in) throws IOException { | ||
InternalTopHits buckets = new InternalTopHits(); | ||
buckets.readFrom(in); | ||
return buckets; | ||
} | ||
}; | ||
|
||
public static void registerStreams() { | ||
AggregationStreams.registerStream(STREAM, TYPE.stream()); | ||
} | ||
|
||
private int size; | ||
private Sort sort; | ||
private TopDocs topDocs; | ||
private InternalSearchHits searchHits; | ||
|
||
InternalTopHits() { | ||
} | ||
|
||
public InternalTopHits(String name, int size, Sort sort, TopDocs topDocs, InternalSearchHits searchHits) { | ||
this.name = name; | ||
this.size = size; | ||
this.sort = sort; | ||
this.topDocs = topDocs; | ||
this.searchHits = searchHits; | ||
} | ||
|
||
public InternalTopHits(String name, InternalSearchHits searchHits) { | ||
this.name = name; | ||
this.searchHits = searchHits; | ||
this.topDocs = new TopDocs(0, Lucene.EMPTY_SCORE_DOCS, 0); | ||
} | ||
|
||
|
||
@Override | ||
public Type type() { | ||
return TYPE; | ||
} | ||
|
||
@Override | ||
public SearchHits getHits() { | ||
return searchHits; | ||
} | ||
|
||
@Override | ||
public InternalAggregation reduce(ReduceContext reduceContext) { | ||
List<InternalAggregation> aggregations = reduceContext.aggregations(); | ||
if (aggregations.size() == 1) { | ||
return aggregations.get(0); | ||
} | ||
|
||
TopDocs[] shardDocs = new TopDocs[aggregations.size()]; | ||
InternalSearchHits[] shardHits = new InternalSearchHits[aggregations.size()]; | ||
for (int i = 0; i < shardDocs.length; i++) { | ||
InternalTopHits topHitsAgg = (InternalTopHits) aggregations.get(i); | ||
shardDocs[i] = topHitsAgg.topDocs; | ||
shardHits[i] = topHitsAgg.searchHits; | ||
} | ||
|
||
try { | ||
int[] tracker = new int[shardHits.length]; | ||
TopDocs reducedTopDocs = TopDocs.merge(sort, size, shardDocs); | ||
InternalSearchHit[] hits = new InternalSearchHit[reducedTopDocs.scoreDocs.length]; | ||
for (int i = 0; i < reducedTopDocs.scoreDocs.length; i++) { | ||
ScoreDoc scoreDoc = reducedTopDocs.scoreDocs[i]; | ||
hits[i] = (InternalSearchHit) shardHits[scoreDoc.shardIndex].getAt(tracker[scoreDoc.shardIndex]++); | ||
if (scoreDoc instanceof FieldDoc) { | ||
FieldDoc fieldDoc = (FieldDoc) scoreDoc; | ||
hits[i].sortValues(fieldDoc.fields); | ||
} | ||
} | ||
return new InternalTopHits(name, new InternalSearchHits(hits, reducedTopDocs.totalHits, reducedTopDocs.getMaxScore())); | ||
} catch (IOException e) { | ||
throw ExceptionsHelper.convertToElastic(e); | ||
} | ||
} | ||
|
||
@Override | ||
public void readFrom(StreamInput in) throws IOException { | ||
name = in.readString(); | ||
size = in.readVInt(); | ||
topDocs = Lucene.readTopDocs(in); | ||
if (topDocs instanceof TopFieldDocs) { | ||
sort = new Sort(((TopFieldDocs) topDocs).fields); | ||
} | ||
searchHits = InternalSearchHits.readSearchHits(in); | ||
} | ||
|
||
@Override | ||
public void writeTo(StreamOutput out) throws IOException { | ||
out.writeString(name); | ||
out.writeVInt(size); | ||
Lucene.writeTopDocs(out, topDocs, 0); | ||
searchHits.writeTo(out); | ||
} | ||
|
||
@Override | ||
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { | ||
builder.startObject(name); | ||
searchHits.toXContent(builder, params); | ||
builder.endObject(); | ||
return builder; | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
src/main/java/org/elasticsearch/search/aggregations/bucket/tophits/TopHits.java
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,30 @@ | ||
/* | ||
* Licensed to Elasticsearch under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
package org.elasticsearch.search.aggregations.bucket.tophits; | ||
|
||
import org.elasticsearch.search.SearchHits; | ||
import org.elasticsearch.search.aggregations.Aggregation; | ||
|
||
/** | ||
*/ | ||
public interface TopHits extends Aggregation { | ||
|
||
SearchHits getHits(); | ||
|
||
} |
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.
nice :)