美文网首页springboot
Windwos中SpringBoot整合ElasticSearc

Windwos中SpringBoot整合ElasticSearc

作者: 刻骨铭心_17d7 | 来源:发表于2019-10-28 15:40 被阅读0次

简介

 当需要再我们的应用中使用检索功能时需要引入现有的组件,ElasticSearch是目前使用全文搜索引擎的首选,它底层使用了Lucene,这个一个用java开发的工具包。
 在SpringBoot中使用ElasticSearch有两种方式,一种是使用Jest来整合,一种是使用SpringBoot的SpringData中已整合的SpringData-ElasticSearch。下面分别介绍这两种方式的使用。

1.安装ElasticSearch

进入官网https://www.elastic.co/cn/downloads/elasticsearch进行下载window版本,要下载之前版本点击past relases

图1
下载后解压zip文件进入bin运行elasticsearch.bat文件
图2
图3
该应用默认使用9200端口,访问该端口地址,出现如下界面说明安装成功 图4

2.使用ElasticSearch

查看中文版官方文档https://www.elastic.co/guide/cn/elasticsearch/guide/current/index.html简单使用示例

图5
用postman模拟上述请求
图6

结果


图7

使用get请求查询即可得到刚才添加的结果


图8
也可以使用表达式进行匹配查询
图9

查询结果如下

{
    "took": 2,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 1,
        "max_score": 0.2876821,
        "hits": [
            {
                "_index": "megacorp",
                "_type": "employee",
                "_id": "1",
                "_score": 0.2876821,
                "_source": {
                    "first_name": "John",
                    "last_name": "Smith",
                    "age": 25,
                    "about": "I love to go rock climbing",
                    "interests": [
                        "sports",
                        "music"
                    ]
                }
            }
        ]
    }
}

score表示匹配程度,值越大匹配度越高

2.Jest与springboot整合

1.建立springboot项目,在https://mvnrepository.com/artifact/io.searchbox/jest

中搜索包导入


图10
图11

2.建立实体类Article

@Data
public class Article {

    @JestId
    private Long id;
    private String Title;
    private String author;
    private String content;

}

3.使用junit测试

    @Test
    void contextLoads() {
        Article article = new Article();
        article.setId(1L);
        article.setTitle("海的女儿");
        article.setAuthor("海明威");
        article.setContent("这是一本关于海的书");

        Index index = new Index.Builder(article).index("shop").type("books").build();
        try {
            jestClient.execute(index);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

4.运行访问http://localhost:9200/shop/books/1得到结果如下

图12

5.在代码中使用检索功能:

@Test
    public void Search() {
        String json = "{\n" +
                "    \"query\" : {\n" +
                "        \"match\" : {\n" +
                "            \"content\" : \"一本 书\"\n" +
                "        }\n" +
                "    }\n" +
                "}";
        Search search = new Search.Builder(json).addIndex("shop").addType("books").build();

        try {
            SearchResult searchResult = jestClient.execute(search);
            System.out.println(searchResult.getJsonString());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

这里json字符串即是postman中的请求题内容

结果

{"took":1,"timed_out":false,"_shards":{"total":5,"successful":5,"skipped":0,"failed":0},"hits":{"total":1,"max_score":0.8630463,"hits":[{"_index":"shop","_type":"books","_id":"1","_score":0.8630463,"_source":{"id":1,"Title":"海的女儿","author":"海明威","content":"这是一本关于海的书"}}]}}

3.springData使用ElasticSearch

导入依赖包

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
        </dependency>

该方式受到springboot的版本限制,需要使用对应的ElasticSearch版本号这里使用。
查看该版本对应的ElasticSearch

1.先查看依赖版本

图13

springboot2.2.0对应springData3.2.0


图14
图15

进入springdata官网,查看相应版本文档


图16
图17

点击版本控制


图18
进入github查看依赖文档,得到对应版本号
图19
图20

所以我们这里需要下载6.8.1版本,版本不一致会出现连接错误等问题

2.构建Book实体

@Data
@Document(indexName = "shop",type = "books")
public class Book {
    private Long id;
    private String content;
    private String author;
}

构建BookRepository接口继承ElasticsearchRepository<T, ID>,这里的用法类似JPA,自建了模糊查询的接口

public interface BookRepository extends ElasticsearchRepository<Book,Long> {
    List<Book> findByContentLike(String content);
}

3.注入到使用类中

    @Autowired
    private BookRepository bookRepository;

    @Test
    public void test01() {
        Book book = new Book();
        book.setId(2L);
        book.setAuthor("吴承恩");
        book.setContent("西游记");
        bookRepository.index(book);

        List<Book> books =  bookRepository.findByContentLike("西");
        for (Book book : books) {
            System.out.println(book);
        }
    }

4.运行结果:

图21

相关文章

网友评论

    本文标题:Windwos中SpringBoot整合ElasticSearc

    本文链接:https://www.haomeiwen.com/subject/mrkbvctx.html