PageRenderTime 68ms CodeModel.GetById 2ms app.highlight 61ms RepoModel.GetById 1ms app.codeStats 0ms

/src/main/scala/scalastic/elasticsearch/IndexCrud.scala

Relevant Search: With Applications for Solr and Elasticsearch

For more in depth reading about search, ranking and generally everything you could ever want to know about how lucene, elasticsearch or solr work under the hood I highly suggest this book. Easily one of the most interesting technical books I have read in a long time. If you are tasked with solving search relevance problems even if not in Solr or Elasticsearch it should be your first reference. Amazon Affiliate Link
https://github.com/pmanvi/scalastic
Scala | 521 lines | 453 code | 55 blank | 13 comment | 4 complexity | 3a3e2fedfe599c9279fb234617696a43 MD5 | raw file
  1package scalastic.elasticsearch
  2
  3import org.elasticsearch.action.support.broadcast._
  4import org.elasticsearch.common.settings.ImmutableSettings._
  5import org.elasticsearch.cluster.metadata._
  6import org.elasticsearch.index.query._
  7import org.elasticsearch.common.unit._, TimeValue._
  8import scala.collection._, JavaConversions._
  9import org.elasticsearch.action.admin.indices.cache.clear.ClearIndicesCacheRequestBuilder
 10
 11trait IndexCrud
 12    extends Exists
 13    with CreateIndex with DeleteIndex
 14    with Open with Close
 15    with Alias with Unalias
 16    with PutMapping with DeleteMapping
 17    with PutTemplate with DeleteTemplate
 18    with ClearCache
 19    with Flush
 20    with Refresh
 21    with Optimize
 22    with GatewaySnapshot
 23    with Segments
 24    with Status
 25    with Stats
 26    with UpdateSettings {
 27  self: Indexer =>
 28}
 29
 30trait CreateIndex {
 31  self: Indexer =>
 32
 33  def createIndex(
 34    index: String,
 35//fixme: change settings to  consistently be a map, or a json string
 36    settings: Map[String, String] = Map(),
 37    mappings: Map[String, String] = Map(),
 38    cause: Option[String] = None,
 39    timeout: Option[String] = None) = createIndex_send(index, settings, mappings, cause, timeout).actionGet
 40
 41  def createIndex_send(
 42    index: String,
 43    settings: Map[String, String] = Map(),
 44    mappings: Map[String, String] = Map(),
 45    cause: Option[String] = None,
 46    timeout: Option[String] = None) = createIndex_prepare(index, settings, mappings, cause, timeout).execute
 47
 48  def createIndex_prepare(
 49    index: String,
 50    settings: Map[String, String] = Map(),
 51    mappings: Map[String, String] = Map(),
 52    cause: Option[String] = None,
 53    timeout: Option[String] = None) = {
 54		  /* method body */
 55    val request = client.admin.indices.prepareCreate(index)
 56    if (!settings.isEmpty) request.setSettings(settingsBuilder.put(settings).build())
 57    mappings foreach { case (kind, mapping) => request.addMapping(kind, mapping) }
 58    cause foreach { request.setCause(_) }
 59    timeout foreach { request.setTimeout(_) }
 60    request
 61  }
 62}
 63
 64trait DeleteIndex {
 65  self: Indexer =>
 66  def deleteIndex(indices: Iterable[String] = Nil, timeout: Option[String] = None) = deleteIndex_send(indices, timeout).actionGet
 67  def deleteIndex_send(indices: Iterable[String] = Nil, timeout: Option[String] = None) = deleteIndex_prepare(indices, timeout).execute
 68  def deleteIndex_prepare(indices: Iterable[String] = Nil, timeout: Option[String] = None) = {
 69    val request = client.admin.indices.prepareDelete(indices.toArray: _*)
 70    timeout foreach { request.setTimeout(_) }
 71    request
 72  }
 73
 74  /** caveat: if none of the indices exists, we will encounter a validation exception */
 75  def deleteIndexIfExists(indices: Iterable[String] = Nil, timeout: Option[String] = None) = deleteIndexIfExists_send(indices, timeout).actionGet
 76  def deleteIndexIfExists_send(indices: Iterable[String] = Nil, timeout: Option[String] = None) = deleteIndexIfExists_prepare(indices, timeout).execute
 77  def deleteIndexIfExists_prepare(indices: Iterable[String] = Nil, timeout: Option[String] = None) = {
 78    val existing = indices filter (exists(_).isExists)
 79    val toDelete: Iterable[String] = if (existing.isEmpty) null else existing
 80    deleteIndex_prepare(toDelete, timeout)
 81  }
 82}
 83
 84trait UpdateSettings {
 85  self: Indexer =>
 86  def updateSettings(settings: String, indices: String*) = updateSettings_send(settings, indices.toArray: _*).actionGet
 87  def updateSettings_send(settings: String, indices: String*) = updateSettings_prepare(settings, indices.toArray: _*).execute
 88  def updateSettings_prepare(settings: String, indices: String*) = {
 89    val request = client.admin.indices.prepareUpdateSettings(indices.toArray: _*)
 90    request.setSettings(settingsBuilder.loadFromSource(settings).build())
 91    request
 92  }
 93}
 94
 95trait Exists {
 96  self: Indexer =>
 97  def exists(indices: String*) = exists_send(indices.toArray: _*).actionGet
 98  def exists_send(indices: String*) = exists_prepare(indices.toArray: _*).execute
 99  def exists_prepare(indices: String*) = client.admin.indices.prepareExists(indices.toArray: _*)
100}
101
102trait Alias {
103  self: Indexer =>
104    
105  def alias(
106    indices: Iterable[String],
107    alias: String,
108    actions: Iterable[AliasAction] = Nil,
109    filter: Option[FilterBuilder] = None,
110    timeout: Option[String] = None) = alias_send(indices, alias, actions, filter, timeout).actionGet
111    
112  def alias_send(
113    indices: Iterable[String],
114    alias: String,
115    actions: Iterable[AliasAction] = Nil,
116    filter: Option[FilterBuilder] = None,
117    timeout: Option[String] = None) = alias_prepare(indices, alias, actions, filter, timeout).execute
118    
119  def alias_prepare(
120    indices: Iterable[String],
121    alias: String,
122    actions: Iterable[AliasAction] = Nil,
123    filter: Option[FilterBuilder] = None,
124    timeout: Option[String] = None) = {
125		  /* method body */
126    val request = client.admin.indices.prepareAliases
127    actions foreach { request.addAliasAction(_) }
128    indices foreach {
129      filter match {
130        case Some(builder) => request.addAlias(_, alias, builder)
131        case None => request.addAlias(_, alias)
132      }
133    }
134    timeout foreach { each => request.setTimeout(parseTimeValue(each, null)) }
135    request
136  }
137}
138
139trait Unalias {
140  self: Indexer =>
141    
142  def unalias(
143    indices: Iterable[String],
144    alias: String,
145    timeout: Option[String] = None) = unalias_send(indices, alias, timeout).actionGet
146    
147  def unalias_send(
148    indices: Iterable[String],
149    alias: String,
150    timeout: Option[String] = None) = unalias_prepare(indices, alias, timeout).execute
151    
152  def unalias_prepare(
153    indices: Iterable[String],
154    alias: String,
155    timeout: Option[String] = None) = {
156		  /* method body */
157    val request = client.admin.indices.prepareAliases
158    indices foreach { request.removeAlias(_, alias) }
159    timeout foreach { each => request.setTimeout(parseTimeValue(each, null)) }
160    request
161  }
162}
163
164trait Optimize {
165  self: Indexer =>
166
167  def optimize(
168    indices: Iterable[String] = Nil,
169    flush: Option[Boolean] = None,
170    listenerThreaded: Option[Boolean] = None,
171    maxNumSegments: Option[Int] = None,
172    onlyExpungeDeletes: Option[Boolean] = None,
173    operationThreading: Option[BroadcastOperationThreading] = None,
174    refresh: Option[Boolean] = None, 
175    waitForMerge: Option[Boolean] = None) =
176    optimize_send(indices, flush, listenerThreaded, maxNumSegments, onlyExpungeDeletes, operationThreading, refresh, waitForMerge).actionGet
177
178  def optimize_send(
179    indices: Iterable[String] = Nil,
180    flush: Option[Boolean] = None,
181    listenerThreaded: Option[Boolean] = None,
182    maxNumSegments: Option[Int] = None,
183    onlyExpungeDeletes: Option[Boolean] = None,
184    operationThreading: Option[BroadcastOperationThreading] = None,
185    refresh: Option[Boolean] = None, 
186    waitForMerge: Option[Boolean] = None) =
187    optimize_prepare(indices, flush, listenerThreaded, maxNumSegments, onlyExpungeDeletes, operationThreading, refresh, waitForMerge).execute
188
189  def optimize_prepare(
190    indices: Iterable[String] = Nil,
191    flush: Option[Boolean] = None,
192    listenerThreaded: Option[Boolean] = None,
193    maxNumSegments: Option[Int] = None,
194    onlyExpungeDeletes: Option[Boolean] = None,
195    operationThreading: Option[BroadcastOperationThreading] = None,
196    refresh: Option[Boolean] = None, 
197    waitForMerge: Option[Boolean] = None) = {
198		  /* method body */
199    val request = client.admin.indices.prepareOptimize(indices.toArray: _*)
200    flush foreach { request.setFlush(_) }
201    listenerThreaded foreach { request.setListenerThreaded(_) }
202    maxNumSegments foreach { request.setMaxNumSegments(_) }
203    onlyExpungeDeletes foreach { request.setOnlyExpungeDeletes(_) }
204    operationThreading foreach { request.setOperationThreading(_) }
205    refresh foreach { request.setRefresh(_) }
206    waitForMerge foreach { request.setWaitForMerge(_) }
207    request
208  }
209}
210
211trait Flush {
212  self: Indexer =>
213    
214  def flush(
215    indices: Iterable[String] = Nil,
216    full: Option[Boolean] = None,
217    refresh: Option[Boolean] = None) = flush_send(indices, full, refresh).actionGet
218    
219  def flush_send(
220    indices: Iterable[String] = Nil,
221    full: Option[Boolean] = None,
222    refresh: Option[Boolean] = None) = flush_prepare(indices, full, refresh).execute
223    
224  def flush_prepare(
225    indices: Iterable[String] = Nil,
226    full: Option[Boolean] = None,
227    refresh: Option[Boolean] = None) = {
228		  /* method body */
229    val request = client.admin.indices.prepareFlush(indices.toArray: _*)
230    full foreach { request.setFull(_) }
231    refresh foreach { request.setRefresh(_) }
232    request
233  }
234}
235
236trait Refresh {
237  self: Indexer =>
238    
239  def refresh(
240    indices: Iterable[String] = Nil,
241    listenerThreaded: Option[Boolean] = None,
242    operationThreading: Option[BroadcastOperationThreading] = None,
243    waitForOperations: Option[Boolean] = None) = refresh_send(indices, listenerThreaded, operationThreading, waitForOperations).actionGet
244    
245  def refresh_send(
246    indices: Iterable[String] = Nil,
247    listenerThreaded: Option[Boolean] = None,
248    operationThreading: Option[BroadcastOperationThreading] = None,
249    waitForOperations: Option[Boolean] = None) = refresh_prepare(indices, listenerThreaded, operationThreading, waitForOperations).execute
250    
251  def refresh_prepare(
252    indices: Iterable[String] = Nil,
253    listenerThreaded: Option[Boolean] = None,
254    operationThreading: Option[BroadcastOperationThreading] = None,
255    waitForOperations: Option[Boolean] = None) = {
256		  /* method body */
257    val request = client.admin.indices.prepareRefresh(indices.toArray: _*)
258    listenerThreaded foreach { request.setListenerThreaded(_) }
259    operationThreading foreach { request.setOperationThreading(_) }
260    waitForOperations foreach { request.setWaitForOperations(_) }
261    request
262  }
263}
264
265trait Status {
266  self: Indexer =>
267  def status(indices: Iterable[String] = Nil, recovery: Option[Boolean] = None, snapshot: Option[Boolean] = None) = status_send(indices, recovery, snapshot).actionGet
268  def status_send(indices: Iterable[String] = Nil, recovery: Option[Boolean] = None, snapshot: Option[Boolean] = None) = status_prepare(indices, recovery, snapshot).execute
269  def status_prepare(indices: Iterable[String] = Nil, recovery: Option[Boolean] = None, snapshot: Option[Boolean] = None) = {
270		  /* method body */
271    val request = client.admin.indices.prepareStatus(indices.toArray: _*)
272    recovery foreach { request.setRecovery(_) }
273    snapshot foreach { request.setSnapshot(_) }
274    request
275  }
276}
277
278trait Stats {
279  self: Indexer =>
280
281  def stats(
282    indices: Iterable[String] = Nil,
283    docs: Option[Boolean] = None,
284    flush: Option[Boolean] = None,
285    get: Option[Boolean] = None,
286    groups: Iterable[String] = Nil,
287    indexing: Option[Boolean] = None,
288    merge: Option[Boolean] = None,
289    refresh: Option[Boolean] = None,
290    search: Option[Boolean] = None,
291    store: Option[Boolean] = None,
292    types: Iterable[String] = Nil) = stats_send(indices, docs, flush, get, groups, indexing, merge, refresh, search, store, types).actionGet
293
294  def stats_send(
295    indices: Iterable[String] = Nil,
296    docs: Option[Boolean] = None,
297    flush: Option[Boolean] = None,
298    get: Option[Boolean] = None,
299    groups: Iterable[String] = Nil,
300    indexing: Option[Boolean] = None,
301    merge: Option[Boolean] = None,
302    refresh: Option[Boolean] = None,
303    search: Option[Boolean] = None,
304    store: Option[Boolean] = None,
305    types: Iterable[String] = Nil) = stats_prepare(indices, docs, flush, get, groups, indexing, merge, refresh, search, store, types).execute
306
307  def stats_prepare(
308    indices: Iterable[String] = Nil,
309    docs: Option[Boolean] = None,
310    flush: Option[Boolean] = None,
311    get: Option[Boolean] = None,
312    groups: Iterable[String] = Nil,
313    indexing: Option[Boolean] = None,
314    merge: Option[Boolean] = None,
315    refresh: Option[Boolean] = None,
316    search: Option[Boolean] = None,
317    store: Option[Boolean] = None,
318    types: Iterable[String] = Nil) = {
319		  /* method body */
320    val request = client.admin.indices.prepareStats(indices.toArray: _*)
321    docs foreach { request.setDocs(_) }
322    flush foreach { request.setFlush(_) }
323    get foreach { request.setGet(_) }
324    request.setGroups(groups.toArray: _*)
325    indexing foreach { request.setIndexing(_) }
326    merge foreach { request.setMerge(_) }
327    refresh foreach { request.setRefresh(_) }
328    search foreach { request.setSearch(_) }
329    store foreach { request.setStore(_) }
330    request.setTypes(types.toArray: _*)
331    request
332  }
333}
334
335trait PutMapping {
336  self: Indexer =>
337    
338  def putMapping(index: String, `type`: String, source: String, ignoreConflicts: Option[Boolean] = None, timeout: Option[String] = None) = 
339    putMappingForAll(Seq(index), `type`, source, ignoreConflicts, timeout)
340  def putMapping_send(index: String, `type`: String, source: String, ignoreConflicts: Option[Boolean] = None, timeout: Option[String] = None) = 
341    putMappingForAll_send(Seq(index), `type`, source, ignoreConflicts, timeout)
342  def putMapping_prepare(index: String, `type`: String, source: String, ignoreConflicts: Option[Boolean] = None, timeout: Option[String] = None) = 
343    putMappingForAll_prepare(Seq(index), `type`, source, ignoreConflicts, timeout)
344
345  def putMappingForAll(indices: Iterable[String], `type`: String, source: String, ignoreConflicts: Option[Boolean] = None, timeout: Option[String] = None) = 
346    putMappingForAll_send(indices, `type`, source, ignoreConflicts, timeout).actionGet
347  def putMappingForAll_send(indices: Iterable[String], `type`: String, source: String, ignoreConflicts: Option[Boolean] = None, timeout: Option[String] = None) = 
348    putMappingForAll_prepare(indices, `type`, source, ignoreConflicts, timeout).execute
349  def putMappingForAll_prepare(indices: Iterable[String], `type`: String, source: String, ignoreConflicts: Option[Boolean] = None, timeout: Option[String] = None) = {
350    val request = client.admin.indices.preparePutMapping(indices.toArray: _*)
351    request.setType(`type`)
352    request.setSource(source)
353    ignoreConflicts foreach { request.setIgnoreConflicts(_) }
354    timeout foreach { request.setTimeout(_) }
355    request
356  }
357}
358
359trait DeleteMapping {
360  self: Indexer =>
361    
362  def deleteMapping(
363    indices: Iterable[String] = Nil,
364    `type`: Option[String] = None,
365    timeout: Option[String] = None) =  deleteMapping_send(indices, `type`, timeout).actionGet
366    
367  def deleteMapping_send(
368    indices: Iterable[String] = Nil,
369    `type`: Option[String] = None,
370    timeout: Option[String] = None) = deleteMapping_prepare(indices, `type`, timeout).execute
371    
372  def deleteMapping_prepare(
373    indices: Iterable[String] = Nil,
374    `type`: Option[String] = None,
375    timeout: Option[String] = None) = {
376		  /* method body */
377    val request = client.admin.indices.prepareDeleteMapping(indices.toArray: _*)
378    `type` foreach { request.setType(_) }
379    timeout foreach { each => request.setMasterNodeTimeout(parseTimeValue(each, null)) }
380    request
381  }
382}
383
384trait PutTemplate {
385  self: Indexer =>
386    
387  def putTemplate(
388    name: String,
389    settings: Map[String, String] = Map(),
390    mappings: Map[String, String] = Map(),
391    cause: Option[String] = None,
392    create: Option[Boolean] = None,
393    order: Option[Int] = None,
394    source: Option[String] = None,
395    template: Option[String] = None,
396    timeout: Option[String] = None) =
397    putTemplate_send(name, settings, mappings, cause, create, order, source, template, timeout).actionGet
398
399  def putTemplate_send(
400    name: String,
401    settings: Map[String, String] = Map(),
402    mappings: Map[String, String] = Map(),
403    cause: Option[String] = None,
404    create: Option[Boolean] = None,
405    order: Option[Int] = None,
406    source: Option[String] = None,
407    template: Option[String] = None,
408    timeout: Option[String] = None) =
409    putTemplate_prepare(name, settings, mappings, cause, create, order, source, template, timeout).execute
410
411  def putTemplate_prepare(
412    name: String,
413    settings: Map[String, String] = Map(),
414    mappings: Map[String, String] = Map(),
415    cause: Option[String] = None,
416    create: Option[Boolean] = None,
417    order: Option[Int] = None,
418    source: Option[String] = None,
419    template: Option[String] = None,
420    timeout: Option[String] = None) = {
421		  /* method body */
422    val request = client.admin.indices.preparePutTemplate(name)
423    if (!settings.isEmpty) request.setSettings(settingsBuilder.put(settings).build())
424    mappings foreach { case (kind, mapping) => request.addMapping(kind, mapping) }
425    cause foreach { request.cause(_) }
426    create foreach { request.setCreate(_) }
427    order foreach { request.setOrder(_) }
428    source foreach { request.setSource(_) }
429    template foreach { request.setTemplate(_) }
430    timeout foreach { request.setTimeout(_) }
431    request
432  }
433}
434
435trait DeleteTemplate {
436  self: Indexer =>
437  def deleteTemplate(name: String, timeout: Option[String] = None) = deleteTemplate_send(name, timeout).actionGet
438  def deleteTemplate_send(name: String, timeout: Option[String] = None) = deleteTemplate_prepare(name, timeout).execute
439  def deleteTemplate_prepare(name: String, timeout: Option[String] = None) = {
440    val request = client.admin.indices.prepareDeleteTemplate(name)
441    timeout foreach { request.setTimeout(_) }
442    request
443  }
444}
445
446trait Open {
447  self: Indexer =>
448  def openIndex(index: String, timeout: Option[String] = None) = openIndex_send(index, timeout).actionGet
449  def openIndex_send(index: String, timeout: Option[String] = None) = openIndex_prepare(index, timeout).execute
450  def openIndex_prepare(index: String, timeout: Option[String] = None) = {
451    val request = client.admin.indices.prepareOpen(index)
452    timeout foreach { request.setTimeout(_) }
453    request
454  }
455}
456
457trait Close {
458  self: Indexer =>
459  def closeIndex(index: String, timeout: Option[String] = None) = closeIndex_send(index, timeout).actionGet
460  def closeIndex_send(index: String, timeout: Option[String] = None) = closeIndex_prepare(index, timeout).execute
461  def closeIndex_prepare(index: String, timeout: Option[String] = None) = {
462    val request = client.admin.indices.prepareClose(index)
463    timeout foreach { request.setTimeout(_) }
464    request
465  }
466}
467
468trait GatewaySnapshot {
469  self: Indexer =>
470  def gatewaySnapshot(indices: String*) = gatewaySnapshot_send(indices.toArray: _*).actionGet
471  def gatewaySnapshot_send(indices: String*) = gatewaySnapshot_prepare(indices.toArray: _*).execute
472  def gatewaySnapshot_prepare(indices: String*) = client.admin.indices.prepareGatewaySnapshot(indices.toArray: _*)
473}
474
475trait Segments {
476  self: Indexer =>
477  def segments(indices: String*) = segments_send(indices.toArray: _*).actionGet
478  def segments_send(indices: String*) = segments_prepare(indices.toArray: _*).execute
479  def segments_prepare(indices: String*) = client.admin.indices.prepareSegments(indices.toArray: _*)
480}
481
482trait ClearCache {
483  self: Indexer =>
484    
485  def clearCache(
486    indices: Iterable[String] = Nil,
487    fields: Iterable[String] = Nil,
488    fieldDataCache: Option[Boolean] = None,
489    filterCache: Option[Boolean] = None,
490    idCache: Option[Boolean] = None,
491    listenerThreaded: Option[Boolean] = None,
492    operationThreading: Option[BroadcastOperationThreading] = None) = clearCache_send(indices, fields, fieldDataCache, filterCache, idCache, listenerThreaded, operationThreading).actionGet
493    
494  def clearCache_send(
495    indices: Iterable[String] = Nil,
496    fields: Iterable[String] = Nil,
497    fieldDataCache: Option[Boolean] = None,
498    filterCache: Option[Boolean] = None,
499    idCache: Option[Boolean] = None,
500    listenerThreaded: Option[Boolean] = None,
501    operationThreading: Option[BroadcastOperationThreading] = None) = clearCache_prepare(indices, fields, fieldDataCache, filterCache, idCache, listenerThreaded, operationThreading).execute
502    
503  def clearCache_prepare(
504    indices: Iterable[String] = Nil,
505    fields: Iterable[String] = Nil,
506    fieldDataCache: Option[Boolean] = None,
507    filterCache: Option[Boolean] = None,
508    idCache: Option[Boolean] = None,
509    listenerThreaded: Option[Boolean] = None,
510    operationThreading: Option[BroadcastOperationThreading] = None): ClearIndicesCacheRequestBuilder = {
511		  /* method body */
512    val request = client.admin.indices.prepareClearCache(indices.toArray: _*)
513    request.setFields(fields.toArray: _*)
514    fieldDataCache foreach { request.setFieldDataCache(_) }
515    filterCache foreach { request.setFilterCache(_) }
516    idCache foreach { request.setIdCache(_) }
517    listenerThreaded foreach { request.setListenerThreaded(_) }
518    operationThreading foreach { request.setOperationThreading(_) }
519    request
520  }
521}