ElasticSearch下载
官网地址:https://www.elastic.co/cn/downloads/elasticsearch
DEMO参考地址:https://github.com/spring-projects/spring-data-elasticsearch
但是下载比较慢,无奈只有在csdn上下载一个版本使用。
启动ElasticSearch服务
运行测试,报告一个bug:
这个错误信息的意思就是说根据你的配置无法连接这个节点。
可能的原因有一下:
端口不对,注意是9300端口,9200端口是http的端口。这里使用的tcp连接。
ip地址有问题,这个也好排查,直接curl 你的地址:9200看是否返回成功的json
yml中cluster-name名字与config/elasticsearch.yml中的不一样。
以上三个问题都排查了还是无法解决问题的话,那么恭喜你。很有可能就是版本冲突的问题了。
网上说SpringBoot2.X的spring-boot-starter-data-elasticsearch仅支持es2.X的版本。 最好还是不用随便相信。自己去探索真理不就好了
我们先来看一下这个启动器里面有哪些东西
发现没有,里面就是用了spring-data-elasticsearch3.1.9
那么我们去github上面看看https://github.com/spring-projects/spring-data-elasticsearch
Versions
The following table shows the Elasticsearch versions that are used by Spring Data Elasticsearch:
Spring Data Elasticsearch | Elasticsearch |
---|---|
3.2.x |
6.7.2 |
3.1.x |
6.2.2 |
3.0.x |
5.5.0 |
2.1.x |
2.4.0 |
2.0.x |
2.2.0 |
1.3.x |
1.5.2 |
那么spring-data-elasticsearch要换成3.2.x
-
1 更换elasticsearch版本
-
2 更改SpringBoot的版本
-
3 改用spring-data-elasticsearch(推荐)
-
使用spring-data-elasticsearch
根据github的提示,添加依赖
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-elasticsearch</artifactId>
<version>3.0.11.RELEASE</version>
</dependency>
这里光添加这一个包还是报错,还需要添加两个包。
<!-- elasticsearch -->
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>5.6.12</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.plugin</groupId>
<artifactId>transport-netty4-client</artifactId>
<version>5.6.12</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-elasticsearch</artifactId>
<version>3.0.11.RELEASE</version>
</dependency>
配置文件的端口号修改为http9200,直接ok了:
配置文件:elasticsearch.yml
# ======================== Elasticsearch Configuration =========================
#
# NOTE: Elasticsearch comes with reasonable defaults for most settings.
# Before you set out to tweak and tune the configuration, make sure you
# understand what are you trying to accomplish and the consequences.
#
# The primary way of configuring a node is via this file. This template lists
# the most important settings you may want to configure for a production cluster.
#
# Please consult the documentation for further information on configuration options:
# https://www.elastic.co/guide/en/elasticsearch/reference/index.html
#
# ---------------------------------- Cluster -----------------------------------
#
# Use a descriptive name for your cluster:
#
cluster.name: es-mongodb
#
# ------------------------------------ Node ------------------------------------
#
# Use a descriptive name for the node:
#
node.name: node-1
#
# Add custom attributes to the node:
#
node.attr.rack: r1
#
# ----------------------------------- Paths ------------------------------------
#
# Path to directory where to store the data (separate multiple locations by comma):
#
path.data: /path/to/data
#
# Path to log files:
#
path.logs: /path/to/logs
#
# ----------------------------------- Memory -----------------------------------
#
# Lock the memory on startup:
#
bootstrap.memory_lock: true
#
# Make sure that the heap size is set to about half the memory available
# on the system and that the owner of the process is allowed to use this
# limit.
#
# Elasticsearch performs poorly when the system is swapping the memory.
#
# ---------------------------------- Network -----------------------------------
#
# Set the bind address to a specific IP (IPv4 or IPv6):
#
network.host: 192.168.1.106
#
# Set a custom port for HTTP:
#
http.port: 9200
#
# For more information, consult the network module documentation.
#
# --------------------------------- Discovery ----------------------------------
#
# Pass an initial list of hosts to perform discovery when new node is started:
# The default list of hosts is ["127.0.0.1", "[::1]"]
#
#discovery.zen.ping.unicast.hosts: ["host1", "host2"]
#
# Prevent the "split brain" by configuring the majority of nodes (total number of master-eligible nodes / 2 + 1):
#
#discovery.zen.minimum_master_nodes:
#
# For more information, consult the zen discovery module documentation.
#
# ---------------------------------- Gateway -----------------------------------
#
# Block initial recovery after a full cluster restart until N nodes are started:
#
gateway.recover_after_nodes: 3
#
# For more information, consult the gateway module documentation.
#
# ---------------------------------- Various -----------------------------------
#
# Require explicit names when deleting indices:
#
action.destructive_requires_name: true
pow.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.lucence</groupId>
<artifactId>lucence</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>lucence</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--start-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
<!--end-->
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
springboot的配置文件:application.properties
server.port=8080
# 集群名(默认值: elasticsearch,配置文件`cluster.name`: es-mongodb)
spring.data.elasticsearch.cluster-name=es-mongodb
# 集群节点地址列表,用逗号分隔
spring.data.elasticsearch.cluster-nodes=192.168.1.106:9300
#是否开启本地存储
spring.data.elasticsearch.repositories.enable=true
代码demo:
实体类代码:
package com.lucence.lucence.model;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
/**
* @authorseerhuitao 描述文件
* @create2019/6/29
*/
@Document(indexName = "customer", type = "customer", shards = 1, replicas = 0, refreshInterval = "-1")
public class Customer {
@Id
private String id;
private String userName;
private String address;
private int age;
public Customer(String userName,String address,int age){
this.userName=userName;
this.address=address;
this.age=age;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Customer{" +
"id='" + id + '\'' +
", userName='" + userName + '\'' +
", address='" + address + '\'' +
", age=" + age +
'}';
}
//必须要添加午餐的构造方法,否则或报错
public Customer(){
}
}
测试类代码:
package com.lucence.lucence;
import com.lucence.lucence.model.Customer;
import com.lucence.lucence.service.CustomerRepository;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class LucenceApplicationTests {
@Autowired
CustomerRepository repository;
//测试插入
@Test
public void saveCustomers() {
repository.save(new Customer("Alice", "北京",13));
repository.save(new Customer("Bob", "北京",23));
repository.save(new Customer("neo", "西安",30));
repository.save(new Customer("summer", "烟台",22));
}
//测试查询
@Test
public void fetchAllCustomers() {
System.out.println("Customers found with findAll():");
System.out.println("-------------------------------");
Iterable<Customer> iterable=repository.findAll();
for (Customer customer :iterable) {
System.out.println(customer);
}
}
}
运行:
从存储到查询基本实现。
ok!坑真多。
提示这个bug:org.springframework.data.elasticsearch.ElasticsearchException: failed to map source
必须要给实体类添加一个无参数的构造方法。
public Customer(){}
参考地址:https://blog.csdn.net/qq_36781505/article/details/89876179
参考地址:https://blog.csdn.net/linzhiqiang0316/article/details/80343401