workday / escalar   1.7.0

MIT License GitHub

A Scala client for Elasticsearch’s HTTP API which presents Scala-idiomatic, typed interface. Elasticsearch exposes an HTTP API with several community-provided language bindings. A Java client exists for this HTTP API called Jest, but there is no Scala API. This client is currently implemented as a wrapper around Jest.

Scala versions: 2.11 2.10

escalar

A Scala REST Client for Elasticsearch.

Overview

Escalar is a Scala project you can use to make requests over HTTP to your Elasticsearch cluster using Elasticsearch's REST API. This client supports a number of features of the Elasticsearch REST API. Create indices, index documents, create and restore from snapshots, manage your cluster health, and much more using Escalar.

We currently support v1.7 of the REST API, but we are currently porting our client to v5.x in the near future.

Build Status Code Coverage Maven Central

How-to

First add escalar as a dependency in your build.sbt file. Currently escalar is cross-built against Scala 2.10 and 2.11:

libraryDependencies += "com.workday" %% "escalar" % "1.7.0"

Build a client like this:

import com.workday.esclient._
val esUrl = "http://localhost:9200"
val client = EsClient.createEsClient(esUrl)

And shutdown with:

client.shutdownClient()

Create an index, index a document, and retrieve that same document:

val indexName = "presidents" //index name
val typeName = "president" //type for documents
val id = "1" //document ID
val doc = "{\"first_name\":\"George\", \"last_name\":\"Washington\", \"home_state\":\"Virginia\"}" //actual document to index
client.createIndex(indexName) //creates index in ES
client.index(indexName, typeName, id, doc) //indexes doc to that index
val getDoc = client.get(indexName, id) //get the doc within an EsResponse object

Get more sophisticated by adding a few more presidents to our "presidents" index and query by home state:

val doc2 = "{'first_name':'Thomas', 'last_name':'Jefferson', 'home_state':'Virginia'}"
val doc3 = "{'first_name':'Abraham', 'last_name':'Lincoln', 'home_state':'Illinois'}"
val doc4 = "{'first_name':'Theodore', 'last_name':'Roosevelt', 'home_state':'New York'}"
val bulkActions = Seq(UpdateDocAction(indexName, typeName, "2", doc2), 
                      UpdateDocAction(indexName, typeName, "3", doc3), 
                      UpdateDocAction(indexName, typeName, "4", doc4))
client.bulkWithRetry(bulkActions) //bulk add documents
client.search(indexName, typeName, "{\"query\": {\"query_string\": {\"query\": \"New York\"}}}") //search the "presidents" index for documents with the text "Virginia"

Finally remove all the docs we just created and delete the index:

client.deleteDocsByQuery(indexName) //defaults to "match_all" query
client.deleteIndex(indexName)

Building, Testing, and Contributing

This is an SBT-based project, so building and testing locally is done simply by using:

sbt clean coverage test

Generate the code coverage report with:

sbt coverageReport

This project aims for 100% test coverage, so any new code should be covered by test code.

To contribute, first read our contributing documentation. Create a separate branch for your patch and get a passing CI build before submitting a pull request.

Documentation

The full documentation can be found on Sonatype.

Dependencies

We use the following dependencies:

FasterXML jackson for JSON parsing. We're planning changing this dependency to Circe in the future, but Jackson will remain as our primary JSON parser in the meantime.

Jest is our core Java HTTP REST client for ES.

Google Gson is used to serialize/deserialize Java objects to/from JSON.

Authors

Note that the commit history doesn't accurately reflect authorship, because much of the code was ported from an internal repository. Please view the Contributors List for a full list of everyone who's contributed to the project.

License

Copyright 2017 Workday, Inc.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.