forked from opensearch-project/opensearch-spark
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOpenSearchSuite.scala
238 lines (210 loc) · 7.85 KB
/
OpenSearchSuite.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
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/
package org.opensearch.flint
import scala.io.Source
import org.apache.http.HttpHost
import org.opensearch.action.admin.indices.delete.DeleteIndexRequest
import org.opensearch.action.bulk.BulkRequest
import org.opensearch.action.index.IndexRequest
import org.opensearch.action.support.WriteRequest.RefreshPolicy
import org.opensearch.client.{RequestOptions, RestClient, RestHighLevelClient}
import org.opensearch.client.indices.{CreateIndexRequest, GetIndexRequest}
import org.opensearch.common.xcontent.XContentType
import org.opensearch.testcontainers.OpenSearchContainer
import org.scalatest.{BeforeAndAfterAll, Suite}
import org.apache.spark.sql.flint.config.FlintSparkConf.{HOST_ENDPOINT, HOST_PORT, IGNORE_DOC_ID_COLUMN, REFRESH_POLICY}
/**
* Test required OpenSearch domain should extend OpenSearchSuite.
*/
trait OpenSearchSuite extends BeforeAndAfterAll {
self: Suite =>
protected lazy val container = new OpenSearchContainer()
protected lazy val openSearchPort: Int = container.port()
protected lazy val openSearchHost: String = container.getHost
protected lazy val openSearchClient = new RestHighLevelClient(
RestClient.builder(new HttpHost(openSearchHost, openSearchPort, "http")))
protected lazy val openSearchOptions =
Map(
s"${HOST_ENDPOINT.optionKey}" -> openSearchHost,
s"${HOST_PORT.optionKey}" -> s"$openSearchPort",
s"${REFRESH_POLICY.optionKey}" -> "wait_for",
s"${IGNORE_DOC_ID_COLUMN.optionKey}" -> "false")
override def beforeAll(): Unit = {
container.start()
super.beforeAll()
}
override def afterAll(): Unit = {
container.close()
super.afterAll()
}
/**
* Delete index `indexNames` after calling `f`.
*/
protected def withIndexName(indexNames: String*)(f: => Unit): Unit = {
try {
f
} finally {
indexNames.foreach { indexName =>
openSearchClient
.indices()
.delete(new DeleteIndexRequest(indexName), RequestOptions.DEFAULT)
}
}
}
val oneNodeSetting = """{
| "number_of_shards": "1",
| "number_of_replicas": "0"
|}""".stripMargin
val multipleShardSetting = """{
| "number_of_shards": "2",
| "number_of_replicas": "0"
|}""".stripMargin
def simpleIndex(indexName: String): Unit = {
val mappings = """{
| "properties": {
| "accountId": {
| "type": "keyword"
| },
| "eventName": {
| "type": "keyword"
| },
| "eventSource": {
| "type": "keyword"
| }
| }
|}""".stripMargin
val docs = Seq("""{
| "accountId": "123",
| "eventName": "event",
| "eventSource": "source"
|}""".stripMargin)
index(indexName, oneNodeSetting, mappings, docs)
}
def multipleDocIndex(indexName: String, N: Int): Unit = {
val mappings = """{
| "properties": {
| "id": {
| "type": "integer"
| }
| }
|}""".stripMargin
val docs = for (n <- 1 to N) yield s"""{"id": $n}""".stripMargin
index(indexName, multipleShardSetting, mappings, docs)
}
def multipleShardAndDocIndex(indexName: String, N: Int): Unit = {
val mappings = """{
| "properties": {
| "id": {
| "type": "integer"
| }
| }
|}""".stripMargin
val docs = for (n <- 1 to N) yield s"""{"id": $n}""".stripMargin
index(indexName, oneNodeSetting, mappings, docs)
}
def openSearchDashboardsIndex(useCaseName: String, indexName: String): Unit = {
val mappings =
Source
.fromResource(s"opensearch/${useCaseName}_mappings.json")
.mkString
val docs: Seq[String] =
Source.fromResource(s"opensearch/${useCaseName}.json").getLines().toSeq
index(indexName, oneNodeSetting, mappings, docs)
}
def indexWithAlias(indexName: String): Unit = {
val mappings = """{
| "properties": {
| "id": {
| "type": "integer"
| },
| "alias": {
| "type": "alias",
| "path": "id"
| }
| }
|}""".stripMargin
val docs = Seq("""{"id": 1}""", """{"id": 2}""")
index(indexName, oneNodeSetting, mappings, docs)
}
def indexMultiFields(indexName: String): Unit = {
val mappings = """{
| "properties": {
| "id": {
| "type": "integer"
| },
| "aText": {
| "type": "text"
| },
| "aString": {
| "type": "keyword"
| },
| "aTextString": {
| "type": "text",
| "fields": {
| "raw": {
| "type": "keyword"
| }
| }
| }
| }
|}""".stripMargin
val docs = Seq("""{
| "id": 1,
| "aText": "Treviso-Sant'Angelo Airport",
| "aString": "OpenSearch-Air",
| "aTextString": "Treviso-Sant'Angelo Airport"}""".stripMargin)
index(indexName, oneNodeSetting, mappings, docs)
}
def indexWithIp(indexName: String): Unit = {
val mappings = """{
| "properties": {
| "client": {
| "type": "ip"
| },
| "server": {
| "type": "ip"
| }
| }
|}""".stripMargin
val docs = Seq(
"""{
| "client": "192.168.0.10",
| "server": "100.10.12.123"
|}""".stripMargin,
"""{
| "client": "192.168.0.11",
| "server": "100.10.12.123"
|}""".stripMargin,
"""{
| "client": "2001:db8:3333:4444:5555:6666:7777:8888",
| "server": "2001:db8::1234:5678"
|}""".stripMargin)
index(indexName, oneNodeSetting, mappings, docs)
}
def index(index: String, settings: String, mappings: String, docs: Seq[String]): Unit = {
openSearchClient.indices.create(
new CreateIndexRequest(index)
.settings(settings, XContentType.JSON)
.mapping(mappings, XContentType.JSON),
RequestOptions.DEFAULT)
val getIndexResponse =
openSearchClient.indices().get(new GetIndexRequest(index), RequestOptions.DEFAULT)
assume(getIndexResponse.getIndices.contains(index), s"create index $index failed")
/**
* 1. Wait until refresh the index.
*/
if (docs.nonEmpty) {
val request = new BulkRequest().setRefreshPolicy(RefreshPolicy.WAIT_UNTIL)
for (doc <- docs) {
request.add(new IndexRequest(index).source(doc, XContentType.JSON))
}
val response =
openSearchClient.bulk(request, RequestOptions.DEFAULT)
assume(
!response.hasFailures,
s"bulk index docs to $index failed: ${response.buildFailureMessage()}")
}
}
}