PageRenderTime 28ms CodeModel.GetById 2ms app.highlight 22ms RepoModel.GetById 1ms app.codeStats 0ms

/src/main/scala/scalastic/elasticsearch/Indexing.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 | 438 lines | 398 code | 32 blank | 8 comment | 3 complexity | 21ffda1d119b5ed4d71428271f4869b8 MD5 | raw file
  1package scalastic.elasticsearch
  2
  3import org.elasticsearch.common._, xcontent._
  4import org.elasticsearch.index.VersionType
  5import org.elasticsearch.index.query._, QueryBuilders._
  6import org.elasticsearch.action._, get._, index._, delete._
  7import org.elasticsearch.action.support.broadcast._
  8import org.elasticsearch.action.support.replication._
  9import scala.collection._, JavaConversions._
 10
 11trait Indexing
 12    extends Index
 13    with Bulk
 14    with Searching
 15    with Count
 16    with Get
 17    with Multiget
 18    with Delete
 19    with DeleteByQuery {
 20  self: Indexer =>
 21}
 22
 23trait Index {
 24  self: Indexer =>
 25
 26  def index(
 27    index: String,
 28    `type`: String,
 29    @Nullable id: String,
 30    source: String,
 31    parent: String = null,
 32    consistencyLevel: Option[WriteConsistencyLevel] = None,
 33    contentType: Option[XContentType] = None,
 34    create: Option[Boolean] = None,
 35    listenerThreaded: Option[Boolean] = None,
 36    operationThreaded: Option[Boolean] = None,
 37    opType: Option[IndexRequest.OpType] = None,
 38    percolate: Option[String] = None,
 39    refresh: Option[Boolean] = None,
 40    replicationType: Option[ReplicationType] = None,
 41    routing: Option[String] = None,
 42    timeout: Option[String] = None,
 43    timestamp: Option[String] = None,
 44    ttl: Option[Long] = None,
 45    version: Option[Long] = None,
 46    versionType: Option[VersionType] = None) = index_send(index, `type`, id, source, parent, consistencyLevel, contentType, create, listenerThreaded, operationThreaded, opType, percolate, refresh, replicationType, routing, timeout, timestamp, ttl, version, versionType).actionGet
 47    
 48  def index_send(
 49    index: String,
 50    `type`: String,
 51    @Nullable id: String,
 52    source: String,
 53    parent: String = null,
 54    consistencyLevel: Option[WriteConsistencyLevel] = None,
 55    contentType: Option[XContentType] = None,
 56    create: Option[Boolean] = None,
 57    listenerThreaded: Option[Boolean] = None,
 58    operationThreaded: Option[Boolean] = None,
 59    opType: Option[IndexRequest.OpType] = None,
 60    percolate: Option[String] = None,
 61    refresh: Option[Boolean] = None,
 62    replicationType: Option[ReplicationType] = None,
 63    routing: Option[String] = None,
 64    timeout: Option[String] = None,
 65    timestamp: Option[String] = None,
 66    ttl: Option[Long] = None,
 67    version: Option[Long] = None,
 68    versionType: Option[VersionType] = None) = index_prepare(index, `type`, id, source, parent, consistencyLevel, contentType, create, listenerThreaded, operationThreaded, opType, percolate, refresh, replicationType, routing, timeout, timestamp, ttl, version, versionType).execute
 69
 70  def index_prepare(
 71    index: String,
 72    `type`: String,
 73    @Nullable id: String,
 74    source: String,
 75    parent: String = null,
 76    consistencyLevel: Option[WriteConsistencyLevel] = None,
 77    contentType: Option[XContentType] = None,
 78    create: Option[Boolean] = None,
 79    listenerThreaded: Option[Boolean] = None,
 80    operationThreaded: Option[Boolean] = None,
 81    opType: Option[IndexRequest.OpType] = None,
 82    percolate: Option[String] = None,
 83    refresh: Option[Boolean] = None,
 84    replicationType: Option[ReplicationType] = None,
 85    routing: Option[String] = None,
 86    timeout: Option[String] = None,
 87    timestamp: Option[String] = None,
 88    ttl: Option[Long] = None,
 89    version: Option[Long] = None,
 90    versionType: Option[VersionType] = None) = {
 91		  /* method body */
 92    val request = client.prepareIndex(index, `type`, id)
 93    request.setSource(source)
 94    request.setParent(parent)
 95    consistencyLevel foreach { request.setConsistencyLevel(_) }
 96    contentType foreach { request.setContentType(_) }
 97    create foreach { request.setCreate(_) }
 98    listenerThreaded foreach { request.setListenerThreaded(_) }
 99    operationThreaded foreach { request.setOperationThreaded(_) }
100    opType foreach { request.setOpType(_) }
101    percolate foreach { request.setPercolate(_) }
102    refresh foreach { request.setRefresh(_) }
103    replicationType foreach { request.setReplicationType(_) }
104    routing foreach { request.setRouting(_) }
105    timeout foreach { request.setTimeout(_) }
106    timestamp foreach { request.setTimestamp(_) }
107    ttl foreach { request.setTTL(_) }
108    version foreach { request.setVersion(_) }
109    versionType foreach { request.setVersionType(_) }
110    request
111  }
112}
113
114trait Bulk {
115  self: Indexer =>
116
117  def bulk[A <: ActionRequest[A]](
118    requests: Iterable[A],
119    consistencyLevel: Option[WriteConsistencyLevel] = None,
120    refresh: Option[Boolean] = None,
121    replicationType: Option[ReplicationType] = None) = bulk_send(requests, consistencyLevel, refresh, replicationType).actionGet
122    
123  def bulk_send[A <: ActionRequest[A]](
124    requests: Iterable[A],
125    consistencyLevel: Option[WriteConsistencyLevel] = None,
126    refresh: Option[Boolean] = None,
127    replicationType: Option[ReplicationType] = None) = bulk_prepare(requests, consistencyLevel, refresh, replicationType).execute
128    
129  def bulk_prepare[A <: ActionRequest[A]](
130    requests: Iterable[A],
131    consistencyLevel: Option[WriteConsistencyLevel] = None,
132    refresh: Option[Boolean] = None,
133    replicationType: Option[ReplicationType] = None) = {
134		  /* method body */
135    val request = client.prepareBulk
136    consistencyLevel foreach { request.setConsistencyLevel(_) }
137    refresh foreach { request.setRefresh(_) }
138    replicationType foreach { request.setReplicationType(_) }
139    requests.foldLeft(request) { (result, each) =>
140      each match {
141        case indexing: IndexRequest => result.add(indexing)
142        case deleting: DeleteRequest => result.add(deleting)
143        case other => throw new IllegalArgumentException("%s type can not be bulk-indexed".format(other.getClass))
144      }
145    }
146    request
147  }
148}
149
150trait Count {
151  self: Indexer =>
152
153  def count(
154    indices: Iterable[String] = Nil,
155    types: Iterable[String] = Nil,
156    query: QueryBuilder = matchAllQuery,
157    listenerThreaded: Option[Boolean] = None,
158    minScore: Option[Float] = None,
159    operationThreading: Option[BroadcastOperationThreading] = None,
160    routing: Option[String] = None) = count_send(indices, types, query, listenerThreaded, minScore, operationThreading, routing).actionGet
161
162  def count_send(
163    indices: Iterable[String] = Nil,
164    types: Iterable[String] = Nil,
165    query: QueryBuilder = matchAllQuery,
166    listenerThreaded: Option[Boolean] = None,
167    minScore: Option[Float] = None,
168    operationThreading: Option[BroadcastOperationThreading] = None,
169    routing: Option[String] = None) = count_prepare(indices, types, query, listenerThreaded, minScore, operationThreading, routing).execute
170
171  def count_prepare(
172    indices: Iterable[String] = Nil,
173    types: Iterable[String] = Nil,
174    query: QueryBuilder = matchAllQuery,
175    listenerThreaded: Option[Boolean] = None,
176    minScore: Option[Float] = None,
177    operationThreading: Option[BroadcastOperationThreading] = None,
178    routing: Option[String] = None) = {
179		  /* method body */
180    val request = client.prepareCount(indices.toArray: _*)
181      .setTypes(types.toArray: _*)
182      .setQuery(query)
183    listenerThreaded foreach { request.setListenerThreaded(_) }
184    minScore foreach { request.setMinScore(_) }
185    operationThreading foreach { request.setOperationThreading(_) }
186    routing foreach { request.setRouting(_) }
187    request
188  }
189}
190
191trait Get {
192  self: Indexer =>
193  def get(
194    index: String,
195    @Nullable `type`: String,
196    id: String,
197    fields: Iterable[String] = Nil,
198    listenerThreaded: Option[Boolean] = None,
199    operationThreaded: Option[Boolean] = None,
200    preference: Option[String] = None,
201    realtime: Option[Boolean] = None,
202    refresh: Option[Boolean] = None,
203    routing: Option[String] = None) = get_send(index, `type`, id, fields, listenerThreaded, operationThreaded, preference, realtime, refresh, routing).actionGet
204      
205  def get_send(
206    index: String,
207    @Nullable `type`: String,
208    id: String,
209    fields: Iterable[String] = Nil,
210    listenerThreaded: Option[Boolean] = None,
211    operationThreaded: Option[Boolean] = None,
212    preference: Option[String] = None,
213    realtime: Option[Boolean] = None,
214    refresh: Option[Boolean] = None,
215    routing: Option[String] = None) = get_prepare(index, `type`, id, fields, listenerThreaded, operationThreaded, preference, realtime, refresh, routing).execute
216      
217  def get_prepare(
218    index: String,
219    @Nullable `type`: String,
220    id: String,
221    fields: Iterable[String] = Nil,
222    listenerThreaded: Option[Boolean] = None,
223    operationThreaded: Option[Boolean] = None,
224    preference: Option[String] = None,
225    realtime: Option[Boolean] = None,
226    refresh: Option[Boolean] = None,
227    routing: Option[String] = None) = {
228		  /* method body */
229    val request = client.prepareGet(index, `type`, id)
230    if (!fields.isEmpty) request.setFields(fields.toArray: _*)
231    listenerThreaded foreach { request.setListenerThreaded(_) }
232    operationThreaded foreach { request.setOperationThreaded(_) }
233    preference foreach { request.setPreference(_) }
234    realtime foreach { request.setRealtime(_) }
235    refresh foreach { request.setRefresh(_) }
236    routing foreach { request.setRouting(_) }
237    request
238  }
239}
240
241trait Multiget {
242  self: Indexer =>
243  def multiget(
244    index: String,
245    @Nullable `type`: String,
246    ids: Iterable[String],
247    fields: Iterable[String] = Nil,
248    listenerThreaded: Option[Boolean] = None,
249    preference: Option[String] = None,
250    realtime: Option[Boolean] = None,
251    refresh: Option[Boolean] = None) = multiget_send(index, `type`, ids, fields, listenerThreaded, preference, realtime, refresh).actionGet
252    
253  def multiget_send(
254    index: String,
255    @Nullable `type`: String,
256    ids: Iterable[String],
257    fields: Iterable[String] = Nil,
258    listenerThreaded: Option[Boolean] = None,
259    preference: Option[String] = None,
260    realtime: Option[Boolean] = None,
261    refresh: Option[Boolean] = None) = multiget_prepare(index, `type`, ids, fields, listenerThreaded, preference, realtime, refresh).execute
262    
263  def multiget_prepare(
264    index: String,
265    @Nullable `type`: String,
266    ids: Iterable[String],
267    fields: Iterable[String] = Nil,
268    listenerThreaded: Option[Boolean] = None,
269    preference: Option[String] = None,
270    realtime: Option[Boolean] = None,
271    refresh: Option[Boolean] = None) = {
272		  /* method body */
273    val request = client.prepareMultiGet
274    for (each <- ids) {
275      val item = new MultiGetRequest.Item(index, `type`, each)
276      if (!fields.isEmpty) item.fields(fields.toArray: _*)
277      request.add(item)
278    }
279    listenerThreaded foreach { request.setListenerThreaded(_) }
280    preference foreach { request.setPreference(_) }
281    realtime foreach { request.setRealtime(_) }
282    refresh foreach { request.setRefresh(_) }
283    request
284  }
285}
286
287trait Update {
288  self: Indexer =>
289
290  def update(
291    index: String, 
292    `type`: String, 
293    id: String, 
294    parent: Option[String] = None,
295    script: Option[String] = None, 
296    scriptLanguage: Option[String] = None, 
297    scriptParams: Map[String, Object] = Map(),
298    percolate: Option[String] = None, 
299    replicationType: Option[ReplicationType] = None, 
300    consistencyLevel: Option[WriteConsistencyLevel] = None) = update_send(index, `type`, id, parent, script, scriptLanguage, scriptParams, percolate, replicationType, consistencyLevel).actionGet
301
302  def update_send(
303    index: String, 
304    `type`: String, 
305    id: String, 
306    parent: Option[String] = None,
307    script: Option[String] = None, 
308    scriptLanguage: Option[String] = None, 
309    scriptParams: Map[String, Object] = Map(),
310    percolate: Option[String] = None, 
311    replicationType: Option[ReplicationType] = None, 
312    consistencyLevel: Option[WriteConsistencyLevel] = None) = update_prepare(index, `type`, id, parent, script, scriptLanguage, scriptParams, percolate, replicationType, consistencyLevel).execute
313
314  def update_prepare(
315    index: String, 
316    `type`: String, 
317    id: String, 
318    parent: Option[String] = None,
319    script: Option[String] = None, 
320    scriptLanguage: Option[String] = None, 
321    scriptParams: Map[String, Object] = Map(),
322    percolate: Option[String] = None, 
323    replicationType: Option[ReplicationType] = None, 
324    consistencyLevel: Option[WriteConsistencyLevel] = None) = {
325		  /* method body */
326    val request = client.prepareUpdate(index, `type`, id)
327    parent foreach { request.setParent(_) }
328    script foreach { that =>
329      request.setScript(that)
330      request.setScriptParams(scriptParams)
331      scriptLanguage foreach { request.setScriptLang(_) }
332    }
333    percolate foreach { request.setPercolate(_) }
334    replicationType foreach { request.setReplicationType(_) }
335    consistencyLevel foreach { request.setConsistencyLevel(_) }
336    request
337  }
338}
339
340trait Delete {
341  self: Indexer =>
342    
343  def delete(
344    index: String,
345    `type`: String,
346    id: String,
347    parent: String = null,
348    consistencyLevel: Option[WriteConsistencyLevel] = None,
349    listenerThreaded: Option[Boolean] = None,
350    operationThreaded: Option[Boolean] = None,
351    refresh: Option[Boolean] = None,
352    replicationType: Option[ReplicationType] = None,
353    routing: Option[String] = None,
354    version: Option[Long] = None,
355    versionType: Option[VersionType] = None) = delete_send(index, `type`, id, parent, consistencyLevel, listenerThreaded, operationThreaded, refresh, replicationType, routing, version, versionType).actionGet
356  
357  def delete_send(
358    index: String,
359    `type`: String,
360    id: String,
361    parent: String = null,
362    consistencyLevel: Option[WriteConsistencyLevel] = None,
363    listenerThreaded: Option[Boolean] = None,
364    operationThreaded: Option[Boolean] = None,
365    refresh: Option[Boolean] = None,
366    replicationType: Option[ReplicationType] = None,
367    routing: Option[String] = None,
368    version: Option[Long] = None,
369    versionType: Option[VersionType] = None) = delete_prepare(index, `type`, id, parent, consistencyLevel, listenerThreaded, operationThreaded, refresh, replicationType, routing, version, versionType).execute
370  
371  def delete_prepare(
372    index: String,
373    `type`: String,
374    id: String,
375    parent: String = null,
376    consistencyLevel: Option[WriteConsistencyLevel] = None,
377    listenerThreaded: Option[Boolean] = None,
378    operationThreaded: Option[Boolean] = None,
379    refresh: Option[Boolean] = None,
380    replicationType: Option[ReplicationType] = None,
381    routing: Option[String] = None,
382    version: Option[Long] = None,
383    versionType: Option[VersionType] = None) = {
384		  /* method body */
385    val request = client.prepareDelete(index, `type`, id)
386    request.setParent(parent)
387    consistencyLevel foreach { request.setConsistencyLevel(_) }
388    listenerThreaded foreach { request.setListenerThreaded(_) }
389    operationThreaded foreach { request.setOperationThreaded(_) }
390    refresh foreach { request.setRefresh(_) }
391    replicationType foreach { request.setReplicationType(_) }
392    routing foreach { request.setRouting(_) }
393    version foreach { request.setVersion(_) }
394    versionType foreach { request.setVersionType(_) }
395    request
396  }
397}
398
399trait DeleteByQuery {
400  self: Indexer =>
401
402  def deleteByQuery(
403    indices: Iterable[String] = Nil,
404    types: Iterable[String] = Nil,
405    query: QueryBuilder = matchAllQuery,
406    replicationType: Option[ReplicationType] = None,
407    consistencyLevel: Option[WriteConsistencyLevel] = None,
408    routing: Option[String] = None,
409    timeout: Option[String] = None) = deleteByQuery_send(indices, types, query, replicationType, consistencyLevel, routing, timeout).actionGet
410
411  def deleteByQuery_send(
412    indices: Iterable[String] = Nil,
413    types: Iterable[String] = Nil,
414    query: QueryBuilder = matchAllQuery,
415    replicationType: Option[ReplicationType] = None,
416    consistencyLevel: Option[WriteConsistencyLevel] = None,
417    routing: Option[String] = None,
418    timeout: Option[String] = None) = deleteByQuery_prepare(indices, types, query, replicationType, consistencyLevel, routing, timeout).execute
419
420  def deleteByQuery_prepare(
421    indices: Iterable[String] = Nil,
422    types: Iterable[String] = Nil,
423    query: QueryBuilder = matchAllQuery,
424    replicationType: Option[ReplicationType] = None,
425    consistencyLevel: Option[WriteConsistencyLevel] = None,
426    routing: Option[String] = None,
427    timeout: Option[String] = None) = {
428		  /* method body */
429    val request = client.prepareDeleteByQuery(indices.toArray: _*)
430    request.setTypes(types.toArray: _*)
431    request.setQuery(query)
432    replicationType foreach { request.setReplicationType(_) }
433    consistencyLevel foreach { request.setConsistencyLevel(_) }
434    routing foreach { request.setRouting(_) }
435    timeout foreach { request.setTimeout(_) }
436    request
437  }
438}