博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
springboot -- 检索 Elasticsearch 初了解
阅读量:3920 次
发布时间:2019-05-23

本文共 5869 字,大约阅读时间需要 19 分钟。

前言

数据库一般用于储存数据与事务能力, 但一旦数据量大的时候, 查询数据的效率大大不如利用搜索引擎去拿数据

有两种方法来测试 elasticsearch , 但前提都要启动 elasticsearch 服务, 我是用虚拟机启动的。

一、Ubuntu 虚拟机 docker 启动 elasticsearch:

1)、输入命令拉取 elasticsearch 2.4.6 版本的镜像

sudo docker pull elasticsearch : 2.4.6

2)、开启 elasticsearch

sudo docker run -e ES_JAVA_OPTS="-Xms256m -Xmx256m" -d -p 9200:9200 -p 9300:9300 --name ES_2.4.6 0b44dca495a2

*其中9200:9200 第一个9200端口应该是面向客户端的(用网页或测试工具postman的时候, 端口要用9200)

*其中9300:9300 第一个9300端口应该是面向服务端的(SpringBoot配置elaticsearch 时用9300端口去配置)

0b44dca495a2 是镜像ID

可用 sudo docker images 查询

3)、打开 postman 测试是否配置成功:

在这里插入图片描述

用 jest 来作为检索工具

1、引入依赖:

io.searchbox
jest
5.3.3

2、配置文件:

spring.elasticsearch.jest.uris=http://192.168.93.137:9200

配置好开启 jest 服务主机的IP地址, 注意端口是9200!

3 、测试:

package com.example.demo;import java.io.IOException;import javax.management.Query;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 com.example.ElasticsearchApplication;import com.example.bean.Article;import io.searchbox.client.JestClient;import io.searchbox.client.JestResult;import io.searchbox.core.Index;import io.searchbox.core.Search;import io.searchbox.core.SearchResult;@RunWith(SpringRunner.class)@SpringBootTest(classes = ElasticsearchApplication.class)public class ElasticsearchApplicationTests {
@Autowired JestClient jestClient; @Autowired Article article; /* * 创建检索内容 */ @Test public void contextLoads() throws IOException {
/* * huang 是索引, 相当于数据库 * yu 是类型, 相当于表 * 999 是ID, 相当于主键 * 每条内容相当于一条数据 */ //创建检索 Index index = new Index.Builder(article).index("huang").type("yu").id("999").build(); //执行后返回添加的信息 JestResult r = jestClient.execute(index); System.out.println(r.getJsonString()); } /* * 创建搜索条件语句 */ @Test public void contextLoads02() throws IOException {
//搜索的条件语句 String query = "{\n" + " \"query\" : {\n" + " \"match\" : {\n" + " \"author\" : \"听大野\"\n" + " }\n" + " }\n" + "}"; Search search = new Search.Builder(query).addIndex("huang").addType("yu").build(); //执行并返回 SearchResult result = jestClient.execute(search); System.out.println(result.getJsonString()); } }

huang 是索引, 相当于数据库

yu 是类型, 相当于表
999 是ID, 相当于主键
每条内容相当于一条数据

创建检索内容后可以查询到:

在这里插入图片描述

根据搜索条件语句可以得到:

在这里插入图片描述

//条件语句		String query = "{\n" + 			"		    \"query\" : {\n" + 			"		        \"match\" : {\n" + 			"		            \"author\" : \"听大野\"\n" +			"		        }\n" + 			"		    }\n" + 			"}";

这就是搜索时的条件

用 spring-boot-starter-data-elasticsearch 来检索

这里注意一下, 我springboot版本是1.5.9, 对应引入的 spring-boot-starter-data-elasticsearch是1.5.9, 引入的 spring-data-elaticsearch 是2.1.9, 所以对应的elaticsearch 版本应该是2.1.9左右的, 我这里用的是2.4.6版本。如果版本不适配的话, 项目启动时会出现拒绝连接和连接超时的异常。

在这里插入图片描述

在这里插入图片描述

1、引入依赖:

org.springframework.boot
spring-boot-starter-data-elasticsearch

2、配置文件:

spring.data.elasticsearch.cluster-name=elasticsearchspring.data.elasticsearch.cluster-nodes=192.168.93.137:9300

注意端口是9300!不是9200!

3、上传的索引实体内容用 @Document 标识

1)、indexName 是 索引

2)、type 是 类型

