【SpringBoot整合Elasticsearch】SpringBoot整合ElasticSearch

时间:2021-07-29 00:43:59

一、Linux下安装ElasticSearch

1、检测是否安装了Elasticsearch

 ps aux |grep elasticsearch

2、安装JDK

3、下载Elasticsearch

 wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.0.0.tar.gz

解压Elasticsearch

tar -zxvf elasticsearch-6.0..tar.gz

移动Elasticsearch到/usr/local/elasticsearch

 mv elasticsearch-6.0. /usr/local/elasticsearch

4、修改配置文件

进入/usr/local/elasticsearch/config目录,使用vi编辑器

 vi elasticsearch.yml
 network.host: 192.168.181.201
discovery.zen.ping.unicast.hosts: ["192.168.181.201"]

修改/etc/sysctl.conf否则会出现

max virutal memory areas vm.max_map_count [65530] is too low, increase to at least [262144]

vm.max_map_count=

退出保存后执行如下命令:

 sysctl -p

使用vi编辑器,修改/etc/security/limits.conf文件,在文件末尾添加如下代码,否则会出现

max number of threads [3750] for user [xxx] is too low, increase to at least [4096]

 # sanglp为登录服务器的用户名

 sanglp soft nofile
sanglp hard nofile
sanglp soft nproc
sanglp hard nproc

切记退出重新登录。

5、启动Elasticsearch

进入/usr/local/elasticsearch目录之中,输入以下命令,开始Elasticsearch服务:

 ./bin/elasticsearch

测试:【SpringBoot整合Elasticsearch】SpringBoot整合ElasticSearch

二、新建spring boot的elasticsearch项目

在start.spring.io中新建springboot项目,并导入到intellij中

pom.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.</modelVersion> <groupId>com.slp</groupId>
<artifactId>springboot-elasticsearch</artifactId>
<version>0.0.-SNAPSHOT</version>
<packaging>jar</packaging> <name>springboot-elasticsearch</name>
<description>Demo project for Spring Boot</description> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1..BUILD-SNAPSHOT</version>
<relativePath/> <!-- lookup parent from repository -->
</parent> <properties>
<project.build.sourceEncoding>UTF-</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties> <dependencies>
<!--添加web的应用依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--添加spring-data-elasticsearch的依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency> <dependency>
<groupId>net.java.dev.jna</groupId>
<artifactId>jna</artifactId>
<version>3.0.</version>
</dependency>
</dependencies> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> <repositories>
<repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories> <pluginRepositories>
<pluginRepository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
<pluginRepository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories> </project>

application.yml

 spring:
data:
elasticsearch:
cluster-name: elasticsearch #默认为elasticsearch
cluster-nodes: 192.168.181.201: #配置es节点信息,逗号分隔,如果没有指定,则启动ClientNode
properties:
path:
logs: ./elasticsearch/log #elasticsearch日志存储目录
data: ./elasticsearch/data #elasticsearch数据存储目录

Article.java

 package com.slp.springbootelasticsearch.pojo;

 import org.springframework.data.elasticsearch.annotations.Document;

 import java.io.Serializable;

 /**
* @author sanglp
* @create 2018-07-04 9:06
* @desc 文章实体类,默认情况下添加@Document注解会对实体中的所有属性建立索引,
**/
@Document(indexName = "projectname",type = "article",indexStoreType = "fs",shards = ,replicas = ,refreshInterval = "-1")
public class Article implements Serializable { private Long id;
/**
* 标题
*/
private String title;
/**
* 摘要
*/
private String abstracts;
/**
* 内容
*/
private String content;
/**
* 发表时间
*/
private String postTime;
/**
* 点击率
*/
private String clickCount;
/**
* 作者
*/
private Author author;
/**
* 所属教程
*/
private Tutorial tutorial; public Long getId() {
return id;
} public void setId(Long id) {
this.id = id;
} public String getTitle() {
return title;
} public void setTitle(String title) {
this.title = title;
} public String getContent() {
return content;
} public void setContent(String content) {
this.content = content;
} public String getPostTime() {
return postTime;
} public void setPostTime(String postTime) {
this.postTime = postTime;
} public String getClickCount() {
return clickCount;
} public void setClickCount(String clickCount) {
this.clickCount = clickCount;
} public Author getAuthor() {
return author;
} public void setAuthor(Author author) {
this.author = author;
} public Tutorial getTutorial() {
return tutorial;
} public void setTutorial(Tutorial tutorial) {
this.tutorial = tutorial;
} public String getAbstracts() {
return abstracts;
} public void setAbstracts(String abstracts) {
this.abstracts = abstracts;
}
}

