Background: ElasticSearch Java client connection ElasticSearch Based on this blog
ElasticSearch: brief introduction and deployment of ElasticSearch and Kibana using Docker This blog simply deploys ElasticSearch
Brief introduction to index concept
Generally speaking, the index has two parts of speech, name and verb.
- Verb indexing, indexing a document, means to store a document in the Index index, which can be used for query and retrieval. es adopts inverted Index
- Noun index is simply understood as the concept of database in relational database
Create index
static void createIndex(RestHighLevelClient client){ // Create index - Request object CreateIndexRequest request = new CreateIndexRequest("userxt"); // Send request and get response CreateIndexResponse response = null; try { response = client.indices().create(request, RequestOptions.DEFAULT); } catch (IOException e) { e.printStackTrace(); } boolean acknowledged = response.isAcknowledged(); // Response status System.out.println("Operation status = " + acknowledged); }
The index was created successfully as a result of the call
Friends who have updated to Elastic stack version 7.13 or above may have noticed that when the Elastic security function is not enabled by default, Kibana's search results page will be prompted with an additional line, suggesting that we enable the Elastic search security function.
In the case of personal learning or ES+VPN connection on the intranet, we do not need to turn on the security function at all. In other cases, it is recommended to turn on the security option in the production cluster.
This is because the security option is not explicitly disabled. In other words, ElasticSearch will prompt you whether you forgot to enable this option. You can cancel this prompt as long as you explicitly disable it in the configuration file.
In ElasticSearch YML configuration disables the security option xpack security. Enabled, and then restart ElasticSearch:
xpack.security.enabled: false
Restart container
docker restart Container name/container ID
Query index
static void searchIndex(RestHighLevelClient client){ // Query index - Request object GetIndexRequest request = new GetIndexRequest("userxt"); // Send request and get response GetIndexResponse response = null; try { response = client.indices().get(request, RequestOptions.DEFAULT); } catch (IOException e) { e.printStackTrace(); } System.out.println("aliases:"+response.getAliases()); System.out.println("mappings:"+response.getMappings()); System.out.println("settings:"+response.getSettings()); }
The calling result prints out the information such as mappings, settings and aliases
You can also see the newly created index in ElasticSearch Head plug-in, but as shown in the figure below, it is found that ElasticSearch is in sub-health state and some fragments have not been allocated
From the screenshot above, we can see that there are unassigned partitions. When creating an index, the number of partitions is 1 and the number of replicas is 1. After creating a new index, the cluster status becomes yellow. The fundamental reason is that there are replica partitions that are not enabled in the cluster. Let's take a look at the introduction of the secondary partition on the official website first:
The main purpose of replica sharding is for failover. As discussed in the principle within the cluster: if the node holding the primary shard dies, a replica shard will be promoted to the role of the primary shard.
It can be seen that replica shards and primary shards cannot be placed on one node, but in a cluster with only one node, replica shards cannot be allocated to other nodes, so all replica shards are unassigned. Because there is only one node, if the primary partition node hangs up, the whole cluster should hang up. There is no case that the replica partition is promoted to the primary partition.
The solution is to delete the index with replica fragmentation in the single node elastic search cluster, and set the replica of the new index to 0. Or set up a multi node ElasticSearch cluster
Delete index
static void deleteIndex(RestHighLevelClient client){ // Delete index - Request object DeleteIndexRequest request = new DeleteIndexRequest("userxt"); // Send request and get response AcknowledgedResponse response = null; try { response = client.indices().delete(request, RequestOptions.DEFAULT); } catch (IOException e) { e.printStackTrace(); } // Operation results if(response.isAcknowledged()==true){ System.out.println("Index deleted successfully"); }else{ System.out.println("Failed to delete index"); } }
Call result
Determine whether the specified index exists
static boolean existsIndex(RestHighLevelClient client,String indexName){ GetIndexRequest request = new GetIndexRequest(indexName); request.local(false); request.humanReadable(true); request.includeDefaults(false); try { return client.indices().exists(request, RequestOptions.DEFAULT); } catch (IOException e) { e.printStackTrace(); return false; } }
Call result
Complete code
public class ElasticsearchConnect { public static void main(String[] args) throws IOException { final CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("xt", "xt")); RestClientBuilder restClientBuilder = RestClient.builder( new HttpHost("IP", 9200,"http")); // Authentication and number of threads restClientBuilder.setHttpClientConfigCallback(httpClientBuilder -> { httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider); int threadCount = 10; httpClientBuilder.setDefaultIOReactorConfig(IOReactorConfig.custom().setIoThreadCount(threadCount).build()); return httpClientBuilder; }); // Timeout setting restClientBuilder.setRequestConfigCallback(requestConfigCallback -> { requestConfigCallback.setConnectTimeout(10); requestConfigCallback.setSocketTimeout(10); return requestConfigCallback; }); // Although the creation of client objects has been abandoned, there is still no problem in understanding the basic use. It encapsulates RestClient RestHighLevelClient client = new RestHighLevelClient(restClientBuilder); System.out.println(client); //Create index createIndex(client,"userxt"); //Gets the information for the specified index searchIndex(client,"userxt"); //Deletes the specified index deleteIndex(client,"userxt"); if(existsIndex(client,"userxt")==true){ System.out.println("Indexes"+"userxt"+"existence"); }else{ System.out.println("Indexes"+"userxt"+"non-existent"); } // Close client connection client.close(); } static void createIndex(RestHighLevelClient client,String indexName){ // Create index - Request object CreateIndexRequest request = new CreateIndexRequest(indexName); // Send request and get response CreateIndexResponse response = null; try { response = client.indices().create(request, RequestOptions.DEFAULT); } catch (IOException e) { e.printStackTrace(); } if(response.isAcknowledged()==true){ System.out.println("New index"+indexName+"success"); }else{ System.out.println("New index+"+indexName+"fail"); } } static void searchIndex(RestHighLevelClient client,String indexName){ // Query index - Request object GetIndexRequest request = new GetIndexRequest("userxt"); // Send request and get response GetIndexResponse response = null; try { response = client.indices().get(request, RequestOptions.DEFAULT); } catch (IOException e) { e.printStackTrace(); } System.out.println("aliases:"+response.getAliases()); System.out.println("mappings:"+response.getMappings()); System.out.println("settings:"+response.getSettings()); } static boolean existsIndex(RestHighLevelClient client,String indexName){ GetIndexRequest request = new GetIndexRequest(indexName); request.local(false); request.humanReadable(true); request.includeDefaults(false); try { return client.indices().exists(request, RequestOptions.DEFAULT); } catch (IOException e) { e.printStackTrace(); return false; } } static void deleteIndex(RestHighLevelClient client,String indexName){ // Delete index - Request object DeleteIndexRequest request = new DeleteIndexRequest(indexName); // Send request and get response AcknowledgedResponse response = null; try { response = client.indices().delete(request, RequestOptions.DEFAULT); } catch (IOException e) { e.printStackTrace(); } // Operation results if(response.isAcknowledged()==true){ System.out.println("Delete index+"+indexName+"success"); }else{ System.out.println("Delete index"+indexName+"fail"); } } }
References:
- https://www.elastic.co/guide/en/elasticsearch/reference/7.16/security-minimal-setup.html
- https://www.cnblogs.com/novwind/p/15145943.html
- https://blog.csdn.net/x4609883/article/details/79926267
(blogging is mainly to summarize and sort out their own learning. Most of the materials come from books, online materials, official documents and their own practice. Please comment and correct the shortcomings and mistakes in the sorting. At the same time, thank the bloggers and authors for their hard sorting out resources and shared knowledge.)