Skip to content
This repository was archived by the owner on Oct 18, 2021. It is now read-only.

Commit be1a829

Browse files
authored
Merge pull request #163 from SumoLogic/release-2.0.8.1
Support NestedMapping and MultiFieldsMapping in Release 2.0.8.1
2 parents 4f7f891 + 997c8f7 commit be1a829

File tree

2 files changed

+55
-8
lines changed

2 files changed

+55
-8
lines changed

elasticsearch-core/src/main/scala/com/sumologic/elasticsearch/restlastic/dsl/MappingDsl.scala

+20-5
Original file line numberDiff line numberDiff line change
@@ -142,24 +142,39 @@ trait MappingDsl extends DslCommons {
142142
val _ignoreAbove = "ignore_above"
143143
val _fieldIndexOpions = "index_options"
144144

145-
case class BasicFieldMapping(tpe: FieldType, index: Option[IndexType], analyzer: Option[Name],
146-
ignoreAbove: Option[Int] = None, search_analyzer: Option[Name]= None,
147-
indexOption: Option[IndexOption] = None)
145+
case class BasicFieldMapping(tpe: FieldType,
146+
index: Option[IndexType],
147+
analyzer: Option[Name],
148+
ignoreAbove: Option[Int] = None,
149+
search_analyzer: Option[Name]= None,
150+
indexOption: Option[IndexOption] = None,
151+
fieldsOption: Option[FieldsMapping] = None)
148152
extends FieldMapping {
149153

150154
override def toJson: Map[String, Any] = Map(
151155
_type -> tpe.rep) ++
152156
index.map(_index -> _.rep) ++
153157
analyzer.map(_analyzer -> _.name) ++
154158
search_analyzer.map(_searchAnalyzer -> _.name) ++
155-
indexOption.map(_fieldIndexOpions -> _.option)
156-
ignoreAbove.map(_ignoreAbove -> _).toList.toMap
159+
indexOption.map(_fieldIndexOpions -> _.option) ++
160+
ignoreAbove.map(_ignoreAbove -> _) ++
161+
fieldsOption.map(_.toJson).getOrElse(Map[String, Any]())
162+
}
163+
164+
case class FieldsMapping(fields: Map[String, FieldMapping]) extends FieldMapping {
165+
val _fields = "fields"
166+
override def toJson: Map[String, Any] = Map(_fields -> fields.mapValues(_.toJson))
157167
}
158168

159169
case class BasicObjectMapping(fields: Map[String, FieldMapping]) extends FieldMapping {
160170
override def toJson: Map[String, Any] = Map(_properties -> fields.mapValues(_.toJson))
161171
}
162172

173+
case class NestedObjectMapping(fields: Map[String, FieldMapping]) extends FieldMapping {
174+
val _nested = "nested"
175+
override def toJson: Map[String, Any] = Map(_type -> _nested, _properties -> fields.mapValues(_.toJson))
176+
}
177+
163178
trait Completion {
164179
val _type = "type" -> "completion"
165180
val _context = "context"

elasticsearch-core/src/test/scala/com/sumologic/elasticsearch/restlastic/RestlasticSearchClientTest.scala

+35-3
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ class RestlasticSearchClientTest extends WordSpec with Matchers with ScalaFuture
121121
val foundDoc: ElasticJsonDocument = whenReady(resFut){ res =>
122122
res.rawSearchResponse.hits.hits.head
123123
}
124-
124+
125125
val delFut = restClient.deleteById(index, tpe, foundDoc._id)
126126

127127
whenReady(delFut) { res =>
@@ -195,15 +195,15 @@ class RestlasticSearchClientTest extends WordSpec with Matchers with ScalaFuture
195195
res.jsonStr should include("doc4")
196196
res.jsonStr should not include "doc5"
197197
}
198-
198+
199199
val delFut = restClient.bulkDelete(index, tpe, Seq(doc3, doc4, doc5))
200200
whenReady(delFut){ resp =>
201201
resp.length should be(3)
202202
resp.head.success should be(true)
203203
resp(1).success should be(true)
204204
resp(2).success should be(true)
205205
}
206-
206+
207207
refresh()
208208
val resFut2 = restClient.query(index, tpe, new QueryRoot(TermQuery("text", "here")))
209209
whenReady(resFut2) { res =>
@@ -1109,6 +1109,38 @@ class RestlasticSearchClientTest extends WordSpec with Matchers with ScalaFuture
11091109
Await.result(delFut, 10.seconds) // May not need Await?
11101110
}
11111111

1112+
"Support nested mapping" in {
1113+
val basicFieldMapping = BasicFieldMapping(StringType, None, Some(analyzerName), ignoreAbove = Some(10000), Some(analyzerName))
1114+
val metadataMapping = Mapping(tpe,
1115+
IndexMapping(
1116+
Map("name" -> basicFieldMapping,
1117+
"kv" -> NestedObjectMapping(Map("key" -> basicFieldMapping, "val" -> basicFieldMapping))
1118+
)
1119+
)
1120+
)
1121+
1122+
val mappingFut = restClient.putMapping(index, tpe, metadataMapping)
1123+
whenReady(mappingFut) { _ => refresh() }
1124+
val mappingRes = restClient.getMapping(index, tpe)
1125+
val expected = """"kv":{"type":"nested","properties":{"key":{"type":"string","analyzer":"keyword_lowercase","ignore_above":10000},"val":{"type":"string","analyzer":"keyword_lowercase","ignore_above":10000}}}"""
1126+
mappingRes.futureValue.jsonStr.toString.contains(expected) should be(true)
1127+
}
1128+
1129+
"Support multi-fields mapping" in {
1130+
val fields = FieldsMapping(Map("raw" -> BasicFieldMapping(StringType, None, Some(analyzerName), ignoreAbove = Some(10000), Some(analyzerName))))
1131+
val basicFieldMapping = BasicFieldMapping(StringType, None, Some(analyzerName), ignoreAbove = Some(10000), Some(analyzerName), fieldsOption = Some(fields))
1132+
val metadataMapping = Mapping(tpe,
1133+
IndexMapping(
1134+
Map("multi-fields" -> basicFieldMapping)
1135+
)
1136+
)
1137+
val mappingFut = restClient.putMapping(index, tpe, metadataMapping)
1138+
whenReady(mappingFut) { _ => refresh() }
1139+
val mappingRes = restClient.getMapping(index, tpe)
1140+
val expected = """{"multi-fields":{"type":"string","fields":{"raw":{"type":"string","analyzer":"keyword_lowercase","ignore_above":10000}},"analyzer":"keyword_lowercase","ignore_above":10000}}"""
1141+
mappingRes.futureValue.jsonStr.toString.contains(expected) should be(true)
1142+
}
1143+
11121144
def indexDocs(docs: Seq[Document]): Unit = {
11131145
val bulkIndexFuture = restClient.bulkIndex(index, tpe, docs)
11141146
whenReady(bulkIndexFuture) { _ => refresh() }

0 commit comments

Comments
 (0)