Skip to content

Instantly share code, notes, and snippets.

@rivancic
Last active July 12, 2018 05:27
Show Gist options
  • Save rivancic/fda29eb61d7e16322b4925d5eb0ceae1 to your computer and use it in GitHub Desktop.
Save rivancic/fda29eb61d7e16322b4925d5eb0ceae1 to your computer and use it in GitHub Desktop.
Elastic Springdata Docker

ElasticSearch

Springdata

Reference

Compatible only with version 2.4. Enough for simple data storage functionality.

Dependencies:

- spring-boot-starter-data-elasticsearch : 1.5.1.RELEASE
  - org.springframework.data:spring-data-elasticsearch 	2.1.0.RELEASE
    - org.elasticsearch:elasticsearch 	2.4.0

Client

The simplest and cleanest way to set up client is with setting properties that are listed below. If for some reason this is not enough, client can be created with defining a ElasticsearchTemplate @Bean.

@Bean
public ElasticsearchTemplate elasticsearchTemplate() throws UnknownHostException {
  Settings settings = Settings.settingsBuilder().put("cluster.name", clusterName).build();
  TransportClient client = TransportClient.builder().settings(settings).build()
      .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(clusterNodesHost),
          clusterNodesPort));
  return new ElasticsearchTemplate(client);
}

Application properties

Appendix A contains list of properties

# ELASTICSEARCH (ElasticsearchProperties)
spring.data.elasticsearch.cluster-name=elasticsearch # Elasticsearch cluster name.
spring.data.elasticsearch.cluster-nodes= # Comma-separated list of cluster node addresses. If not specified, starts a client node.
spring.data.elasticsearch.properties.*= # Additional properties used to configure the client.
spring.data.elasticsearch.repositories.enabled=true # Enable Elasticsearch repositories.

Docker image

Docker Hub

docker pull elasticsearch:2.4-alpine

Clients

The NodeClient and the TranspotClient are basic Java clients. They can not be accessed by Perl, Python, Ruby etc. Link

9200

The REST HTTP client exposes the NodeClient so that ES can be used by all languages who have a REST client. i.e. curl which listens at 9200.

9300

Transport Client or Node Client that listens at 9300. The advantage of the TransportClient is that remote JVM can connect to ES cluster(s) over configurable network interface without becoming a full cluster node.

Queries

Talking to Elasticsearch

Number of documents: curl -XGET 'url:9200/_count?pretty'

Check clusters: curl -XGET 'url:9200/_cat/indices?v'

Check health: curl -XGET 'url:9200/_cat/health?v'

Get document: curl -XGET 'url:9200/{index}/{type}/{ID}?pretty'

Delete index: curl -XDELETE 'url:9200/{index}?pretty'

Create index: curl -XPUT 'url:9200/{index_name}?pretty'

Explicitly specify properties

curl -XPUT 'http://url:9200/articles?pretty' -H 'Content-Type: application/json' -d'
{
    "settings" : {
        "index" : {
            "number_of_shards" : 5, 
            "number_of_replicas" : 1 
        }
    }
}
'

List indices: curl -XGET '172.17.0.2:9200/_cat/indices'

Investigating

Get cluster health

curl -XGET 'http://url:9200/_cat/health?v'

epoch      timestamp cluster       status node.total node.data shards pri relo init unassign pending_tasks max_task_wait_time active_shards_percent
1502975492 13:11:32  cluster-name red             3         1      0   0    0  0       12             0                  -                  0.0%

Status read tells us that the cluster is not healthy. We need to fix it.

Get indices status

bash-4.3# curl -XGET 'http://url:9200/_cat/indices'
red open index1  1 1
red open index2 5 1

We see that both indices are red

Recreation of indices

Recreation = delete index + create index

Check commands above.

@rivancic
Copy link
Author

rivancic commented Apr 20, 2017

  1. Write about model annotation index, type, id
  2. Write about repository and methods/operations..
  3. Write about integration test

@maxtuzz
Copy link

maxtuzz commented May 9, 2017

Do you just use a standard Docker link to enable your Spring app talk to your Elastic cluster? If so - what does your application.properties look like once configured?

@rivancic
Copy link
Author

@maxtuzz, I have it in application.yml:

spring:
  data:
    elasticsearch:
      cluster-name: ${ELASTIC_CLUSTER_NAME:cluster-name}
      cluster-nodes: ${ELASTIC_CLUSTER_NODE_HOST:172.17.0.3}:${ELASTIC_CLUSTER_NODE_PORT:9300}
      repositories:
        enabled: true

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment