# elastic4s **Repository Path**: dblike/elastic4s ## Basic Information - **Project Name**: elastic4s - **Description**: No description available - **Primary Language**: Unknown - **License**: Apache-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2015-11-08 - **Last Updated**: 2020-12-19 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README elastic4s - Elasticsearch Scala Client ========= [![Join the chat at https://gitter.im/sksamuel/elastic4s](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/sksamuel/elastic4s?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) [![Build Status](https://travis-ci.org/sksamuel/elastic4s.png)](https://travis-ci.org/sksamuel/elastic4s) Elastic4s is mostly a wrapper around the standard Elasticsearch Java client with the intention of creating a concise, idiomatic, reactive, type safe DSL for applications in Scala that use Elasticsearch. The Java client, which can of course be used directly in Scala, is more verbose due to Java's nature. Scala lets us do better. Elastic4s's DSL allows you to to construct your requests programatically, with syntatic and semantic errors manifested at compile time, and uses standard Scala futures to enable you to easily integrate into your existing asynchronous workflow. The aim of the DSL is that requests are written in an SQL-like way, while staying true to the Java API or Rest API. Elastic4s supports Scala collections so you don't have to do tedious conversions from your Scala domain classes into Java collections. It also allows you to index documents directly without having to extract and set fields manually - eg from a case class, a JSON document, or a Map (or a custom source). Due to its type safe nature, it is easy to see what operations are available for any request type, because your IDE can use type information to show what methods are available. #### Key points * Type safe concise DSL * Integrates with standard Scala futures * Uses Scala collections library * Leverages the built-in Java client * Provides [reactive-streams](#elastic-reactive-streams) implementation * SQL-style requests #### Release The latest release is 1.7.4 which is compatible with Elasticsearch 1.7.x. There are releases for both Scala 2.10 and Scala 2.11. For releases that are compatible with earlier versions of Elasticsearch, [search maven central](http://search.maven.org/#search|ga|1|g%3A%22com.sksamuel.elastic4s%22). For more information read [Using Elastic4s in your project](#using-elastic4s-in-your-project). |Elastic4s Release|Target Elasticsearch version| |-------|---------------------| |1.7.4|1.7.X| |1.6.6|1.6.X| |1.5.17|1.5.X| |1.4.14|1.4.x| |1.3.3|1.3.x| |1.2.3.0|1.2.x| |1.1.2.0|1.1.x| |1.0.3.0|1.0.x| |0.90.13.2|0.90.x| ##### Changelog ###### 2.0.0 (in progress) * Updated to Elasticsearch 2.0.0. See upgrade guide in next section on breaking changes. ###### 1.7.0 * Works with Elasticsearch 1.7.x * Removed sync client (deprecated since 1.3.0) ###### 1.6.6 * Fix for race condition in elastic-streams subscriber ###### 1.6.5 * Added sourceAsUpsert to allow `Indexable` as upsert in update queries * Added geohash aggregation * Added geohash cell filter * Added cluster state api * Added support for unmapped_type property * Added block until index/type exists testkit helpers * Added raw query to count dsl * Added `show` typeclass for count * Added `InFilter` and `IndicesFilter` * Added shorter syntax for field types, eg `stringField(name)` vs `field name typed StringType` ###### 1.6.4 * Added reactive streams implementation for elastic4s. * Support explicit field types in the update dsl * Added missing options to restore snapshot dsl * Added `show` typeclass for percolate register ###### 1.5.17 * Added clear scroll api * Added missing options to restore snapshot dsl ###### 1.6.3 * Added clear scroll api * Added `show` typeclass for multisearch * Allow update dsl to use explicit field values ###### 1.5.16 * Added `HitAs` as a replacement for the `Reader` typeclass * Added indices option to mapping, count and search dsl * Added docValuesFormat to timestamp mapping ###### 1.6.2 * Added new methods to testkit * Introduced simplier syntax for sorts * Added `HitAs` as a replacement for the `Reader` typeclass * Fixed validate query for block queries * Added `show` typeclasses for search, create index, into into, validate, count, and percolate to allow easy debugging of the json of requests. ###### 1.5.15 * Added `matched_fields` and highlight filter to highlighter ###### 1.6.1 * Added IterableSearch for iterating over a scroll * Enhanced multiget dsl to include `routing`, `version` and `field` options * Added rich result for GetAliasResponse * Added context queries to suggestions * Breaking change: Changed syntax of suggestions to be clearer and allow for type safe results * Allow setting analyzer by name on matchphraseprefix * Added singleMethodSyntax variant, eg `indexInto(index)` rather than `index into index` * Added re-write to validate * Added filter support to alias (previously only the java client filters were supported) * Added cluster settings api * Added field stats api * Addd `docValuesFormat` to timestamp mapping * Added `matched_fields` and highlight filter to highlighter * Supported `stopwords_list` in filter * Reworked testkit to allow more configuration over the creating of the test clients #### Dependencies Starting from version 1.5.13 the main artifact has been renamed to elastic4s-core_2.x. Please update your build scripts. There is now an elastic4s-testkit_2.x which brings in a couple of useful methods for waiting until the node/cluster is in some expected state. Very useful when trying unit tests. If you previously used the Jackson support for DocumentSource or Indexables then you need to add a new dependency elastic4s-jackson_2.x. This will allow you to do `import ElasticJackson.Implicits._` which puts a Jackson based `Indexable` into scope, which allows any class to be indexed automagically without the need to manually create maps or json objects. Similarly, if you are using `response.hitsAs[T]`, then the same import brings in a `Reader` that will convert any type to a case class. ## Introduction The basic usage of the Scala driver is that you create an instance of `ElasticClient` and then invoke the various `execute` methods with the requests you want to perform. The execute methods are asynchronous and will return a standard Scala `Future[T]` where T is the response type appropriate for your request type. For example a search request will return a response of type `SearchResponse` which contains the results of the search. Requests, such as inserting a document, searching, creating an index, etc, are created using the DSL syntax that is similar in style to SQL queries. For example to create a search request, you would do: `search in "index/type" query "findthistext"` The response objects are, for the most part, the exact same type the Java API returns. This is because there is mostly no reason to wrap these as they are fairly easy to use in Scala. All the DSL keywords are located in the `ElasticDsl` trait which needs to be imported or extended. An example is worth 1000 characters so here is a quick example of how to create a local node with a client and index a one field document. Then we will search for that document using a simple text query. ```scala import com.sksamuel.elastic4s.ElasticClient import com.sksamuel.elastic4s.ElasticDsl._ object Test extends App { val client = ElasticClient.local // await is a helper method to make this operation synchronous instead of async // You would normally avoid doing this in a real program as it will block your thread client.execute { index into "bands" / "artists" fields "name"->"coldplay" }.await // we need to wait until the index operation has been flushed by the server. // this is an important point - when the index future completes, that doesn't mean that the doc // is necessarily searchable. It simply means the server has processed your request and the doc is // queued to be flushed to the indexes. Elasticsearch is eventually consistent. // For this demo, we'll simply wait for 2 seconds (default refresh interval is 1 second). Thread.sleep(2000) // now we can search for the document we indexed earlier val resp = client.execute { search in "bands" / "artists" query "coldplay" }.await println(resp) } ``` For more in depth examples keep reading. ## 2.0.0 upgrade guide (beta) * In elasticsearch 2.0.0 one of the major changes has been filters have become queries. So in elastic4s this means all methods `xxxFilter` are now `xxxQuery`, eg `hasChildrenFilter` is now `hasChildrenQuery`. * Some options on filters like cache and cache key are now removed. * In queries that were previously filters, `filterName` is now `queryName`. * Fuzzy like this query has been removed. * Script dsl has changed ... todo more * Index_analyzer has been removed in elasticsearch. Use analyzer and then override the analyzer for search with search_analyzer * MoreLikeThis was removed from elasticsearch in favour of a `moreLikeThisQuery` on a search request. * `moreLikeThisQuery` has changed camel case (capital L), also now requires the 'like' text as the 2nd method, eg `moreLikeThisQuery("field").text("a")` (both can take varargs as well). * Search requests now return a richer response type. Previously it returned the java type. The richer type has java style methods so your code will continue to compile, but with deprecation warnings. * The sorting DSL has changed in that the previous infix style methods are deprecated. So `field sort x` becomes `fieldSort(x)` etc. * Or and And filters have been removed completely (not changed into queries like other filters). Use a bool query with `must` clauses for ands and `should` clauses for ors. * Highlight dsl has changed slightly, `highlight field x` is now deprecated in favour of `highlight(x)` * Delete mapping has been removed * IndexStatus api has been removed * Field and mapping syntax has changed slightly. The implicit `"fieldname" as StringType ...` has been deprecated in favour of `field("fieldname", StringType)` ## Syntax Here is a list of the common requests and the syntax used to create them. For more details on each request click through to the readme page. For options that are not yet documented, refer to the Elasticsearch documentation as the DSL closely mirrors the standard Java API / REST API. | Operation | Syntax | |-------------------------------------------|----------------| | [Add Alias](guide/aliases.md) | `add alias "" on ""` | | Clear index cache | `clear cache ` | | Close index | `close index ` | | [Count](guide/count.md) | `count from types ` | | Cluster health | `get cluster health` | | Cluster stats | `get cluster stats` | | [Create Index](guide/createindex.md) | `create index mappings { mappings block> } [settings]`| | [Create Repository](guide/snapshot.md) | `create repository type settings ` | | [Create Snapshot](guide/snapshot.md) | `create snapshot in ...` | | Create Template | `create template pattern mappings {...}` | | [Delete by id](guide/delete.md) | `delete id from [settings]` | [Delete by query](guide/delete.md) | `delete from query { } [settings]` | [Delete index](guide/delete.md) | `delete index [settings]` | Delete Mapping | `delete mapping ` | | [Delete Snapshot](guide/snapshot.md) | `delete snapshot in ...` | | Delete Template | `delete template ` | | [Explain](guide/explain.md) | `explain id in query { }` | Field stats | `field stats ` | | Flush Index | `flush index ` | | [Get](guide/get.md) | `get id from [settings]` | | Get Alias | `get alias on ` | | Get Mapping | `get mapping / ` | | Get Segments | `get segments ` | | Get Snapshot | `get snapshot from ` | | Get Template | `get template ` | | [Index](guide/index.md) | `index into fields { } [settings]` | | Index exists | `index exists ` | | Index Status | `index status ` | | More like this | `morelike id in { fields } [settings]` | | [Multiget](guide/multiget.md) | `multiget ( get id 1 from index, get id 2 from index, ... )` | | [Multisearch](guide/multisearch.md) | `multi ( search ..., search ..., ...)`| | Open index | `open index ` | | [Optimize](guide/optimize.md) | `optimize index "indexname" [settings]` | | Percolate Doc | `percolate in { fields }` | | Put mapping | `put mapping / add { mappings block }` | | Recover Index | `recover index ` | | Refresh index | `refresh index ` | | Register Query | `register id into query { }` | | [Remove Alias](guide/aliases.md) | `remove alias "" on ""` | | [Restore Snapshot](guide/snapshot.md) | `restore snapshot from ...` | | [Search](guide/search.md) | `search in query ... postFilter ... sort ...` | | Search scroll | `search scroll ` | | Type Exists | `types exists in ` | | [Update](guide/update.md) | `update id in script