Elasticsearch (ES) {Elasticsearch (ES) {Elasticsearch (ES) {Elasticsearch (ES) {Elasticsearch (ES);}}
1. Initialize the project
We’ll start by creating a Spring-Boot Web project, which you can do on your own. Elasticsearch dependencies can be modified to match the version of elasticSearch installed.
Maven:
<! -- elasticsearch & rest-high-level-client --> <dependency> <groupId>org.elasticsearch.client</groupId> < artifactId > elasticsearch - the rest - high - level - client < / artifactId > < version > 7.12.0 < / version > < / dependency >Copy the code
Gradle:
Implementation Group: 'org.elasticSearch. client', name:' ElasticSearch-rest-high-level client', version: '7.12.0'Copy the code
Added the following configuration:
Elasticsearch: cluster-name: elasticsearch cluster-Nodes: 127.0.0.1:9200 index: elasticSearch: cluster-name: elasticsearch cluster-nodes: 127.0.0.1:9200 index: number-of-replicas: 2 number-of-shards: 3 account: username: password:Copy the code
In addition, you need to create a configuration class to connect to the Elasticsearch client:
@Data @Builder @Component @NoArgsConstructor @AllArgsConstructor @ConfigurationProperties(prefix = "elasticsearch") Public class ElasticsearchProperties {/** * private String schema = "HTTP "; /** * Private String clusterName = "ElasticSearch "; /** * clusterNodes */ @notnull (message = "clusterNodes cannot be empty ") private List<String> clusterNodes = new ArrayList<>(); /** * Connection timeout (ms) */ private Integer connectTimeout = 1000; /** * Socket timeout duration */ private Integer socketTimeout = 30000; /** * Private Integer connectionRequestTimeout = 500; /** * Maximum number of connections per route */ private Integer maxConnectPerRoute = 10; /** * Maximum number of connections */ private Integer maxConnectTotal = 30; /** * private Index Index = new Index(); /** * private Account Account = new Account(); @data public static class Index {/** * number of fragments */ private Integer numberOfShards = 3; /** * numberOfReplicas */ private Integer numberOfReplicas = 2; } /** * @data public static class Account {/** * private String username; /** * Authentication password */ private String password; }}Copy the code
@Configuration
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@EnableConfigurationProperties(ElasticsearchProperties.class)
public class ElasticsearchConfiguration {
private final ElasticsearchProperties elasticsearchProperties;
private final List<HttpHost> httpHosts = new ArrayList<>();
@Bean(name = "restHighLevelClient")
@ConditionalOnMissingBean
public RestHighLevelClient restHighLevelClient() {
List<String> clusterNodes = elasticsearchProperties.getClusterNodes();
clusterNodes.forEach(node -> {
try {
String[] parts = StringUtils.split(node, ":");
Assert.notNull(parts, "Must defined");
Assert.state(parts.length == 2, "Must be defined as 'host:port'");
httpHosts.add(new HttpHost(parts[0], Integer.parseInt(parts[1]), elasticsearchProperties.getSchema()));
} catch (Exception e) {
throw new IllegalStateException("Invalid ES nodes " + "property '" + node + "'", e);
}
});
RestClientBuilder builder = RestClient.builder(httpHosts.toArray(new HttpHost[0]));
return getRestHighLevelClient(builder, elasticsearchProperties);
}
/**
* get restHistLevelClient
*
* @param builder RestClientBuilder
* @param elasticsearchProperties elasticsearch default properties
* @return {@link RestHighLevelClient}
* @author fxbin
*/
private static RestHighLevelClient getRestHighLevelClient(RestClientBuilder builder, ElasticsearchProperties elasticsearchProperties) {
// Callback used the default {@link RequestConfig} being set to the {@link CloseableHttpClient}
builder.setRequestConfigCallback(requestConfigBuilder -> {
requestConfigBuilder.setConnectTimeout(elasticsearchProperties.getConnectTimeout());
requestConfigBuilder.setSocketTimeout(elasticsearchProperties.getSocketTimeout());
requestConfigBuilder.setConnectionRequestTimeout(elasticsearchProperties.getConnectionRequestTimeout());
requestConfigBuilder.setAuthenticationEnabled(true);
return requestConfigBuilder;
});
// Callback used the basic credential auth
final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
ElasticsearchProperties.Account account = elasticsearchProperties.getAccount();
if (!StringUtils.isEmpty(account.getUsername()) && !StringUtils.isEmpty(account.getUsername())) {
credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(account.getUsername(), account.getPassword()));
}
// Callback used to customize the {@link CloseableHttpClient} instance used by a {@link RestClient} instance.
builder.setHttpClientConfigCallback(httpClientBuilder -> {
httpClientBuilder.setMaxConnTotal(elasticsearchProperties.getMaxConnectTotal());
httpClientBuilder.setMaxConnPerRoute(elasticsearchProperties.getMaxConnectPerRoute());
httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
return httpClientBuilder;
});
return new RestHighLevelClient(builder);
}
}
Copy the code
Then, start the Elasticsearch client first and then the project. Access the interface /actuator/ Health to check elasticSearch connections:
{ "status":"UP", "components":{ "elasticsearchRest":{ "status":"UP", "details":{ "cluster_name":"elasticsearch", "status":"yellow", "timed_out":false, "number_of_nodes":1, "number_of_data_nodes":1, "active_primary_shards":40, "active_shards":40, "relocating_shards":0, "initializing_shards":0, "unassigned_shards":79, "delayed_unassigned_shards":0, "number_of_pending_tasks":0, "number_of_in_flight_fetch":0, "Task_max_waiting_in_queue_millis" : 0, "active_shards_percent_as_number" : 33.61344537815126}}, "ping":{ "status":"UP" } } }Copy the code
2. Create or delete indexes
For Elasticsearch indexes, check to see if the index exists, create the index, and drop the index. For index operation, we need to use client.indices() method to obtain IndicesClient instance and perform related operation on index.
@Autowired
@Qualifier(value = "restHighLevelClient")
protected RestHighLevelClient client;
protected static final RequestOptions COMMON_OPTIONS;
private void checkIndexExist(String index) {
try {
GetIndexRequest indexRequest = new GetIndexRequest(index);
boolean exists = client.indices().exists(indexRequest, COMMON_OPTIONS);
if (!exists) {
createIndexRequest(index);
}
} catch (IOException e) {
log.error("Failed to check index. {}", index, e);
}
}
Copy the code
/** * create elasticsearch index (asyc) * * @param index elasticsearch index */ protected void createIndexRequest(String index) { try { CreateIndexRequest request = new CreateIndexRequest(index); // Settings for this index request.settings(Settings.builder() .put("index.number_of_shards", elasticsearchProperties.getIndex().getNumberOfShards()) .put("index.number_of_replicas", elasticsearchProperties.getIndex().getNumberOfReplicas())); CreateIndexResponse createIndexResponse = client.indices().create(request, COMMON_OPTIONS); log.info(" whether all of the nodes have acknowledged the request : {}", createIndexResponse.isAcknowledged()); log.info(" Indicates whether the requisite number of shard copies were started for each shard in the index before timing out :{}", createIndexResponse.isShardsAcknowledged()); } catch (IOException e) { log.error("failed to create index. ", e); Throw new ElasticsearchException(" create index {" + index + "} failed "); }}Copy the code
/** * delete elasticsearch index * * @param index elasticsearch index name */ protected void deleteIndexRequest(String index) { DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest(index); try { client.indices().delete(deleteIndexRequest, COMMON_OPTIONS); } catch (IOException e) { log.error("failed to delete index. ", e); Throw new ElasticsearchException(" delete index {" + index + "} failed "); }}Copy the code
Select * from Elasticsearch;_doc
)
For Elasticsearch documents (_doc), there are four CRUD based operations. They correspond to four Request objects:
IndexRequest Request = new IndexRequest(index) // Document id.id (ID) // Document source data.source (jsonUtil.tojsonstr (object), XContentType.JSON); Index (request, COMMON_OPTIONS); // Create file client.index(request, COMMON_OPTIONS);Copy the code
GetRequest GetRequest = new GetRequest(esconstant.index_name) // Index name // id.id (string.valueof (ID)); GetResponse response = client.get(getRequest, COMMON_OPTIONS);Copy the code
UpdateRequest = new UpdateRequest(index, id).doc(beanutil.beantomap (object), XContentType.JSON); Update (updateRequest, COMMON_OPTIONS);Copy the code
DeleteRequest = new DeleteRequest(index, ID); Delete (deleteRequest, COMMON_OPTIONS);Copy the code
conclusion
This article gives you a brief introduction and demonstrates how to initialize our test project, so you can try to build one yourself. We’ll cover various implementations of the Elasticsearch search scenario in more detail later.
link
- Project address: github.com/lq920320/es…
- Reference items: github.com/xkcoding/sp…