Scala driver for ElasticSearch
Scalastic is an interface for ElasticSearch, designed to provide more flexible and Scala-esque interface around the native ElasticSearch Java API.
Add the following to your sbt build:
libraryDependencies += "org.scalastic" %% "scalastic" % "0.90.10.1"
Please note, Scalastic supports Scala 2.10.x only.
In general, look at the test sources for usage examples.
The main dude is the Indexer
:
import scalastic.elasticsearch._
val indexer = Indexer.<some creation method>
val indexer = Indexer.local.start
val indexer = Indexer.using(settings) // String or Map
val indexer = Indexer.at(node)
val indexer = Indexer.transport(settings = Map(...), host = "...")
Just about every Indexer
API call has these forms:
indexer.<api-call> // a blocking call
indexer.<api-call>_send // async call
indexer.<api-call>_prepare // get the builder and tailor it all to your heart's content
api-call
s employ named parameters and provide default values - you only need to provide what differs.
val indexType = "subnet"
val mapping = s"""
{
"$indexType": {
"properties" : {
"from" : {"type": "ip"},
"to" : {"type": "ip"}
}
}
}
"""
val indexName = "networks"
indexer.createIndex(indexName, settings = Map("number_of_shards" -> "1"))
indexer.waitTillActive()
indexer.putMapping(indexName, indexType, mapping)
indexer.index(indexName, indexType, "1", """{"from":"192.168.0.5", "to":"192.168.0.10"}""")
indexer.refresh()
- for an atomic total-reindexing operation, see
indexer.reindexWith
method - for syncing with indexing operations on a type (index/delete), see the family of methods in the
WaitingForGodot
trait:
indexer.waitTillCount[AtLeast | Exactly | AtMost]
import org.elasticsearch.index.query.QueryBuilders._
indexer.search(query = boolQuery
.must(rangeQuery("from") lt "192.168.0.7")
.must(rangeQuery("to") gt "192.168.0.7"))
or:
import org.elasticsearch.index.query.QueryBuilder
val searchQuery: QueryBuilder = ...
val response = indexer.search(indices = List("index1", "indexN"),
query = searchQuery,
from = 100,
size = 25 /* and so on */)
Try mixing in the UsingIndexer
trait
- Scala 2.10
- sbt 0.12.3
Scalastic versions correspond to ElasticSearch versions (starting from 0.90.0
binaries are available via Maven repo)
with a small addition - the fourth component of the version is used to reflect Scalastic improvements/bug fixes.
For example: given ElasticSearch 0.90.0 - Scalastic versions will be 0.90.0, 0.90.0.1, 0.90.0.2 and so on.
- Benny Sadeh benny.sadeh@gmail.com
- Ivan Yatskevich (github)
- you?
This software is available under Apache 2 license.