Maven
Şu satırı dahil ederiz
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-elasticsearch</artifactId></dependency>
Kavramlar
Index : ilişkisel veri tabanındaki DB anlamına gelir
Type : ilişkisel veri tabanındaki Table anlamına gelir
Document : ilişkisel veri tabanındaki Row anlamına gelir
Field : ilişkisel veri tabanındaki Column anlamına gelir
Açıklaması şöyle
There are two ways to do operations on elastic search using spring boot.
1. By using ElasticsearchRestTemplate: This we should use when we want to create more complex queries.
2. By Using Repositories: This is very simple and it has all the methods defined and internally it creates elastic-based queries.
application.properties
Açıklaması şöyle
By default, application tries to connect with Elasticsearch on localhost. If we use another target URL we need to override it in configuration settings.
ElasticsearchOperations ile işlem yapılabilir
ElasticsearchRepository kullanılabilir
Örnek
Şöyle yaparız
spring: data: elasticsearch: cluster-name: docker-cluster # Comma-separated cluster node addresses. If not specified, starts a client node. cluster-nodes: localhost:9200 # Enable Elasticsearch repositories. repositories: enabled: true rest: uris: http://localhost:9200
ElasticsearchConfiguration Sınıfı
Örnek
Şöyle yaparız
@Configuration public class ElasticsearchClientConfig extends ElasticsearchConfiguration { @Value("${spring.elasticsearch.rest.uris}") String connetionUrl; @Override public ClientConfiguration clientConfiguration() { return ClientConfiguration.builder() .connectedTo(connetionUrl) .build(); } }
AbstractElasticsearchConfiguration Sınıfı
Eğer ElasticsearchRestTemplate kullanacaksak şu açıklamayı bilmek lazım. Açıklaması şöyle
ElasticSearchRestTemplate is built on the top of RestHighLevelClient. You can think of it as a Spring wrapper over RestHighLevelClient.
Örnek
Şöyle yaparız
import org.elasticsearch.client.RestHighLevelClient; import org.springframework.data.elasticsearch.client.ClientConfiguration; import org.springframework.data.elasticsearch.client.RestClients; import org.springframework.data.elasticsearch.config.AbstractElasticsearchConfiguration; import org.springframework.data.elasticsearch.repository.config. EnableElasticsearchRepositories; @Configuration @EnableElasticsearchRepositories(basePackages = "com.search.elasticsearchapp.repo") @ComponentScan(basePackages = { "com.search.elasticsearchapp" }) public class ElasticsearchClientConfig extends AbstractElasticsearchConfiguration { @Override @Bean public RestHighLevelClient elasticsearchClient() { ClientConfiguration clientConfiguration = ClientConfiguration .builder() .connectedTo("localhost:9200") .build(); return RestClients.create(clientConfiguration).rest(); } }
Daha sonra bir nesne yaratırız
import org.springframework.data.annotation.Id; import org.springframework.data.elasticsearch.annotations.Document; import org.springframework.data.elasticsearch.annotations.Field; import org.springframework.data.elasticsearch.annotations.FieldType; @Document(indexName = "product") public class Product { @Id private String id; @Field(type = FieldType.Text, name = "name") private String name; @Field(type = FieldType.Double, name = "price") private Double price; @Field(type = FieldType.Text, name = "creator") private String creator; }
Daha sonra repository için şöyle yaparız. Repository sınıfı save() vs gibi metodlar sağlıyor ancak kendi metodlarımızı da yazmak gerekebilir.
import com.search.elasticsearchapp.model.Product; import org.springframework.data.elasticsearch.repository.ElasticsearchRepository; import org.springframework.stereotype.Repository; @Repository public interface ProductRepo extends ElasticsearchRepository<Product, Long> { List<Product> findByName(String name); List<Product> findByNameContaining(String name); List<Product> findByCreatorAndName(String creator, String name); }
RestHighLevelClient Sınıfı
RestHighLevelClient Sınıfı yazısına taşıdım
Hiç yorum yok:
Yorum Gönder