直接进入正题
一.pom引入依赖
org.springframework.boot
spring-boot-starter-data-elasticsearch
net.java.dev.jna
jna
4.5.1
这里需要引入两个依赖,我这边只引入第一个的时候,会报错,不知道为什么,就网上找答案,引入了第二个
二.application.yaml配置es
spring
data:
elasticsearch:
cluster-name: docker-cluster
cluster-nodes: xxx:9200
我这边使用的docker安装运行的es
三,直接上代码
@Data @Document(indexName = "houseindex", type = "house") public class HouseIndexTemplate implements Serializable {
private static final long serialVersionUID = -3433706274548353650L;
@Id
private Long houseId;
// 使用分词器
@Field(type = FieldType.Text, analyzer = "ik_max_word", searchAnalyzer = "ik_max_word")
private String title;
@Field(type = FieldType.Keyword)
private String name;
@Field(type = FieldType.Integer)
private int price;
}
注意:
这个bean不要加//@Repository 注解,网上有很多说要加注解的,
实现ElasticsearchRepository 接口,会自动注入bean
public interface HouseRepository extends ElasticsearchRepository<HouseIndexTemplate, Long> {
}```
@Api("elasticsearch")
@RestController("/es")
public class HourseController {
@Autowired
private HouseRepository houseRepository;
@ApiOperation("save")
@GetMapping("/save")
public void save(@RequestBody HouseIndexTemplate template ,HttpServletRequest request, HttpServletResponse response) throws Exception {
houseRepository.save(template);
System.out.println("<<<<<<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>>>");
}
}
配置这些后,进行测试,结果出现了
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userDocumentRespository': Cannot resolve reference to bean 'elasticsearchTemplate' while setting bean property 'elasticsearchOperations'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'elasticsearchTemplate' defined in class path resource [org/springframework/boot/autoconfigure/data/elasticsearch/ElasticsearchDataAutoConfiguration.class]: Unsatisfied dependency expressed through method 'elasticsearchTemplate' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'elasticsearchClient' defined in class path resource [org/springframework/boot/autoconfigure/data/elasticsearch/ElasticsearchAutoConfiguration.class]: Bean instantiation via factory method failed; nested exception is 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 [4], rejecting [4]
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'elasticsearchTemplate' defined in class path resource [org/springframework/boot/autoconfigure/data/elasticsearch/ElasticsearchDataAutoConfiguration.class]: Unsatisfied dependency expressed through method 'elasticsearchTemplate' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'elasticsearchClient' defined in class path resource [org/springframework/boot/autoconfigure/data/elasticsearch/ElasticsearchAutoConfiguration.class]: Bean instantiation via factory method failed; nested exception is 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 [4], rejecting [4]
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'elasticsearchClient' defined in class path resource [org/springframework/boot/autoconfigure/data/elasticsearch/ElasticsearchAutoConfiguration.class]: Bean instantiation via factory method failed; nested exception is 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 [4], rejecting [4]
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 [4], rejecting [4]
Caused by: java.lang.IllegalStateException: availableProcessors is already set to [4], rejecting [4]``
问题出现的原因,github给出了答案,因为项目中使用了redis,需要加环境变量,配置bean加载
@Configuration
public class ElasticsearchConfig {
@PostConstruct
void init() {
System.setProperty("es.set.netty.runtime.available.processors", "false");
}
}
第二种解决方法
在启动类中设置环境变量
public static void main(String[] args) {
System.setProperty("es.set.netty.runtime.available.processors", "false");
SpringApplication.run(SpringbootApplication.class, args);
}
配置文件中使用9200的端口,会出现无节点的问题,项目中应该使用9300tcp的端口