pom文件
1.8
1.8
org.elasticsearch
elasticsearch
5.6.8
org.elasticsearch.client
transport
5.6.8
org.slf4j
slf4j-api
1.7.24
org.apache.logging.log4j
log4j-to-slf4j
2.9.1
org.slf4j
slf4j-simple
1.7.21
log4j
log4j
1.2.12
junit
junit
4.12
com.fasterxml.jackson.core
jackson-core
2.8.1
com.fasterxml.jackson.core
jackson-databind
2.8.1
com.fasterxml.jackson.core
jackson-annotations
2.8.1
org.springframework.data
spring-data-elasticsearch
3.0.5.RELEASE
org.elasticsearch.plugin
transport-netty4-client
org.springframework
spring-test
5.0.4.RELEASE
org.springframework.data
spring-data-commons
2.0.5.RELEASE
(1)客户端:es集群名字,es集群包含的节点
(2)包扫描器:对定义的接口进行扫描,使用该接口调用springData提供的增删改查方法
(3)模板对象:springData提供了对es操作的模板
包括索引库,映射,文档,域
@Document(indexName = "sdes_blog", type = "article")
public class Article {
@Id
private long id;
@Field(type = FieldType.Long, store = true)
private String aid;
@Field(type = FieldType.text, store = true, analyzer = "ik_smart")
private String title;
@Field(type = FieldType.text, store = true, analyzer = "ik_smart")
private String content;
接口不用任何注解也能被扫描到,因为它继承自ElasticsearchRepository
public interface ArticleRespository extends ElasticsearchRepository {
}
使用Spring提供的测试方法,需要注入接口代理对象以及模板对象
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class SpringDataElasticSearchTest {
@Autowired
private ArticleRespository articleRespository;
@Autowired
private ElasticsearchTemplate template;
(1)创建索引库
public void createIndex() throws Exception {
template.createIndex(Article.class);
}
(2)插入记录
public void addDocument() throws Exception {
for (int i = 10; i < 20; i++) {
Article article = new Article();
article.setId(i);
article.setAid("0" + i);
article.setTitle("裁员200人后,他为何把自己也裁了?互联网HR自曝裁员内幕");
article.setContent("上海一家互联网公司的HR柯文,裁掉两百人后,把自己也裁掉了。");
articleRespository.save(article);
}
}
查看
(3)删除记录
public void deleteDocumentById() throws Exception {
articleRespository.deleteById(1l);
}
(4)修改记录
修改是先删除,再添加
public void updateDocumentById() throws Exception {
Article article = new Article();
article.setId(0);
article.setAid("01");
article.setTitle("修改第" + article.getAid() + "条记录");
article.setContent("修改第" + article.getAid() + "条记录的内容");
articleRespository.save(article);
}
(5)查询记录
//查询全部
public void findAll() throws Exception {
Iterable articles = articleRespository.findAll();
articles.forEach(article -> System.out.println(article));
}
//根据id查询
public void findById() throws Exception {
Optional optional = articleRespository.findById(1l);
Article article = optional.get();
System.out.println(article);
}
(6)自定义查询
完全按照springData的格式写,不需要写额外的代码
public interface ArticleRespository extends ElasticsearchRepository {
List findByTitle(String title);
List findByTitleOrContent(String title,String content);
List findByTitleOrContent(String title, String content, Pageable pageable);
}
//根据title查询
public void findByTitle() throws Exception {
List list = articleRespository.findByTitle("制裁");
list.stream().forEach(article -> System.out.println(article));
}
//带有分页的查询
public void findByTitleOrContent() throws Exception {
PageRequest pageable = PageRequest.of(0, 20);
List list = articleRespository.findByTitleOrContent("制裁", "互联网公司", pageable);
list.stream().forEach(article -> System.out.println(article));
}
(7)原生查询
使用自定义的查询,查询语句是and的形式,而且是有序的。使用下面的自定义查询,虽然包含关键词,但是查询不出任何结果。因为它顺序和索引库中词的顺序不一致。
public void findByTitle() throws Exception {
List list = articleRespository.findByTitle("伊朗被美国制裁");
list.stream().forEach(article -> System.out.println(article));
}
使用原生查询,词的顺序是可以不一致的。
public void testNativeSearchQuery() throws Exception {
NativeSearchQuery query = new NativeSearchQueryBuilder()
.withQuery(QueryBuilders.queryStringQuery("伊朗被美国制裁").defaultField("title"))
.withPageable(PageRequest.of(0, 5))
.build();
List list = template.queryForList(query, Article.class);
list.stream().forEach(article -> System.out.println(article));
}