安装启动ElasticSearch
- docker安装
docker pull elasticsearch
启动:
docker run -e ES_JAVA_OPTS="-Xms256m -Xmx256m" -d -p 9200:9200 -p 9300:9300 --name ES01 5acf0e8da90b
输入网址访问:出现以下页面表示安装启动成功
检索
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-rn6jYlpY-1575010999847)(http://www.chshady.club/upload/2019/3/QQ截图2019040514534620190425112246548.png)]
elasticSearch基本使用方式:
参考官方文档https://www.elastic.co/guide/cn/elasticsearch/guide/current/highlighting-intro.html
springboot整合elasticsearch
jestClient模式
配置uris:
spring:
elasticsearch:
jest:
uris: http://192.168.25.128:9200
写一个测试类:注明jestId,使用注解@JestId
package com.rabbit.springbootelasticsearch.bean;
import io.searchbox.annotations.JestId;
public class Book {
private String bookName;
private String author;
@JestId
private Integer id;
public String getBookName() {
return bookName;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getAuthor() {
return author;
}
public void setBookName(String bookName) {
this.bookName = bookName;
}
public void setAuthor(String author) {
this.author = author;
}
public Book(String bookName, String author) {
this.bookName = bookName;
this.author = author;
}
public Book() {
}
@Override
public String toString() {
return "Book{" +
"bookName='" + bookName + '\'' +
", author='" + author + '\'' +
'}';
}
}
测试:添加,搜索
@Autowired
JestClient jestClient;
@Test
public void contextLoads() {
//给es中索引保存一个文档
Book book = new Book();
book.setId(1);
book.setAuthor("金庸");
book.setBookName("倚天屠龙记");
//构建一个索引功能
Index index = new Index.Builder(book).index("rabbit").type("news").build();
try {
//执行
jestClient.execute(index);
} catch (IOException e) {
e.printStackTrace();
}
}
//使用jest进行搜索
@Test
public void test2(){
String json = "{\n" +
" \"query\" : {\n" +
" \"match\" : {\n" +
" \"content\" : \"屠龙\"\n" +
" }\n" +
" }\n" +
"}";
//构建搜索功能
Search search = new Search.Builder(json).addIndex("rabbit").addType("news").build();
try {
SearchResult result = jestClient.execute(search);
System.out.println(result.getJsonString());
} catch (IOException e) {
e.printStackTrace();
}
}
Spring Data ElasticSearch
注意:Spring Data ElasticSearch 需要对应版本:详细信息看官网https://github.com/spring-projects/spring-data-elasticsearch
本次用的是spring-boot1.5.12版本对应ElasticSearch2.4.6版本;
配置信息:
spring:
data:
elasticsearch:
cluster-name: elasticsearch
#ip地址加上映射elasticsearch的9300端口号
cluster-nodes: 192.168.25.128:9302
测试:
写一个Bean,bean上写上@Document注解配置该数据的indexName和type
@Document(indexName ="rabbit",type = "book")
public class Book {
private String bookName;
private String author;
private Integer id;
public String getBookName() {
return bookName;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getAuthor() {
return author;
}
public void setBookName(String bookName) {
this.bookName = bookName;
}
public void setAuthor(String author) {
this.author = author;
}
public Book(String bookName, String author) {
this.bookName = bookName;
this.author = author;
}
public Book() {
}
@Override
public String toString() {
return "Book{" +
"bookName='" + bookName + '\'' +
", author='" + author + '\'' +
'}';
}
}
写一个接口作为操作ElasticSearch的template
//Book为传入的类,Integer为主键类型
public interface BookRepostiory extends ElasticsearchRepository<Book,Integer> {
//模糊查询搜索,不用谢实现就能直接用
public List<Book> findByBookNameLike(String bookName);
}
相关的命名规则可以从官网查看:https://github.com/spring-projects/spring-data-elasticsearch
比如:
public interface BookRepository extends Repository<Book, String> {
//通过name和price查询
List<Book> findByNameAndPrice(String name, Integer price);
//通过name或price查询
List<Book> findByNameOrPrice(String name, Integer price);
//通过name查询
Page<Book> findByName(String name,Pageable page);
Page<Book> findByNameNot(String name,Pageable page);
Page<Book> findByPriceBetween(int price,Pageable page);
Page<Book> findByNameLike(String name,Pageable page);
@Query("{\"bool\" : {\"must\" : {\"term\" : {\"message\" : \"?0\"}}}}")
Page<Book> findByMessage(String message, Pageable pageable);
}
测试检索和搜索:
//添加到检索
@Test
public void test3(){
Book book = new Book();
book.setId(1);
book.setBookName("倚天屠龙记");
book.setAuthor("金庸");
bookRepostiory.index(book);
}
//使用自己定义的方法搜索
@Test
public void test4(){
List<Book> book = bookRepostiory.findByBookNameLike("倚天");
System.out.println(book);
}