1检查elasticsearch版本注入对应的jest依赖
使用对应版本依赖:
<dependency>
<groupId>io.searchbox</groupId>
<artifactId>jest</artifactId>
<version>5.3.4</version>
</dependency>
http://localhost:9200/ 版本信息:"number" : "5.6.12",
{ "name" : "iHLGkjp", "cluster_name" : "elasticsearch", "cluster_uuid" : "L8JIRpCYSKOvdqkGLa68xA", "version" : { "number" : "5.6.12", "build_hash" : "cfe3d9f", "build_date" : "2018-09-10T20:12:43.732Z", "build_snapshot" : false, "lucene_version" : "6.6.1" }, "tagline" : "You Know, for Search" }
2.检查jest配置是否自动生效
配置application.properties :
#spring.elasticsearch.jest.uris=http://localhost:9200
默认值的话不用配置
进入:org.springframework.boot.autoconfigure.elasticsearch.jest.JestAutoConfiguration
配置信息前缀:spring.elasticsearch.jest
@ConfigurationProperties(prefix = "spring.elasticsearch.jest") public class JestProperties {
默认连接信息:
private List<String> uris = new ArrayList<String>( Collections.singletonList("http://localhost:9200"));
启动不报错 即配置成功。
3.使用jest存储数据。
3.1创建对象
并且在主键上添加 @JestId 注解
package com.invi.bean;
import io.searchbox.annotations.JestId;
public class Article {
@JestId
private Integer id;
private String author;
private String title;
private String content;
//get & set
}
3.2编写测试类
1.注入 JestClient。
2.构建索引 传入对象 索引 类型。
3.执行索引。
package com.invi;
import com.invi.bean.Article;
import io.searchbox.client.JestClient;
import io.searchbox.core.Index;
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.io.IOException;
@RunWith(SpringRunner.class)
@SpringBootTest
public class Springboot02ElasticApplicationTests {
@Autowired
JestClient jestClient;
@Test
public void contextLoads() {
Article article = new Article();
article.setId(1);
article.setAuthor("村上春树");
article.setTitle("《挪威森林》");
article.setContent("每个人都有属于自己的一片森林,也许我们 从来不曾去过,但它一直在那里,总会在那里。迷失的人迷失了,相逢的人会再相逢。");
//构建索引 io.searchbox.core.Index;
Index index = new Index.Builder(article).index("book").type("ana").build();
try {
//执行
jestClient.execute(index);
} catch (IOException e) {
e.printStackTrace();
}
}
}
运行测试方法。
查看结果:
发送get请求:http://localhost:9200/book/ana/1
结果:
{
"_index": "book",
"_type": "ana",
"_id": "1",
"_version": 2,
"found": true,
"_source": {
"id": 1,
"author": "村上春树",
"title": "《挪威森林》",
"content": "每个人都有属于自己的一片森林,也许我们 从来不曾去过,但它一直在那里,总会在那里。迷失的人迷失了,相逢的人会再相逢。"
}
}
发现,存储成功!
4.使用jest查询数据
查询 content中包含 “我”
package com.invi;
import com.invi.bean.Article;
import io.searchbox.client.JestClient;
import io.searchbox.core.Index;
import io.searchbox.core.Search;
import io.searchbox.core.SearchResult;
import org.elasticsearch.Build;
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.io.IOException;
@RunWith(SpringRunner.class)
@SpringBootTest
public class Springboot02ElasticApplicationTests {
@Autowired
JestClient jestClient;
@Test
public void build() {
Article article = new Article();
article.setId(1);
article.setAuthor("村上春树");
article.setTitle("《挪威森林》");
article.setContent("每个人都有属于自己的一片森林,也许我们 从来不曾去过,但它一直在那里,总会在那里。迷失的人迷失了,相逢的人会再相逢。");
//构建索引
Index index = new Index.Builder(article).index("book").type("ana").build();
try {
//执行
jestClient.execute(index);
} catch (IOException e) {
e.printStackTrace();
}
}
@Test
public void find() {
//查询表达式
String json = "{\n" +
" \"query\" : {\n" +
" \"match\" : {\n" +
" \"content\" : \"我\"\n" +
" }\n" +
" }\n" +
"}";
//构建搜索功能
Search search = new Search.Builder(json).addIndex("book").addType("ana").build();
try {
//执行
SearchResult result = jestClient.execute(search);
System.out.println("result:" + result.getJsonString());
} catch (IOException e) {
e.printStackTrace();
}
}
}
执行结果:
{
"took": 100,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 0.26051596,
"hits": [{
"_index": "book",
"_type": "ana",
"_id": "1",
"_score": 0.26051596,
"_source": {
"id": 1,
"author": "村上春树",
"title": "《挪威森林》",
"content": "每个人都有属于自己的一片森林,也许我们 从来不曾去过,但它一直在那里,总会在那里。迷失的人迷失了,相逢的人会再相逢。"
}
}]
}
}
发现,查询成功!
网友评论