SpringBoot整合ElasticsearchSpringBoot整合ElasticSearch

Posted 霓裳梦竹

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SpringBoot整合ElasticsearchSpringBoot整合ElasticSearch相关的知识,希望对你有一定的参考价值。

一、Linux下安装ElasticSearch

1、检测是否安装了Elasticsearch

1 ps aux |grep elasticsearch

2、安装JDK

3、下载Elasticsearch

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

解压Elasticsearch

tar -zxvf elasticsearch-6.0.0.tar.gz

移动Elasticsearch到/usr/local/elasticsearch

1 mv elasticsearch-6.0.0 /usr/local/elasticsearch

4、修改配置文件

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

1 vi elasticsearch.yml
1 network.host: 192.168.181.201
2 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=262144

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

1 sysctl -p

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

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

1 # sanglp为登录服务器的用户名
2 
3 sanglp soft nofile 65536
4 sanglp hard nofile 65536
5 sanglp soft nproc  4096
6 sanglp hard nproc  4096

切记退出重新登录。

5、启动Elasticsearch

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

1 ./bin/elasticsearch

测试:

二、新建spring boot的elasticsearch项目

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

pom.xml

  1 <?xml version="1.0" encoding="UTF-8"?>
  2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4     <modelVersion>4.0.0</modelVersion>
  5 
  6     <groupId>com.slp</groupId>
  7     <artifactId>springboot-elasticsearch</artifactId>
  8     <version>0.0.1-SNAPSHOT</version>
  9     <packaging>jar</packaging>
 10 
 11     <name>springboot-elasticsearch</name>
 12     <description>Demo project for Spring Boot</description>
 13 
 14     <parent>
 15         <groupId>org.springframework.boot</groupId>
 16         <artifactId>spring-boot-starter-parent</artifactId>
 17         <version>2.1.0.BUILD-SNAPSHOT</version>
 18         <relativePath/> <!-- lookup parent from repository -->
 19     </parent>
 20 
 21     <properties>
 22         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 23         <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
 24         <java.version>1.8</java.version>
 25     </properties>
 26 
 27     <dependencies>
 28         <!--添加web的应用依赖-->
 29         <dependency>
 30             <groupId>org.springframework.boot</groupId>
 31             <artifactId>spring-boot-starter-web</artifactId>
 32         </dependency>
 33         <!--添加spring-data-elasticsearch的依赖-->
 34         <dependency>
 35             <groupId>org.springframework.boot</groupId>
 36             <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
 37         </dependency>
 38 
 39         <dependency>
 40             <groupId>org.springframework.boot</groupId>
 41             <artifactId>spring-boot-starter-test</artifactId>
 42             <scope>test</scope>
 43         </dependency>
 44 
 45         <dependency>
 46             <groupId>net.java.dev.jna</groupId>
 47             <artifactId>jna</artifactId>
 48             <version>3.0.9</version>
 49         </dependency>
 50     </dependencies>
 51 
 52     <build>
 53         <plugins>
 54             <plugin>
 55                 <groupId>org.springframework.boot</groupId>
 56                 <artifactId>spring-boot-maven-plugin</artifactId>
 57             </plugin>
 58         </plugins>
 59     </build>
 60 
 61     <repositories>
 62         <repository>
 63             <id>spring-snapshots</id>
 64             <name>Spring Snapshots</name>
 65             <url>https://repo.spring.io/snapshot</url>
 66             <snapshots>
 67                 <enabled>true</enabled>
 68             </snapshots>
 69         </repository>
 70         <repository>
 71             <id>spring-milestones</id>
 72             <name>Spring Milestones</name>
 73             <url>https://repo.spring.io/milestone</url>
 74             <snapshots>
 75                 <enabled>false</enabled>
 76             </snapshots>
 77         </repository>
 78     </repositories>
 79 
 80     <pluginRepositories>
 81         <pluginRepository>
 82             <id>spring-snapshots</id>
 83             <name>Spring Snapshots</name>
 84             <url>https://repo.spring.io/snapshot</url>
 85             <snapshots>
 86                 <enabled>true</enabled>
 87             </snapshots>
 88         </pluginRepository>
 89         <pluginRepository>
 90             <id>spring-milestones</id>
 91             <name>Spring Milestones</name>
 92             <url>https://repo.spring.io/milestone</url>
 93             <snapshots>
 94                 <enabled>false</enabled>
 95             </snapshots>
 96         </pluginRepository>
 97     </pluginRepositories>
 98 
 99 
100 </project>

application.yml

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

