From 76e5416322bfabe3d70c83458d8c01b47cca347d Mon Sep 17 00:00:00 2001 From: Denis Novac Date: Wed, 9 Jun 2021 10:21:08 +0500 Subject: [PATCH] Multiple patterns in CreateIndexTemplateRequest (#2446) * Multiple patterns in CreateIndexTemplateRequest * fix tests * createIndexTemplate with one pattern --- .../scala/com/sksamuel/elastic4s/api/IndexTemplateApi.scala | 4 +++- .../requests/index/CreateIndexTemplateRequestTest.scala | 4 ++-- .../requests/indexes/CreateIndexTemplateRequest.scala | 5 +++-- .../elastic4s/handlers/index/IndexTemplateHandlers.scala | 2 +- .../elastic4s/requests/admin/IndexTemplateHttpTest.scala | 2 +- 5 files changed, 10 insertions(+), 7 deletions(-) diff --git a/elastic4s-core/src/main/scala/com/sksamuel/elastic4s/api/IndexTemplateApi.scala b/elastic4s-core/src/main/scala/com/sksamuel/elastic4s/api/IndexTemplateApi.scala index 8da0dd979..d2e4e9b9e 100644 --- a/elastic4s-core/src/main/scala/com/sksamuel/elastic4s/api/IndexTemplateApi.scala +++ b/elastic4s-core/src/main/scala/com/sksamuel/elastic4s/api/IndexTemplateApi.scala @@ -4,7 +4,9 @@ import com.sksamuel.elastic4s.requests.indexes.{CreateIndexTemplateRequest, Dele trait IndexTemplateApi { def deleteIndexTemplate(name: String): DeleteIndexTemplateRequest = DeleteIndexTemplateRequest(name) - def createIndexTemplate(name: String, pattern: String): CreateIndexTemplateRequest = + def createIndexTemplate(name: String, pattern: Seq[String]): CreateIndexTemplateRequest = CreateIndexTemplateRequest(name, pattern) + def createIndexTemplate(name: String, pattern: String): CreateIndexTemplateRequest = + CreateIndexTemplateRequest(name, Seq(pattern)) def getIndexTemplate(name: String): GetIndexTemplateRequest = GetIndexTemplateRequest(name) } diff --git a/elastic4s-core/src/test/scala/com/sksamuel/elastic4s/requests/index/CreateIndexTemplateRequestTest.scala b/elastic4s-core/src/test/scala/com/sksamuel/elastic4s/requests/index/CreateIndexTemplateRequestTest.scala index 969370c61..94b295d03 100644 --- a/elastic4s-core/src/test/scala/com/sksamuel/elastic4s/requests/index/CreateIndexTemplateRequestTest.scala +++ b/elastic4s-core/src/test/scala/com/sksamuel/elastic4s/requests/index/CreateIndexTemplateRequestTest.scala @@ -13,7 +13,7 @@ class CreateIndexTemplateRequestTest extends AnyFunSuite with ElasticDsl with Ma val templateName = "test_template" - val templateDef = createIndexTemplate(templateName, "index_pattern") + val templateDef = createIndexTemplate(templateName, Seq("index_pattern")) .analysis(Analysis(Nil, normalizers = List(lowerCaseNormalizer))) val req = CreateIndexTemplateHandler.build(templateDef) @@ -25,7 +25,7 @@ class CreateIndexTemplateRequestTest extends AnyFunSuite with ElasticDsl with Ma val templateName = "test_template_without_analysis" - val templateDef = createIndexTemplate(templateName, "index_pattern").settings(Map("number_of_shards" -> 1)) + val templateDef = createIndexTemplate(templateName, Seq("index_pattern")).settings(Map("number_of_shards" -> 1)) val expectedEntityContent = """""" diff --git a/elastic4s-domain/src/main/scala/com/sksamuel/elastic4s/requests/indexes/CreateIndexTemplateRequest.scala b/elastic4s-domain/src/main/scala/com/sksamuel/elastic4s/requests/indexes/CreateIndexTemplateRequest.scala index a846e49d8..bcd4131c6 100644 --- a/elastic4s-domain/src/main/scala/com/sksamuel/elastic4s/requests/indexes/CreateIndexTemplateRequest.scala +++ b/elastic4s-domain/src/main/scala/com/sksamuel/elastic4s/requests/indexes/CreateIndexTemplateRequest.scala @@ -5,7 +5,7 @@ import com.sksamuel.elastic4s.requests.mappings.MappingDefinition import com.sksamuel.exts.OptionImplicits.RichOptionImplicits case class CreateIndexTemplateRequest(name: String, - pattern: String, + pattern: Seq[String], settings: Map[String, Any] = Map.empty, mappings: Seq[MappingDefinition] = Nil, @deprecated("use the new analysis package", "7.0.1") @@ -17,7 +17,8 @@ case class CreateIndexTemplateRequest(name: String, priority: Option[Int] = None, aliases: Seq[TemplateAlias] = Nil) { require(name.nonEmpty, "template name must not be null or empty") - require(pattern.nonEmpty, "pattern must not be null or empty") + require(pattern.nonEmpty, "pattern list must not be null or empty") + require(!pattern.exists(_.isEmpty), "patterns must not be null or empty") @deprecated("use new analysis package", "7.2.0") def analysis(first: AnalyzerDefinition, rest: AnalyzerDefinition*): CreateIndexTemplateRequest = diff --git a/elastic4s-handlers/src/main/scala/com/sksamuel/elastic4s/handlers/index/IndexTemplateHandlers.scala b/elastic4s-handlers/src/main/scala/com/sksamuel/elastic4s/handlers/index/IndexTemplateHandlers.scala index 9d5c8773c..5e8e06473 100644 --- a/elastic4s-handlers/src/main/scala/com/sksamuel/elastic4s/handlers/index/IndexTemplateHandlers.scala +++ b/elastic4s-handlers/src/main/scala/com/sksamuel/elastic4s/handlers/index/IndexTemplateHandlers.scala @@ -77,7 +77,7 @@ object CreateIndexTemplateBodyFn { def apply(create: CreateIndexTemplateRequest): XContentBuilder = { val builder = XContentFactory.jsonBuilder() - builder.array("index_patterns", Array(create.pattern)) + builder.array("index_patterns", create.pattern.toArray) create.order.foreach(builder.field("order", _)) create.version.foreach(builder.field("version", _)) create.priority.foreach(builder.field("priority", _)) diff --git a/elastic4s-tests/src/test/scala/com/sksamuel/elastic4s/requests/admin/IndexTemplateHttpTest.scala b/elastic4s-tests/src/test/scala/com/sksamuel/elastic4s/requests/admin/IndexTemplateHttpTest.scala index b5adf7e57..25a3152ef 100644 --- a/elastic4s-tests/src/test/scala/com/sksamuel/elastic4s/requests/admin/IndexTemplateHttpTest.scala +++ b/elastic4s-tests/src/test/scala/com/sksamuel/elastic4s/requests/admin/IndexTemplateHttpTest.scala @@ -35,7 +35,7 @@ class IndexTemplateHttpTest "create template" in { val result = client.execute { - CreateIndexTemplateRequest("brewery_template", "brew*").mappings( + CreateIndexTemplateRequest("brewery_template", Seq("brew*")).mappings( properties( textField("name").boost(123), doubleField("year_founded").ignoreMalformed(true)