一、安装
(1)elasticsearch:
(2)ik分词器:
注意:
1、不同版本服务器对应不同分词器,对照表:
3、经常报错及解决方式:https:///qq_35225231/article/details/78431664。答案在评论中
二、使用
(1)启动本地elasticsearch
(2)代码demo实现
1、依赖
<dependency><!-- 全文搜索引擎 -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
2、配置
#全文搜索 elasticsearch
spring.data.elasticsearch.cluster-name=elasticsearch
spring.data.elasticsearch.cluster-nodes=localhost:9300
3、dao配置
import java.util.List;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
import com.example.demo.dal.entity.Article;
@Repository
public interface ArticleRepository extends CrudRepository<Article, Long> {
List<Article> findByTitleContaining(String title);
}
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.core.query.Criteria;
import org.springframework.data.elasticsearch.core.query.CriteriaQuery;
import org.springframework.stereotype.Repository;
import com.example.demo.dal.entity.Article;
@Repository
public class ArticleTemplate {
@Autowired
private ElasticsearchTemplate elasticsearchTemplate;
public List<Article> queryByTitle(String title) {
return elasticsearchTemplate.queryForList(new CriteriaQuery(Criteria.where("title").contains(title)),
Article.class);
}
}
entity
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 = "cxytiandi02", type = "article")
public class Article {
@Id
@Field(type = FieldType.Integer, index = FieldIndex.not_analyzed, store = true)
private Integer id;
@Field(type = FieldType.String, index = FieldIndex.not_analyzed, store = true)
private String sid;
@Field(type = FieldType.String, index = FieldIndex.not_analyzed, store = true)
private String title;
@Field(type = FieldType.String, index = FieldIndex.not_analyzed, store = true, analyzer = "ik_max_word", searchAnalyzer = "ik_max_word")
private String url;
@Field(type = FieldType.String, index = FieldIndex.not_analyzed, store = true)
private String content;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getSid() {
return sid;
}
public void setSid(String sid) {
this.sid = sid;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}
test
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import com.example.demo.dal.dao.ArticleRepository;
import com.example.demo.dal.dao.ArticleTemplate;
import com.example.demo.dal.entity.Article;
@RunWith(SpringRunner.class)
@SpringBootTest
public class ArticleTest {
@Autowired
ArticleRepository articleRepository;
@Autowired
ArticleTemplate articleTemplate;
@Test
public void testAdd() {
Article article = new Article();
article.setId(2);
article.setSid("dak219dksd");
article.setTitle("java 好难啊");
article.setUrl("http://baidu.com");
article.setContent("java 及的垃圾的 的垃圾大家导入大大大");
articleRepository.save(article);
}
// @Test
public void testList() {
Iterable<Article> list = articleRepository.findAll();
for (Article article : list) {
System.out.println(article.getTitle());
}
}
// @Test
public void testQuery() {
Iterable<Article> list = articleRepository.findByTitleContaining("java");
for (Article article : list) {
System.out.println(article.getTitle());
}
}
@Test
public void testQueryByTitle() {
List<Article> list = articleTemplate.queryByTitle("java");
for (Article article : list) {
System.out.println(article.getTitle());
}
}
}
效果结果:
因篇幅问题不能全部显示,请点此查看更多更全内容