forked from Philippus/elastic4s
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIndexDslTest.scala
266 lines (222 loc) · 7.59 KB
/
IndexDslTest.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
package com.sksamuel.elastic4s
import org.scalatest.{ Matchers, FlatSpec, OneInstancePerTest }
import org.scalatest.mock.MockitoSugar
import ElasticDsl._
import org.elasticsearch.common.xcontent.XContentBuilder
import java.util.{ Calendar, GregorianCalendar, Date }
import java.text.SimpleDateFormat
/** @author Stephen Samuel */
class IndexDslTest extends FlatSpec with MockitoSugar with JsonSugar with Matchers with OneInstancePerTest {
"an index dsl" should "generate with index & type as a delimited string" in {
val req = index into "twitter/tweets" fields Map(
"name" -> "sksamuel"
)
checkRequest(req, "twitter", "tweets", "/json/index/simple_single.json")
}
it should "generate with index & type as a tuple" in {
val req = index into "twitter" -> "tweets" fields (
"name" -> "sksamuel"
)
checkRequest(req, "twitter", "tweets", "/json/index/simple_single.json")
}
it should "generate with a string id" in {
val req = index into "twitter/tweets" id "test-id" fields (
"name" -> "sksamuel"
)
checkRequest(req, "twitter", "tweets", "/json/index/simple_single.json", Some("test-id"))
}
it should "generate with a numeric id" in {
val req = index into "twitter/tweets" id 1234 fields (
"name" -> "sksamuel"
)
checkRequest(req, "twitter", "tweets", "/json/index/simple_single.json", Some(1234))
}
it should "generate including routing and ttl" in {
val req = index into "twitter/tweets" fields (
"name" -> "sksamuel"
) routing "users" ttl 100000
checkRequest(req, "twitter", "tweets", "/json/index/simple_single.json", None, Some("users"), Some(100000))
}
it should "generate for multiple fields" in {
val req = index into "twitter/tweets" fields (
"user" -> "sammy",
"post_date" -> "2009-11-15T14:12:12",
"message" -> "trying out Elastic Search Scala DSL"
)
checkRequest(req, "twitter", "tweets", "/json/index/simple_multiple.json")
}
it should "generate for multiple fields when using a map" in {
val req = index into "twitter/tweets" fields Map(
"user" -> "sammy",
"post_date" -> "2009-11-15T14:12:12",
"message" -> "trying out Elastic Search Scala DSL"
)
checkRequest(req, "twitter", "tweets", "/json/index/simple_multiple.json")
}
it should "generate nested fields" in {
val req = index into "twitter/tweets" fields (
"user" -> Map(
"handle" -> "sammy",
"name" -> "Sam"
),
"post_date" -> "2011-11-15T14:12:12",
"message" -> "Nested message"
)
checkRequest(req, "twitter", "tweets", "/json/index/nested.json")
}
it should "generate array fields" in {
val req = index into "twitter/tweets" fields (
"user" -> "sammy",
"post_date" -> "2011-11-15T14:12:12",
"message" -> "Array message",
"tags" -> Array(
"array",
"search",
"test"
)
)
checkRequest(req, "twitter", "tweets", "/json/index/array.json")
}
it should "generate array fields when using seqs" in {
val req = index into "twitter/tweets" fields (
"user" -> "sammy",
"post_date" -> "2011-11-15T14:12:12",
"message" -> "Array message",
"tags" -> Seq(
"array",
"search",
"test"
)
)
checkRequest(req, "twitter", "tweets", "/json/index/array.json")
}
it should "generate array of nested fields" in {
val req = index into "twitter/tweets" fields (
"user" -> "sammy",
"post_date" -> "2011-11-15T14:12:12",
"message" -> "Array of nested message",
"tags" -> Array(
Map(
"id" -> 642,
"text" -> "array"
),
Map(
"id" -> 883,
"text" -> "search"
),
Map(
"id" -> 231,
"text" -> "test"
)
)
)
checkRequest(req, "twitter", "tweets", "/json/index/array_nested.json")
}
it should "generate array of nested fields when using seqs" in {
val req = index into "twitter/tweets" fields (
"user" -> "sammy",
"post_date" -> "2011-11-15T14:12:12",
"message" -> "Array of nested message",
"tags" -> Seq(
Map(
"id" -> 642,
"text" -> "array"
),
Map(
"id" -> 883,
"text" -> "search"
),
Map(
"id" -> 231,
"text" -> "test"
)
)
)
checkRequest(req, "twitter", "tweets", "/json/index/array_nested.json")
}
it should "generate nested field of arrays" in {
val req = index into "twitter/tweets" fields (
"user" -> Map(
"handle" -> "sammy",
"name" -> "Sam",
"hobbies" -> Array(
"search",
"scala"
)
),
"post_date" -> "2011-11-15T14:12:12",
"message" -> "Nested array message"
)
checkRequest(req, "twitter", "tweets", "/json/index/nested_array.json")
}
it should "generate null fields" in {
val req = index into "twitter/tweets" fields (
"user" -> "sammy",
"message" -> null
)
checkRequest(req, "twitter", "tweets", "/json/index/null.json")
}
it should "generate nested null fields" in {
val req = index into "twitter/tweets" fields (
"user" -> Map(
"handle" -> "sammy",
"name" -> null
),
"message" -> "Message with nested null"
)
checkRequest(req, "twitter", "tweets", "/json/index/nested_null.json")
}
it should "field values" in {
val req = index into "twitter/tweets" fieldValues (
SimpleFieldValue("user", "sammy"),
SimpleFieldValue("post_date", "2009-11-15T14:12:12"),
SimpleFieldValue("message", "trying out Elastic Search Scala DSL")
)
checkRequest(req, "twitter", "tweets", "/json/index/simple_multiple.json")
}
it should "custom field values" in {
val cal = new GregorianCalendar()
cal.set(Calendar.YEAR, 2009)
cal.set(Calendar.MONTH, 10)
cal.set(Calendar.DAY_OF_MONTH, 15)
cal.set(Calendar.HOUR_OF_DAY, 14)
cal.set(Calendar.MINUTE, 12)
cal.set(Calendar.SECOND, 12)
val req = index into "twitter/tweets" fieldValues (
SimpleFieldValue("user", "sammy"),
CustomDateFieldValue("post_date", cal.getTime),
SimpleFieldValue("message", "trying out Elastic Search Scala DSL")
)
checkRequest(req, "twitter", "tweets", "/json/index/simple_multiple.json")
}
private def checkRequest(req: IndexDefinition,
expectedIndex: String,
expectedType: String,
expectedJsonResource: String,
expectedId: Option[Any] = None,
expectedRouting: Option[String] = None,
expectedTtl: Option[Long] = None) {
val builtRequest = req.build
builtRequest.index shouldEqual expectedIndex
builtRequest.`type` shouldEqual expectedType
expectedId match {
case Some(i) => builtRequest.id shouldEqual i.toString
case None => builtRequest.id shouldBe null
}
expectedRouting match {
case Some(r) => builtRequest.routing shouldEqual r
case None => builtRequest.routing shouldBe null
}
expectedTtl match {
case Some(t) => builtRequest.ttl shouldEqual t
case None => builtRequest.ttl shouldEqual -1
}
req._fieldsAsXContent.string should matchJsonResource(expectedJsonResource)
}
case class CustomDateFieldValue(name: String, date: Date) extends FieldValue {
private val dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss")
def output(source: XContentBuilder): Unit = {
source.field(name, dateFormat.format(date))
}
}
}