Skip to content

Commit

Permalink
https://github.com/elastic/elasticsearch/issues/6299
Browse files Browse the repository at this point in the history
was added pagination to top_hits aggregation
  • Loading branch information
Timur Bayurov committed Jun 4, 2018
1 parent 4cecaf6 commit e1b39d7
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ case class TopHitsAggregationDefinition(name: String,
explain: Option[Boolean] = None,
fetchSource: Option[FetchSourceContext] = None,
size: Option[Int] = None,
from: Option[Int] = None,
sorts: Seq[SortDefinition] = Nil,
trackScores: Option[Boolean] = None,
version: Option[Boolean] = None,
Expand All @@ -30,6 +31,8 @@ case class TopHitsAggregationDefinition(name: String,

def size(size: Int): TopHitsAggregationDefinition = copy(size = size.some)

def from(from: Int): TopHitsAggregationDefinition = copy(from = from.some)

def sortBy(first: SortDefinition, rest: SortDefinition*): TopHitsAggregationDefinition = sortBy(first +: rest)
def sortBy(sorts: Iterable[SortDefinition]): TopHitsAggregationDefinition = copy(sorts = sorts.toSeq)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ object TopHitsAggregationBuilder {
val builder = XContentFactory.jsonBuilder().startObject("top_hits")

agg.size.foreach(builder.field("size", _))
agg.from.foreach(builder.field("from", _))
if (agg.sorts.nonEmpty) {
builder.startArray("sort")
agg.sorts.foreach { sort =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ class TopHitsAggregationBuilderTest extends FunSuite with Matchers {
test("top hits aggregation should generate expected json") {
val q = TopHitsAggregationDefinition("top_items")
.size(5)
.from(10)
.version(true)
.explain(false)
.sortBy(List(FieldSortDefinition("price").sortMode(SortMode.Median)))
TopHitsAggregationBuilder(q).string() shouldBe
"""{"top_hits":{"size":5,"sort":[{"price":{"mode":"median","order":"asc"}}],"explain":false,"version":true}}"""
"""{"top_hits":{"size":5,"from":10,"sort":[{"price":{"mode":"median","order":"asc"}}],"explain":false,"version":true}}"""
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ object TopHitsAggregationBuilder {
agg.trackScores.foreach(builder.trackScores)
agg.version.foreach(builder.version)
agg.size.foreach(builder.size)
agg.from.foreach(builder.from)
agg.storedFields.foreach(builder.storedField)

agg.scripts.foreach {
Expand Down

0 comments on commit e1b39d7

Please sign in to comment.