项目作者: perkss

项目描述 :
Clojure ElasticSearch examples using Rest Java API Interop and Testcontainers
高级语言: Clojure
项目地址: git://github.com/perkss/clojure-elasticsearch-examples.git
创建时间: 2019-07-20T17:56:34Z
项目社区:https://github.com/perkss/clojure-elasticsearch-examples

开源协议:

下载


clojure-elasticsearch-examples

A Clojure project showing how to use the Elastic Search API.
Fun times!!

Getting Started

Now using docker-compose we can start and expose elastic search on port 9200 and also start kibana and navigate to
its UI at http://localhost:5601.

By running the command in the root directory docker-compose up -d

If you have errors please check the logs docker-compose logs or docker logs [container-id], I saw exit code 78 and
had to increase the docker daemons memory size.

Building our app into a Docker image

Now lets get ready to build our docker image by building an uberjar first and then creating a docker image from our
DockerFile. This DockerFile is fancy as it will create our Uberjar and then build our docker image.

  1. $ docker build -t elasticsearch-example .

Now its going to run the image we just created with the tag name elasticsearch-example

  1. $ docker run -it elasticsearch-example

Running Test Containers

Testcontainers make it easy to run tests which use Docker containers. In this example we can
simply run lein test and as long as Docker instance is running then it will fire up Elasticsearch and run our applicaiton test
against it. Simple.

Rest High Level Client Tutorial

Create the RestHighLevelClient in Clojure using Java interop.

  1. (defn rest-client ^RestHighLevelClient
  2. ([^String host ^Integer port] (rest-client host port "http"))
  3. ([^String host ^Integer port ^String scheme]
  4. (-> (RestHighLevelClient. (RestClient/builder (into-array HttpHost [(HttpHost. host port scheme)]))))))

Create a IndexRequest document to be indexed by the rest client.

  1. (requests/index-request index "2" (json/write-str
  2. {:user "perkss"
  3. :post-date (t/format (tick/now))
  4. :message "Clojure and Elasticsearch"}))

Function defined to execute the document IndexRequest

  1. (defn execute ^IndexResponse
  2. [^RestHighLevelClient client
  3. ^IndexRequest request
  4. ^RequestOptions request-options]
  5. (.index client request request-options))