package com.example.bean;import org.springframework.boot.context.properties.ConfigurationProperties;//import org.springframework.data.elasticsearch.annotations.Document;//import org.springframework.data.elasticsearch.annotations.Document;import org.springframework.stereotype.Component;//import io.searchbox.annotations.JestId;@Component//@ConfigurationProperties(prefix="huang")@Document(indexName="huang", type="articl")public class Article {
private int id; private String author; private String title; private String content; public int getId() {
return id; } public void setId(int id) {
this.id = id; } public String getAuthor() {
return author; } public void setAuthor(String author) {
this.author = author; } 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; } @Override public String toString() {
return "Article [id=" + id + ", author=" + author + ", title=" + title + ", content=" + content + "]"; } }

4、创建一个类似JPA的操作接口:

package com.example.respersity;import java.util.List;import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;import com.example.bean.Article;public interface authorRespersity extends ElasticsearchRepository
{
public List
findByAuthorLike(String article); }

其中的查询方法命名与查询JPA命名方式雷同,

在这里插入图片描述

在这里插入图片描述

同样的也可以用@Query注解

在这里插入图片描述

5、测试:

package com.example.demo;import java.io.IOException;import java.util.List;import javax.management.Query;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 com.example.ElasticsearchApplication;import com.example.bean.Article;import com.example.respersity.authorRespersity;import io.searchbox.client.JestClient;import io.searchbox.client.JestResult;import io.searchbox.core.Index;import io.searchbox.core.Search;import io.searchbox.core.SearchResult;@RunWith(SpringRunner.class)@SpringBootTest(classes = ElasticsearchApplication.class)public class ElasticsearchApplicationTests {
@Autowired authorRespersity ArticleRespersity; @Test public void test03(){
Article articl = new Article(); articl.setAuthor("zzzzzzz大神"); articl.setContent("牛逼"); articl.setId(3333); articl.setTitle("嗯嗯..."); ArticleRespersity.index(articl); //查询索引 List
list = ArticleRespersity.findByAuthorLike("zzz"); for(Article a : list) System.out.println(a); } }

转载地址:http://fyern.baihongyu.com/

你可能感兴趣的文章
剑指 Offer 32 - I. 从上到下打印二叉树
查看>>
剑指 Offer 32 - III. 从上到下打印二叉树 III
查看>>
剑指 Offer 33. 二叉搜索树的后序遍历序列
查看>>
模式18.桥接模式-java
查看>>
剑指 Offer 34. 二叉树中和为某一值的路径
查看>>
剑指 Offer 35. 复杂链表的复制
查看>>
剑指 Offer 36. 二叉搜索树与双向链表
查看>>
剑指 Offer 38. 字符串的排列
查看>>
剑指 Offer 40. 最小的k个数
查看>>
剑指 Offer 41. 数据流中的中位数
查看>>
模式19.命令模式-Java
查看>>
模式20.职责链模式-Java
查看>>
剑指 Offer 45. 把数组排成最小的数
查看>>
模式18.桥接模式-Java
查看>>
模式21.中介者模式-Java
查看>>
剑指 Offer 46. 把数字翻译成字符串
查看>>
剑指 Offer 48. 最长不含重复字符的子字符串
查看>>
剑指 Offer 49. 丑数
查看>>
剑指 Offer 50. 第一个只出现一次的字符
查看>>
模式23.解释器模式-Java
查看>>