Article.java

  1 package com.slp.springbootelasticsearch.pojo;
  2 
  3 import org.springframework.data.elasticsearch.annotations.Document;
  4 
  5 import java.io.Serializable;
  6 
  7 /**
  8  * @author sanglp
  9  * @create 2018-07-04 9:06
 10  * @desc 文章实体类,默认情况下添加@Document注解会对实体中的所有属性建立索引,
 11  **/
 12 @Document(indexName = "projectname",type = "article",indexStoreType = "fs",shards = 5,replicas = 1,refreshInterval = "-1")
 13 public class Article implements Serializable {
 14 
 15     private Long id;
 16     /**
 17      * 标题
 18      */
 19     private String title;
 20     /**
 21      * 摘要
 22      */
 23     private String abstracts;
 24     /**
 25      * 内容
 26      */
 27     private String content;
 28     /**
 29      * 发表时间
 30      */
 31     private String postTime;
 32     /**
 33      * 点击率
 34      */
 35     private String clickCount;
 36     /**
 37      * 作者
 38      */
 39     private Author author;
 40     /**
 41      * 所属教程
 42      */
 43     private Tutorial tutorial;
 44 
 45     public Long getId() {
 46         return id;
 47     }
 48 
 49     public void setId(Long id) {
 50         this.id = id;
 51     }
 52 
 53     public String getTitle() {
 54         return title;
 55     }
 56 
 57     public void setTitle(String title) {
 58         this.title = title;
 59     }
 60 
 61     public String getContent() {
 62         return content;
 63     }
 64 
 65     public void setContent(String content) {
 66         this.content = content;
 67     }
 68 
 69     public String getPostTime() {
 70         return postTime;
 71     }
 72 
 73     public void setPostTime(String postTime) {
 74         this.postTime = postTime;
 75     }
 76 
 77     public String getClickCount() {
 78         return clickCount;
 79     }
 80 
 81     public void setClickCount(String clickCount) {
 82         this.clickCount = clickCount;
 83     }
 84 
 85     public Author getAuthor() {
 86         return author;
 87     }
 88 
 89     public void setAuthor(Author author) {
 90         this.author = author;
 91     }
 92 
 93     public Tutorial getTutorial() {
 94         return tutorial;
 95     }
 96 
 97     public void setTutorial(Tutorial tutorial) {
 98         this.tutorial = tutorial;
 99     }
100 
101     public String getAbstracts() {
102         return abstracts;
103     }
104 
105     public void setAbstracts(String abstracts) {
106         this.abstracts = abstracts;
107     }
108 }

Author.java

 1 package com.slp.springbootelasticsearch.pojo;
 2 
 3 import java.io.Serializable;
 4 
 5 /**
 6  * @author sanglp
 7  * @create 2018-07-04 9:04
 8  * @desc 作者实体类
 9  **/
10 public class Author  implements Serializable{
11     /**
12      * 作者ID
13      */
14     private Long id;
15     /**
16      * 作者姓名
17      */
18     private String name;
19     /**
20      * 作者简介
21      */
22     private String remark;
23 
24     public Long getId() {
25         return id;
26     }
27 
28     public void setId(Long id) {
29         this.id = id;
30     }
31 
32     public String getName() {
33         return name;
34     }
35 
36     public void setName(String name) {
37         this.name = name;
38     }
39 
40     public String getRemark() {
41         return remark;
42     }
43 
44     public void setRemark(String remark) {
45         this.remark = remark;
46     }
47 }

Tutorial.java

 1 package com.slp.springbootelasticsearch.pojo;
 2 
 3 import java.io.Serializable;
 4 
 5 /**
 6  * @author sanglp
 7  * @create 2018-07-04 9:03
 8  * @desc 实体类
 9  **/
10 public class Tutorial implements Serializable {
11     private Long id;
12     //教程名称
13     private String name ;
14 
15     public Long getId() {
16         return id;
17     }
18 
19     public void setId(Long id) {
20         this.id = id;
21     }
22 
23     public String getName() {
24         return name;
25     }
26 
27     public void setName(String name) {
28         this.name = name;
29     }
30 }

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

 1 @Persistent
 2 @Inherited
 3 @Retention(RetentionPolicy.RUNTIME)
 4 @Target({ElementType.TYPE})
 5 public @interface Document {
 6 
 7 String indexName();//索引库的名称,个人建议以项目的名称命名
 8 
 9 String type() default "";//类型,个人建议以实体的名称命名
10 
11 short shards() default 5;//默认分区数
12 
13 short replicas() default 1;//每个分区默认的备份数
14 
15 String refreshInterval() default "1s";//刷新间隔
16 
17 String indexStoreType() default "fs";//索引文件存储类型
18 }

加上了@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

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

测试用例

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("20180704");
        article.setClickCount("1");
        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:

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

 


以上是关于SpringBoot整合ElasticsearchSpringBoot整合ElasticSearch的主要内容,如果未能解决你的问题,请参考以下文章

SpringBoot 整合 Elasticsearch 实现海量级数据搜索

Springboot 2.5.x整合ElasticSearch 7.1x

SpringBoot检索篇Ⅳ --- 整合ElasticSearch

SpringBoot整合ElasticSearch7.x及实战

[ ElasticSearch ] SpringBoot整合ElasticSearch

[ ElasticSearch ] SpringBoot整合ElasticSearch