依赖
<!--springdata elasticsearch-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
配置文件
spring:
elasticsearch:
rest:
uris:
- http://xx.xxx.xx.xx:9200
自动注入模板类对象
@Autowired
private ElasticsearchRestTemplate elasticsearchRestTemplate;
创建索引并设置映射
public void createIndex(){
boolean isIndex = elasticsearchRestTemplate.createIndex(TbItem.class);
System.out.println("创建索引:"+isIndex);
boolean isMapping = elasticsearchRestTemplate.putMapping(TbItem.class);
System.out.println("创建映射:"+isMapping);
}
删除索引
public void deleteIndex(){
boolean isDelete = elasticsearchRestTemplate.deleteIndex("product_item");
System.out.println(isDelete);
boolean b = elasticsearchRestTemplate.deleteIndex(TbItem.class);
System.out.println(b);
}
增
往es中批量增加数据
public void addAllData(){
List<TbItem> all = tbItemMapper.findAll();
ArrayList<IndexQuery> indexQueries = new ArrayList<>();
for (TbItem tbItem : all) {
IndexQuery indexQuery = new IndexQueryBuilder().withObject(tbItem).build();
indexQueries.add(indexQuery);
}
elasticsearchRestTemplate.bulkIndex(indexQueries);
}
往es中批量增加数据
public void addData(){
TbItem item = new TbItem();
item.setId(23423);
item.setTitle("迪奥口红");
item.setPrice(399);
item.setNum(3);
item.setSellPoint("涂了之后,她好,我也好");
IndexQuery indexQuery = new IndexQueryBuilder().withObject(item).build();
elasticsearchRestTemplate.index(indexQuery);
}
删
public void deleteData(){
String delete = elasticsearchRestTemplate.delete(TbItem.class, "605616");
System.out.println(delete);
}
改
public void updateData() throws IOException {
UpdateRequest updateRequest = new UpdateRequest();
updateRequest.doc(XContentFactory.jsonBuilder()
.startObject()
.field("title","香奈儿口红")
.endObject()
);
UpdateQuery updateQuery = new UpdateQueryBuilder()
.withUpdateRequest(updateRequest)
.withClass(TbItem.class)
.withId("23423").build();
UpdateResponse update = elasticsearchRestTemplate.update(updateQuery);
}
查
全搜索
public void machData(){
SearchQuery searchQuery =new NativeSearchQuery(QueryBuilders.matchAllQuery());
List<TbItem> tbItems = elasticsearchRestTemplate.queryForList(searchQuery, TbItem.class);
for (TbItem t : tbItems) {
System.out.println(t);
}
}
条件搜索:IK分词
public void ikMatchData(){
SearchQuery searchQuery =new NativeSearchQuery(QueryBuilders.matchQuery("title","口红"));
List<TbItem> tbItems = elasticsearchRestTemplate.queryForList(searchQuery, TbItem.class);
for (TbItem t : tbItems) {
System.out.println(t);
}
}
短语搜索:没有任何分词
public void matchPhraseData(){
SearchQuery searchQuery =new NativeSearchQuery(QueryBuilders.matchPhraseQuery("title","海尔(Haier)HM-M209手机"));
List<TbItem> tbItems = elasticsearchRestTemplate.queryForList(searchQuery, TbItem.class);
for (TbItem t : tbItems) {
System.out.println(t);
}
}
term搜索
public void termData(){
SearchQuery searchQuery =new NativeSearchQuery(QueryBuilders.termsQuery("title","阿尔卡特"));
List<TbItem> tbItems = elasticsearchRestTemplate.queryForList(searchQuery, TbItem.class);
for (TbItem t : tbItems) {
System.out.println(t);
}
}
范围搜索
public void testRange(){
SearchQuery searchQuery =new NativeSearchQuery(QueryBuilders.rangeQuery("price").gte(900).lte(3000));
List<TbItem> tbItems = elasticsearchRestTemplate.queryForList(searchQuery, TbItem.class);
for (TbItem t : tbItems) {
System.out.println(t);
}
}
多条件搜索:must,should,must_not
public void testMulti(){
BoolQueryBuilder builder = QueryBuilders.boolQuery();
List<QueryBuilder> must = builder.must();
must.add(QueryBuilders.matchQuery("sell_point","很好看"));
must.add(QueryBuilders.rangeQuery("price").gte(900).lte(3000));
SearchQuery searchQuery =new NativeSearchQuery(builder);
List<TbItem> tbItems = elasticsearchRestTemplate.queryForList(searchQuery, TbItem.class);
for (TbItem t : tbItems) {
System.out.println(t);
}
}
分页搜索
public void testPage(){
SearchQuery searchQuery =new NativeSearchQuery(QueryBuilders.matchAllQuery());
searchQuery.setPageable(PageRequest.of(0,3));
searchQuery.addSort(Sort.by(Sort.Direction.DESC,"price"));
List<TbItem> tbItems = elasticsearchRestTemplate.queryForList(searchQuery, TbItem.class);
for (TbItem t : tbItems) {
System.out.println(t);
}
}
|