1.pom文件
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.6.RELEASE</version>
<relativePath />
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
2.配置
spring.data.elasticsearch.cluster-nodes = 127.0.0.1:9300
3.model
package com.knife.springbootes2;
import java.io.Serializable;
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.FieldIndex;
import org.springframework.data.elasticsearch.annotations.FieldType;
@Document(indexName = "test", type = "article")
public class Article implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
@Id
@Field(index = FieldIndex.not_analyzed, store = true)
private String id;
@Field(type = FieldType.String, store = true)
private String title;
@Field(type = FieldType.String, store = true)
private String content;
@Field(type = FieldType.String, store = true)
private String author;
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String toString() {
return "{id:" + id + ",title:\"" + title + "\",content:\"" + content + "\"}";
}
}
4.注入ElasticsearchTemplate
@Autowired
private ElasticsearchTemplate elasticsearchTemplate;
5.增加
/**
* 插入或等新,需要有id,id需要自己生成
*
* @param tList
* @return
*/
@RequestMapping("/add")
@ResponseBody
public boolean add() {
String id = UUID.randomUUID().toString();
Article article = new Article();
article.setId(id);
article.setContent("罗浮山下梅花村,玉雪为骨冰为魂。");
article.setTitle("再用前韵");
article.setAuthor("苏轼");
try {
IndexQuery indexQuery = new IndexQueryBuilder().withId(id).withObject(article).build();
elasticsearchTemplate.index(indexQuery);
return true;
} catch (Exception e) {
return false;
}
}
6.修改
@RequestMapping("/update")
@ResponseBody
public boolean update(String id, String content) {
Article article = new Article();
article.setId(id);
article.setContent(content);
try {
IndexQuery indexQuery = new IndexQueryBuilder().withId(id).withObject(article).build();
elasticsearchTemplate.index(indexQuery);
return true;
} catch (Exception e) {
return false;
}
}
7.删除
@RequestMapping("delete")
@ResponseBody
public String delete(String id) {
return elasticsearchTemplate.delete(Article.class, id);
}
8.查找
@RequestMapping("query")
@ResponseBody
public List<Article> query(int page, String title) {
NativeSearchQueryBuilder searchQuery = new NativeSearchQueryBuilder();
BoolQueryBuilder bqb = QueryBuilders.boolQuery();
searchQuery.withIndices("test");
searchQuery.withPageable(new PageRequest(page, 5));
bqb.mustNot(QueryBuilders.termQuery("title", title));
List<Article> list = elasticsearchTemplate.queryForList(searchQuery.build(), Article.class);
return list;
}
9.创建索引
@RequestMapping("/createIndex")
@ResponseBody
public Object createIndex() {
return elasticsearchTemplate.createIndex("test");
}
10.创建map
@RequestMapping("/createMap")
@ResponseBody
public Object create() {
return elasticsearchTemplate.putMapping(Article.class);
}