Skip to content

Instantly share code, notes, and snippets.

@shibli049
Forked from rivancic/Elastic-Spring-Data.md
Created July 12, 2018 05:27
Show Gist options
  • Save shibli049/2ab50c6fc59b6c7b11e2f285208d8cd6 to your computer and use it in GitHub Desktop.
Save shibli049/2ab50c6fc59b6c7b11e2f285208d8cd6 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.

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