diff --git a/elastic4s-core/src/main/scala/com/sksamuel/elastic4s/requests/searches/aggs/TopHitsAggregation.scala b/elastic4s-core/src/main/scala/com/sksamuel/elastic4s/requests/searches/aggs/TopHitsAggregation.scala index 2439f3c8c..df6b8f950 100644 --- a/elastic4s-core/src/main/scala/com/sksamuel/elastic4s/requests/searches/aggs/TopHitsAggregation.scala +++ b/elastic4s-core/src/main/scala/com/sksamuel/elastic4s/requests/searches/aggs/TopHitsAggregation.scala @@ -18,7 +18,8 @@ case class TopHitsAggregation(name: String, storedFields: Seq[String] = Nil, subaggs: Seq[AbstractAggregation] = Nil, metadata: Map[String, AnyRef] = Map.empty, - highlight: Option[Highlight] = None) + highlight: Option[Highlight] = None, + docValueFields: Seq[String] = Nil) extends Aggregation { type T = TopHitsAggregation @@ -42,6 +43,9 @@ case class TopHitsAggregation(name: String, def storedFields(first: String, rest: String*): TopHitsAggregation = storedFields(first +: rest) def storedFields(fields: Iterable[String]): TopHitsAggregation = copy(storedFields = fields.toSeq) + def docValueFields(docValueFields: Iterable[String]): TopHitsAggregation = + copy(docValueFields = docValueFields.toSeq) + def version(version: Boolean): TopHitsAggregation = copy(version = version.some) def trackScores(trackScores: Boolean): TopHitsAggregation = copy(trackScores = trackScores.some) diff --git a/elastic4s-core/src/main/scala/com/sksamuel/elastic4s/requests/searches/aggs/builders/TopHitsAggregationBuilder.scala b/elastic4s-core/src/main/scala/com/sksamuel/elastic4s/requests/searches/aggs/builders/TopHitsAggregationBuilder.scala index db9aee9be..1156b0d8b 100644 --- a/elastic4s-core/src/main/scala/com/sksamuel/elastic4s/requests/searches/aggs/builders/TopHitsAggregationBuilder.scala +++ b/elastic4s-core/src/main/scala/com/sksamuel/elastic4s/requests/searches/aggs/builders/TopHitsAggregationBuilder.scala @@ -27,6 +27,12 @@ object TopHitsAggregationBuilder { agg.explain.foreach(builder.field("explain", _)) + if (agg.storedFields.nonEmpty) + builder.array("stored_fields", agg.storedFields.toArray) + + if (agg.docValueFields.nonEmpty) + builder.array("docvalue_fields", agg.docValueFields.toArray) + agg.highlight.foreach { highlight => builder.rawField("highlight", HighlightBuilderFn(highlight)) } diff --git a/elastic4s-core/src/test/scala/com/sksamuel/elastic4s/requests/searches/TopHitsAggregationBuilderTest.scala b/elastic4s-core/src/test/scala/com/sksamuel/elastic4s/requests/searches/TopHitsAggregationBuilderTest.scala index 63b3ac155..3c01b73aa 100644 --- a/elastic4s-core/src/test/scala/com/sksamuel/elastic4s/requests/searches/TopHitsAggregationBuilderTest.scala +++ b/elastic4s-core/src/test/scala/com/sksamuel/elastic4s/requests/searches/TopHitsAggregationBuilderTest.scala @@ -13,9 +13,11 @@ class TopHitsAggregationBuilderTest extends AnyFunSuite with Matchers { .from(10) .version(true) .explain(false) + .docValueFields(List("name")) + .storedFields(List("currency")) .sortBy(List(FieldSort("price").sortMode(SortMode.Median))) TopHitsAggregationBuilder(q).string shouldBe - """{"top_hits":{"size":5,"from":10,"sort":[{"price":{"mode":"median","order":"asc"}}],"explain":false,"version":true}}""" + """{"top_hits":{"size":5,"from":10,"sort":[{"price":{"mode":"median","order":"asc"}}],"explain":false,"stored_fields":["currency"],"docvalue_fields":["name"],"version":true}}""" } test("top hits aggregation builder should support highlighting") {