srsp / elastic-feeder   1.1.0

Apache License 2.0 GitHub

Feed data into ElasticSearch the easy way

Scala versions: 2.11 2.10


Build Status

What does it do?

ElasticFeeder is a very small library that helps you to get data into ElasticSearch quickly. It is written in Scala, but it may work for Java, too.

How does it work?

Add ElasticFeeder to your build.sbt:

libraryDependencies += "com.spruenker" %% "elastic-feeder" % "1.1.0"

Create a new elastic-feeder object:

val feeder = new ElasticFeeder(_index = "my-index")

This will generate give you a feeder that tries to use an ElasticSearch server on localhost:9300. If you want to use another server:

val feeder = new ElasticFeeder("", 9123, "my-index")

Now you have to give the feeder some data. You can feed it with Sequences (e.g. List) of objects, that implement the IdDocumentMap trait, which is really simple. It needs implementations of two methods:

/** Simplified. **/
trait IdDocumentMap {
  def _id: String
  def map: Map[String, Any]

The result of the _id-function will be used as the id of the document in ElasticSearch. The map-function just maps all the properties of your model to the properties you want in your ElasticSearch document. Here is an example:

case class Order(contractNumber: Long, eventDate: Date, clerkId: String, brandId: Integer, signed: String, url: String) extends IdDocumentMap {

  override def _id: String = contractNumber.toString

  override def map: Map[String, Any] = {
    "contractNumber" -> contractNumber,
    "eventDate" -> eventDate,
    "clerkId" -> clerkId,
    "brandId" -> brandId,
    "signed" -> { if (signed.equals("N")) false else true },
    "url" -> url

As you can see, you can even transform properties from your case class to something you want in your ElasticSearch document.

Let's save some Orders into ElasticSearch:

val orders: List[Order] = getOrders

That's it. ElasticFeeder will take your list of Orders and feed it to ElasticSearch. It uses one or more bulk operations to do that to keep things fast.


ElasticFeeder is written in Scala and built with sbt:

sbt compile