Author.java

 package com.slp.springbootelasticsearch.pojo;

 import java.io.Serializable;

 /**
* @author sanglp
* @create 2018-07-04 9:04
* @desc 作者实体类
**/
public class Author implements Serializable{
/**
* 作者ID
*/
private Long id;
/**
* 作者姓名
*/
private String name;
/**
* 作者简介
*/
private String remark; public Long getId() {
return id;
} public void setId(Long id) {
this.id = id;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getRemark() {
return remark;
} public void setRemark(String remark) {
this.remark = remark;
}
}

Tutorial.java

 package com.slp.springbootelasticsearch.pojo;

 import java.io.Serializable;

 /**
* @author sanglp
* @create 2018-07-04 9:03
* @desc 实体类
**/
public class Tutorial implements Serializable {
private Long id;
//教程名称
private String name ; public Long getId() {
return id;
} public void setId(Long id) {
this.id = id;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
}
}

@Document注解里面的几个属性,类比mysql的话是这样: 
index –> DB 
type –> Table 
Document –> row 
@Id注解加上后,在Elasticsearch里相应于该列就是主键了,在查询时就可以直接用主键查询,后面再看。其实和mysql非常类似,基本就是一个数据库。

 @Persistent
@Inherited
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE})
public @interface Document { String indexName();//索引库的名称,个人建议以项目的名称命名 String type() default "";//类型,个人建议以实体的名称命名 short shards() default ;//默认分区数 short replicas() default ;//每个分区默认的备份数 String refreshInterval() default "1s";//刷新间隔 String indexStoreType() default "fs";//索引文件存储类型
}

加上了@Document注解之后,默认情况下这个实体中所有的属性都会被建立索引、并且分词。 
我们通过@Field注解来进行详细的指定,如果没有特殊需求,那么只需要添加@Document即可。

@Field注解的定义如下:

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
@Documented
@Inherited
public @interface Field { FieldType type() default FieldType.Auto;#自动检测属性的类型 FieldIndex index() default FieldIndex.analyzed;#默认情况下分词 DateFormat format() default DateFormat.none; String pattern() default ""; boolean store() default false;#默认情况下不存储原文 String searchAnalyzer() default "";#指定字段搜索时使用的分词器 String indexAnalyzer() default "";#指定字段建立索引时指定的分词器 String[] ignoreFields() default {};#如果某个字段需要被忽略 boolean includeInParent() default false;
}

ArticleSearchRepository.java相当于dao

 package com.slp.springbootelasticsearch.repository;

 import com.slp.springbootelasticsearch.pojo.Article;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository; /**
* @author sanglp
* @create 2018-07-04 9:27
* @desc 文章reposiroty 泛型的参数分别是实体类型和主键类型
**/
public interface ArticleSearchRepository extends ElasticsearchRepository<Article,Long> { }

测试用例

package com.slp.springbootelasticsearch;

import com.slp.springbootelasticsearch.pojo.Article;
import com.slp.springbootelasticsearch.pojo.Author;
import com.slp.springbootelasticsearch.pojo.Tutorial;
import com.slp.springbootelasticsearch.repository.ArticleSearchRepository;
import org.elasticsearch.index.query.QueryStringQueryBuilder;
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; import java.util.Iterator; @RunWith(SpringRunner.class)
@SpringBootTest
public class SpringbootElasticsearchApplicationTests { @Test
public void contextLoads() {
}
@Autowired
private ArticleSearchRepository articleSearchRepository;
@Test
public void testSaveArticleIndex(){
Author author = new Author();
author.setId(1L);
author.setName("slp");
author.setRemark("test"); Tutorial tutorial = new Tutorial();
tutorial.setId(1L);
tutorial.setName("elastic search"); Article article = new Article();
article.setId(1L);
article.setTitle("spring boot integrate elasticsearch");
article.setAbstracts("elasticsearch is very easy");
article.setTutorial(tutorial);
article.setAuthor(author);
article.setContent("elasticsearch based on lucene");
article.setPostTime("");
article.setClickCount("");
articleSearchRepository.save(article); } @Test
public void testSearch(){
String queryString="spring";//搜索关键字
QueryStringQueryBuilder builder=new QueryStringQueryBuilder(queryString);
Iterable<Article> searchResult = articleSearchRepository.search(builder);
Iterator<Article> iterator = searchResult.iterator();
while(iterator.hasNext()){
System.out.println(iterator.next().getAbstracts());
}
}
}

问题:

1、Exception in thread "main" org.elasticsearch.client.transport.NoNodeAvailableException: None of the configured nodes are available:

先直接访问,并查看是否是配置有误,切记要关闭防火墙!!!