引入elasticsearch包
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
application.yml中的elasticsearch配置
spring:
data:
elasticsearch:
cluster-nodes: 127.0.0.1:9300 #配置es節點信息,逗號分隔
cluster-name: elasticsearch #默認即爲 elasticsearch
properties:
transport:
tcp:
connect_timeout: 60s
path:
logs: ./elasticsearch/log #elasticsearch日誌存儲目錄
data: ./elasticsearch/data #elasticsearch數據存儲目錄
CURD操做方式:
public interface TestModelService2 extends ElasticsearchRepository<TestModel, String>
啓動過程出現錯誤:
Caused by: java.lang.IllegalArgumentException: Failed to create query for method public abstract java.lang.Object org.springframework.data.elasticsearch.repository.ElasticsearchRepository.index(java.lang.Object)! No property index found for type TestModel!
Caused by: org.springframework.data.mapping.PropertyReferenceException: No property index found for type TestModel!
集成前提是:系統中已有對數據庫的CURD操做,而後想加入elasticsearch搜索功能,操做的實體既是entity對象,也是document對象
錯誤的大概意思是說:org.springframework.data.elasticsearch.repository.ElasticsearchRepository 類中的index方法,系統按照JPA操做數據庫的方式,認爲index方法是經過index字段查找數據,然而,實體中沒有index字段,因此就出錯了
錯誤的緣由是:
@EntityScan("com.bc")和
@ComponentScan
(
"com.bc"
)註解掃描到了該類,致使系統覺得是數據庫操做類,而後按數據庫的實現類進行注入
解決辦法:
@EnableElasticsearchRepositories
(
basePackages
=
"com.search"
)區分不一樣包掃描,將crud操做類放到該包的子包下
其餘錯誤:
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.elasticsearch.client.transport.TransportClient]: Factory method 'elasticsearchClient' threw exception; nested exception is java.lang.IllegalStateException: availableProcessors is already set to [8], rejecting [8]
加入啓動參數,代碼中:
static{
System.setProperty("es.set.netty.runtime.available.processors", "false");
}
或是在jvm啓動參數:
-Des.set.netty.runtime.available.processors=false