https://reflectoring.io/spring-boot-elasticsearch
docker run -p 9200:9200 \
-e "discovery.type=single-node" \
docker.elastic.co/elasticsearch/elasticsearch:7.10.0
sudo docker ps -a
list docker images
sudo docker images ls
sudo start estic-container-id
curl --location --request PUT 'http://localhost:9200/messages/_doc/1' \
--header 'Content-Type: application/json' \
--data '{
"message": "Pawan is blue today"
}'
curl --location 'http://localhost:9200/_all/_search' \
--header 'Content-Type: application/json' \
--data '{
"query":{
"query_string":{
"query":"pawan"
}
}
}'
https://www.elastic.co/guide/en/elasticsearch/client/java-api-client/current/connecting.html
Documentation for elastic search
Elastic search code implementation
import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.json.jackson.JacksonJsonpMapper;
import co.elastic.clients.transport.ElasticsearchTransport;
import co.elastic.clients.transport.rest_client.RestClientTransport;
import lombok.Builder;
import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Builder
@Configuration
public class ElasticSearchConfig {
@Bean
public RestClient getRestClient() {
Header[] defaultHeaders = new Header[]{new BasicHeader("Content-type", "application/json"),new BasicHeader("X-Elastic-Product", "Elasticsearch")};
RestClient restClient = RestClient.builder(
new HttpHost("localhost", 9200)).setDefaultHeaders(defaultHeaders).build();
return restClient;
}
@Bean
public ElasticsearchTransport getElasticsearchTransport() {
return new RestClientTransport(
getRestClient(), new JacksonJsonpMapper());
}
@Bean
public ElasticsearchClient getElasticsearchClient() {
ElasticsearchClient client = new ElasticsearchClient(getElasticsearchTransport());
return client;
}
}
mport co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.elasticsearch.core.*;
import co.elastic.clients.elasticsearch.core.search.Hit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
@Repository
public class ElasticSearchQuery {
@Autowired
private ElasticsearchClient elasticsearchClient;
private final String indexName = "messages";
public String createOrUpdateDocument(Product product) throws IOException {
IndexResponse response = elasticsearchClient.index(i -> i
.index(indexName)
.id(product.getId())
.document(product)
);
if (response.result().name().equals("Created")) {
return new StringBuilder("Document has been successfully created.").toString();
} else if (response.result().name().equals("Updated")) {
return new StringBuilder("Document has been successfully updated.").toString();
}
return new StringBuilder("Error while performing the operation.").toString();
}
public Product getDocumentById(String productId) throws IOException {
Product product = null;
GetResponse<Product> response = elasticsearchClient.get(g -> g
.index(indexName)
.id(productId),
Product.class
);
if (response.found()) {
product = response.source();
System.out.println("Product name " + product.getName());
} else {
System.out.println("Product not found");
}
return product;
}
public String deleteDocumentById(String productId) throws IOException {
DeleteRequest request = DeleteRequest.of(d -> d.index(indexName).id(productId));
DeleteResponse deleteResponse = elasticsearchClient.delete(request);
if (Objects.nonNull(deleteResponse.result()) && !deleteResponse.result().name().equals("NotFound")) {
return new StringBuilder("Product with id " + deleteResponse.id() + " has been deleted.").toString();
}
System.out.println("Product not found");
return new StringBuilder("Product with id " + deleteResponse.id() + " does not exist.").toString();
}
public List<Product> searchAllDocuments() throws IOException {
SearchRequest searchRequest = SearchRequest.of(s -> s.index(indexName));
SearchResponse searchResponse = elasticsearchClient.search(searchRequest, Product.class);
List<Hit> hits = searchResponse.hits().hits();
List<Product> products = new ArrayList<>();
for (Hit object : hits) {
System.out.print(((Product) object.source()));
products.add((Product) object.source());
}
return products;
}
}
Dependency required
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-client</artifactId>
<version>8.6.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.data/spring-data-elasticsearch -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-elasticsearch</artifactId>
</dependency>
<dependency>
<groupId>co.elastic.clients</groupId>
<artifactId>elasticsearch-java</artifactId>
<version>8.6.2</version>
</dependency>
Header[] defaultHeaders = new Header[]{new BasicHeader("Content-type", "application/json"),new BasicHeader("X-Elastic-Product", "Elasticsearch")};
RestClient restClient = RestClient.builder(
new HttpHost("localhost", 9200)).setDefaultHeaders(defaultHeaders).build();
return restClient;
No comments:
Post a Comment