1.创建springboot项目
Pom文件引入elasticsearch依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
2. yml配置文件添加elasticsearch依赖
#elasticsearch 配置
spring:
data:
elasticsearch:
cluster-name: elasticsearch
cluster-nodes: 127.0.0.1:9300
repositories:
enabled: true
3. 创建Document 实体类和对应repository
实体类
@Data
@Document(indexName = "book",type = "_doc")
public class BookBean {
@Id
private String id;
private String title;
private String author;
private String postDate;
public BookBean(){}
public BookBean(String id, String title, String author, String postDate){
this.id=id;
this.title=title;
this.author=author;
this.postDate=postDate;
}
@Override
public String toString() {
return "BookBean{" +
"id='" + id + '\'' +
", title='" + title + '\'' +
", author='" + author + '\'' +
", postDate='" + postDate + '\'' +
'}';
}
}
对应repository
public interface BookRepository extends ElasticsearchRepository<BookBean,String> {
}
4.创建测试类
@RunWith(SpringRunner.class)
@SpringBootTest
public class TestEs {
@Autowired
private ElasticsearchTemplate elasticsearchTemplate;
@Autowired
private BookRepository bookRepository;
*/
/**
* 创建索引
*/
@Test
public void createIndex() {
// 创建索引,会根据BookBean类的@Document注解信息来创建
elasticsearchTemplate.createIndex(BookBean.class);
// 配置映射,会根据Item类中的id、Field等字段来自动完成映射
elasticsearchTemplate.putMapping(BookBean.class);
}
/**
* 删除索引
*/
@Test
public void deleteBookIndex() {
elasticsearchTemplate.deleteIndex("book");
}
/**
* 删除所有
*/
@Test
public void delete() {
bookRepository.deleteAll();
}
/**
* 新增
*/
@Test
public void insert() {
BookBean book = new BookBean("2", "ES教程", "程裕强", "2019-10-01");
bookRepository.save(book);
}
/**
* 批量新增
*/
@Test
public void insertList() {
List<BookBean> list = new ArrayList<>();
list.add(new BookBean("3", "ES教程", "程裕强", "2019-10-01"));
list.add(new BookBean("4", "ES教程", "程裕强", "2019-10-01"));
list.add(new BookBean("5", "ES教程", "程裕强", "2019-10-01"));
list.add(new BookBean("6", "ES教程", "程裕强", "2019-10-01"));
//接收对象集合,实现批量新增
bookRepository.saveAll(list);
}
/**
* 多字段匹配同一内容
* title、author 匹配同一内容 “ES教程”
*/
@Test
public void multiMatchQuery() {
QueryBuilder queryBuilder = QueryBuilders.multiMatchQuery("ES教程", "title", "author");
Iterable<BookBean> search = bookRepository.search(queryBuilder);
for (BookBean bo : search) {
System.out.println(bo);
}
}
/**
* 多字段查询,成功
*/
@Test
public void matchQuery() {
JSONObject map = new JSONObject();
map.put("title", "ES教程");
map.put("author", "程裕强");
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
for (String in : map.keySet()) {
String str = map.getString(in);
boolQueryBuilder.should(QueryBuilders.matchQuery(in, str));
}
int pageSize = 10;
int pagenum = 1;
// 第几页,页面大小
Pageable pageable = PageRequest.of(pagenum, pageSize);
//构建查询
NativeSearchQuery build = new NativeSearchQueryBuilder()
.withPageable(pageable)
.withQuery(boolQueryBuilder)
.build();
//返回查询结果
Page<BookBean> search = bossRepository.search(build);
System.out.println(search.getTotalElements());
System.out.println(search.getTotalPages());
search.forEach(System.out::println);
}
}
ES快速构建java项目会放在git上