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了:

访问:http://192.168.1.106:9200/

配置文件: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 {@Idprivate 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;}@Overridepublic 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 {@AutowiredCustomerRepository repository;//测试插入@Testpublic 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));}//测试查询@Testpublic 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

springboot细节挖掘(集成ElasticSearch)相关推荐

  1. springboot细节挖掘(配置Swagger2)

    首先启动一个springboot的项目: 配置pow.xml,在maven里面添加依赖 <!--springboot之swaager的配置 start--><dependency&g ...

  2. 2021最新! Springboot 2.X集成ElasticSearch 7.6.2(入门版)

    目录 1.版本选择 2.ElasticSearch 安装 2.1 新增配置 2.2 设置登录密码 2.3 ElasticSearch作为windows服务启动 3 ElasticSearch-head ...

  3. springboot细节挖掘(对测试的支持)

    Spring Boot 提供了专门支持测试的组件 Spring Boot Test,其集成了业内流行的 7 种强大的测试框架: JUnit,一个 Java 语言的单元测试框架: Spring Test ...

  4. springboot细节挖掘(日志系统)

    注意: 1.Spring Boot 1.3.x和以下版本支持log4j的日志输出 2.Spring Boot 1.3.x以上版本只支持log4j2,logback的日志输出 Slf4j+logback ...

  5. springboot细节挖掘(jar和war打包)

    打包运行 springboot下面分别构建俩种包的构建演示 war包配置: <packaging>war</packaging> <build> <final ...

  6. springboot细节挖掘(监听器)

    什么是 Web 监听器?Web 监听器是一种 Servlet 特殊类,它们能帮助开发者监听 Web 中特定的事件,比如 ServletContext.HttpSession.ServletReques ...

  7. springboot细节挖掘(知识积累)

    转载地址:https://blog.csdn.net/HQZ820844012/article/details/80400058#spring-顶级框架

  8. Springboot细节挖掘(对web的支持之数据校验)

    数据校验: 输入验证是最重要的 Web 开发任务之一,在 Spring MVC 中有两种方式可以验证输入:一种是 Spring 自带的验证框架,另外一种是利用 JSR 实现. JSR 是一个规范文档, ...

  9. springboot细节挖掘(数据初始化)

    如何加载一些启动就需要的初始化数据呢? CommandLineRunner spring Boot 为我们提供了一个方法,通过实现接口 CommandLineRunner 来实现 定义初始化类 MyC ...

最新文章

  1. 「AI 质检员」在富士通工厂上岗,检测效率比人工提升 25%
  2. 2017年全球AI芯片公司大盘点
  3. 虚拟化通过更少的硬件以实现更多性能的提升
  4. Linux平台Qt creator报错:Circular all - first dependency dropped
  5. 多选月份的日期选择器_GitHub - ylmyg/SelectionTime: Android下日期选择器,支持范围选择、多选、单选、根据输入天数选择日期...
  6. 怎样避免每次都解释大量指令?
  7. Python并发编程之concurrent.futures
  8. iphone解锁_有人可以用解锁的iPhone做的最糟糕的事情是什么?
  9. 2018焦作网络赛 - Poor God Water 一道水题的教训
  10. 【译】《Pro ASP.NET MVC4 4th Edition》第三章(二)
  11. JavaWeb——springMVC、mybatis与spring的整合
  12. /proc/cpuinfo文件分析(查看CPU信息)
  13. Junipre认证必了解产品:juniper Networks SSG550M
  14. 十四 关于interrupt, interrupted, isInterrupted
  15. 免费进销存真的好用吗?一文告诉你真相
  16. PuttyPsftp
  17. 在reader中勾选pdf复选框_adobe reader pro dc
  18. canva怎么拼接图片_canvas图片拼接(横向)
  19. Web表单设计:表单结构
  20. cls love even fib!(打表,找规律)

热门文章

  1. thinkphp-session与cookie
  2. ListView setOnItemClickListener无法响应点击事件解决
  3. sqlserver2008安装报错 “Previous releases of Microsoft Visual Studio 2008″ failed.
  4. 编写批处理文件编译.Net工程
  5. 网站项目管理规范手册
  6. php服务器错误日志在哪里看,PHP取服务器错误日志
  7. 苹果hdmi没有声音怎么办_HDMI线连接到电视后没有信号怎么办?给你7个方法,常见但很实用...
  8. matlab控制算法C语言,PID算法Matlab仿真程序和C程序
  9. 计算机加经济学加自动化,MIT经济学家戳破机器人真相:除了能取代你,价值微乎其微...
  10. 个人支付源码_[5G时代投资风口源码修复版] 投资区块链+订制UI完美版+对接免签支付+自带发圈推广任务奖励+视频教程...