Relational DB -> Databases -> Tables -> Rows -> Columns
Elasticsearch -> Indices -> Types -> Documents -> Fields
##使用Jest操作ES
<dependencies>
<dependency>
<groupId>io.searchboxgroupId>
<artifactId>jestartifactId>
<version>5.3.3version>
dependency>
dependencies>
spring:
elasticsearch:
jest:
uris: http://192.168.44.113:9200
package com.hf.boot.entity;
import io.searchbox.annotations.JestId;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;
/**
* @Copyright (C), 2016-2019 hf
* @FileName: Article
* @Author: hf
* @Date: 2019/10/9 21:12
* @Description: 文章实体类
*/
@Getter
@Setter
@AllArgsConstructor
public class Article {
//标识id
@JestId
private Integer id;
private String author;
private String title;
private String content;
}
@Autowired
JestClient jestClient;
@Test
public void test_jest02() {
//查询表达式
String json = "{\n" +
" \"query\" : {\n" +
" \"match_phrase\" : {\n" +
" \"content\" : \"婚礼\"\n" +
" }\n" +
" },\n" +
" \"highlight\": {\n" +
" \"fields\" : {\n" +
" \"content\" : {}\n" +
" }\n" +
" }\n" +
"}";
//构建搜索
Search search = new Search.Builder(json).addIndex("hf").addType("article").build();
try {
//执行
SearchResult result = jestClient.execute(search);
System.out.println(result.getJsonString());
} catch (IOException e) {
e.printStackTrace();
}
}
@Test
public void test_jest01() {
//1、给ES中索引一个文档
Article article = new Article(2, "陈二", "婚礼邀请", "本人与XX将于yyyy年mm月dd日在XX举行婚礼、欢迎大家参加。");
//构建索引
Index index = new Index.Builder(article).index("hf").type("article").build();
//执行
try {
jestClient.execute(index);
} catch (IOException e) {
e.printStackTrace();
}
}
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-data-elasticsearchartifactId>
dependency>
dependencies>
spring:
data:
elasticsearch:
cluster-name: elasticsearch-cluster
cluster-nodes: 192.168.44.113:9300,192.168.44.113:9301,192.168.44.113:9302
@NoRepositoryBean
public interface ElasticsearchRepository<T, ID extends Serializable> extends ElasticsearchCrudRepository<T, ID> {
<S extends T> S index(S entity);
Iterable<T> search(QueryBuilder query);
Page<T> search(QueryBuilder query, Pageable pageable);
Page<T> search(SearchQuery searchQuery);
Page<T> searchSimilar(T entity, String[] fields, Pageable pageable);
void refresh();
Class<T> getEntityClass();
}
package com.hf.boot.repository;
import com.hf.boot.entity.Book;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import java.util.List;
/**
* @Copyright (C), 2016-2019 hf
* @FileName: BookRepository
* @Author: hf
* @Date: 2019/10/9 21:33
* @Description: 通过 ElasticsearchRepository操作ES
*/
public interface BookRepository extends ElasticsearchRepository<Book,Integer> {
//自定义方法
List<Book> findByBookNameLike(String bookName);
}
自定义方法规则
Book实体
package com.hf.boot.entity;
import lombok.*;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;
/**
* @Copyright (C), 2016-2019 hf
* @FileName: Book
* @Author: hf
* @Date: 2019/10/9 21:35
* @Description: 书实体
*/
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@ToString
@Document(indexName = "hf",type = "book")
public class Book {
@Id
private Integer id;
//指定文本类型和分词器
@Field(type = FieldType.Text, analyzer = "ik_max_word")
private String bookName;
private String author;
}
@Autowired
BookRepository bookRepository;
@Test
public void test_elasticsearchRepository02() {
//查找数据
List<Book> list = bookRepository.findByBookNameLike("西");
System.out.println(list);
}
@Test
public void test_elasticsearchRepository01() {
//索引数据
Book book = new Book(5, "红楼梦", "曹雪芹");
bookRepository.index(book);
}
@Autowired
ElasticsearchTemplate elasticsearchTemplate;
@Test
public void test_esTemplate02() {
//查找数据
SearchQuery searchQuery = new NativeSearchQueryBuilder()
.withQuery(QueryBuilders.matchQuery("bookName", "ES"))
.withPageable(new PageRequest(0, 10))
.build();
List<Book> list = elasticsearchTemplate.queryForList(searchQuery, Book.class);
System.out.println(list);
}
@Test
public void test_esTemplate01() {
//索引数据
Book book = new Book(5, "ES入门到精通", "ES");
//构建
IndexQuery indexQuery = new IndexQueryBuilder()
.withId(book.getId() + "")
.withObject(book)
.build();
//执行
elasticsearchTemplate.index(indexQuery);
}