Skip to content

bsadeh/scalastic

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Scalastic

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.

Installation

Add the following to your sbt build:

libraryDependencies += "org.scalastic" %% "scalastic" % "0.90.10.1"

Please note, Scalastic supports Scala 2.10.x only.

Way cool, but how do I use it?

In general, look at the test sources for usage examples.

Connect to an ElasticSearch cluster

The main dude is the Indexer:

import scalastic.elasticsearch._

val indexer = Indexer.<some creation method>

Using node-based access:

val indexer = Indexer.local.start
val indexer = Indexer.using(settings) // String or Map
val indexer = Indexer.at(node)

Using a transport client:

val indexer = Indexer.transport(settings = Map(...), host = "...")

General API structure

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-calls employ named parameters and provide default values - you only need to provide what differs.

Indexing

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]

Searching

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 */)

Testing

Try mixing in the UsingIndexer trait

Building from source

  • Scala 2.10
  • sbt 0.12.3

Versioning scheme

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.

Contributors

License

This software is available under Apache 2 license.