SpringBoot 默认支持两种技术来和 ES 交互:
io.searchbox.client.JestClient;
docker pull elasticsearch:6.8.13
docker run --name elasticsearch -p 9200:9200 -p 9300:9300 -e ES_JAVA_OPTS="-Xms512m -Xmx512m" -d elasticsearch:6.8.13
,注意分配内存,默认为 2GBhttp://localhost:9200/
,若显示内容,则代表安装启动成功导入依赖
<!-- https://mvnrepository.com/artifact/io.searchbox/jest -->
<dependency>
<groupId>io.searchbox</groupId>
<artifactId>jest</artifactId>
<version>6.3.1</version>
</dependency>
编写配置文件
# 这也是默认值
spring.elasticsearch.jest.uris=http://localhost:9200
编写 Article 实体类:
public class Article {
@JestId
private Integer id;
private String author;
private String title;
private String content;
// constructor setter getter toString
}
编写测试文件:
@RunWith(SpringRunner.class)
@SpringBootTest
public class IntegrationSearchApplicationTests {
@Autowired
JestClient jestClient; // Jest 操作 ES
// 创建一个索引 http://localhost:9200/parzulpan/tips/1001
@Test
public void testJestCreate() {
// 给 ES 中 索引(保存)一个文档
Article article = new Article(1001, "消息通知", "zs", "Hello World");
// 构建一个索引
Index index = new Index.Builder(article).index("parzulpan").type("tips").build();
try {
// 执行
DocumentResult result = jestClient.execute(index);
System.out.println(result.getJsonString());
} catch (IOException e) {
e.printStackTrace();
}
}
// 全文搜索
@Test
public void testJestSearch() {
// 全文搜索 查询表达式
String json = "{\n" +
" \"query\" : {\n" +
" \"match\" : {\n" +
" \"content\" : \"Hello\"\n" +
" }\n" +
" }\n" +
"}";
// 构建一个搜索
Search search = new Search.Builder(json).addIndex("parzulpan").addType("tips").build();
try {
// 执行
SearchResult result = jestClient.execute(search);
System.out.println(result.getJsonString());
} catch (IOException e) {
e.printStackTrace();
}
}
}
导入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
注意,Spring1.5.22.RELEASE 默认使用的 elasticsearch 版本为 2.4.6。有可能和安装的 ES 版本不合适,可以参考官方说明,解决这个问题有两种方法:
升级 SpringBoot
安装对应的 Elasticsearch,安装:docker pull elasticsearch:2.4.6
,运行:docker run --name elasticsearch2 -p 9201:9200 -p 9301:9300 -e ES_JAVA_OPTS="-Xms256m -Xmx256m" -d elasticsearch:2.4.6
编写配置文件
# 这也是默认值
spring.elasticsearch.jest.uris=http://localhost:9200
## elasticsearch:6.8.13
#spring.data.elasticsearch.cluster-name=docker-cluster
#spring.data.elasticsearch.cluster-nodes=localhost:9300
# elasticsearch:2.4.6
spring.data.elasticsearch.cluster-name=elasticsearch
spring.data.elasticsearch.cluster-nodes=localhost:9301
编写 Book 实体类
编写 ElasticsearchRepository 的子接口
/**
* @Author : parzulpan
* @Time : 2021-01
* @Desc : 操作 ES
*/
public interface BookRepository extends ElasticsearchRepository<Book, Integer> {
// 更多可参考:https://docs.spring.io/spring-data/elasticsearch/docs/2.1.23.RELEASE/reference/html/#reference
public List<Book> findByBookNameLike(String bookName); // 自定义方法,按书名模糊查询
}
编写测试文件:
@RunWith(SpringRunner.class)
@SpringBootTest
public class IntegrationSearchApplicationTests {
@Autowired
BookRepository bookRepository; // 编写 ElasticsearchRepository 的子接口 来 操作 ES
// http://localhost:9201/parzulpan/book/1
@Test
public void testElasticsearchRepositoryCreate() {
Book book = new Book(1, "Elasticsearch 实战", "parzulpan");
Book index = bookRepository.index(book);
System.out.println(index);
}
@Test
public void testElasticsearchRepositorySearch() {
List<Book> books = bookRepository.findByBookNameLike("实战");
System.out.println(books);
}
}
【SpringBoot1.x】SpringBoot1.x 检索
原文:https://www.cnblogs.com/parzulpan/p/14223659.html