idea新建一个module
idea/file/new/module/Spring Initializr


springboot 集成 Elasticsearch7.2.0 ,head 插件
pom.xml导入包
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>starter-data-elasticsearch</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
项目结构

module的applicaiton.properties配置
spring.data.elasticsearch.cluster-nodes=192.168.1.116:9300
# 该配置和Elasticsearch的elasticsearch.yml中的配置信息有关
spring.data.elasticsearch.cluster-name=czg-es-springboot
创建EsCustomer实体
@Data
//useServerConfiguration 表示使用线上的index map和index配置
//createIndex 是否自动插件document实体对应的index
//设置elasticsearch的实体映射 http://www.luyixian.cn/news_show_103399.aspx
//注意之前设置为doc,在调用save时报错,后来改为_do
@Document(indexName = "cztest",type = "_doc",createIndex = false) //useServerConfiguration = true
public class EsCustomer {
@Field(type = FieldType.Long)
@Id
Long id;
@Field(type = FieldType.Text,analyzer = "") //如果涉及中文需要使用analyzer = "ik_max_word",主要es需要安装ik_max_word插件
private String firstName; //first_name
@Field(type = FieldType.Text,analyzer = "") //如果涉及中文需要使用analyzer = "ik_max_word",主要es需要安装ik_max_word插件
private String lastName; //last_name
@Field(type = FieldType.Integer)
private int age;
@Field(type = FieldType.Text,analyzer = "") //如果涉及中文需要使用analyzer = "ik_max_word",主要es需要安装ik_max_word插件
private String about;
/* @Field(type = FieldType.Text)
private String[] interests;*/
}
创建接口EsCustomerRepository继承ElasticsearchRepository(提供类似jpa的接口实现)
public interface EsCustomerRepository extends ElasticsearchRepository<EsCustomer,Integer>{
}
编写单元测试用例EsCustomerRepositoryTest
@RunWith(SpringRunner.class)
@SpringBootTest
public class EsCustomerRepositoryTest {
/* @Autowired
ElasticsearchTemplate template;*/
@Autowired
EsCustomerRepository esCustomerRepository;
//node {#transport#-1}{dReBn_47QuuIGRuK4n0EpA}{192.168.1.116}{192.168.1.116:9300} not part of the cluster Cluster [Hq5OeP5TQmO6OVP3puv7RQ], ignoring...
//no such index [springbootCustomer] https://blog.csdn.net/chen_2890/article/details/83895646
@Test
public void testEs_all(){
Iterable<EsCustomer> all = esCustomerRepository.findAll();
Iterator<EsCustomer> iterator = all.iterator();
EsCustomer esCustomer = iterator.next();
if (null !=esCustomer){
System.out.println("------"+esCustomer.getAbout());
}
}
@Test
public void testEs_sava(){
EsCustomer esCustomer = new EsCustomer();
esCustomer.setId(3L);
esCustomer.setFirstName("cai");
esCustomer.setLastName("haiyun");
esCustomer.setAbout("喜欢打球");
esCustomer.setAge(33);
esCustomerRepository.save(esCustomer);
System.out.println("ok");
}
/* POST cztest/_search
{
"query": {
"bool": {
"should": [
{"match": {
"last_name": "haibin"
}},
{"match": {
"last_name": "yueliang"
}}
]
}
}
}*/
@Test
public void testEs_search(){
String keyword = "hai";
String keyword2 = "haibin";
HashMap<String,Object> map = new HashMap<>();
StopWatch watch = new StopWatch();
watch.start();//开始计时
BoolQueryBuilder builder = QueryBuilders.boolQuery();
builder.should(QueryBuilders.matchPhraseQuery("last_name",keyword));
builder.should(QueryBuilders.matchPhraseQuery("last_name",keyword2));
String s = builder.toString();
/*Iterable<EsCustomer> search = esCustomerRepository.search(builder);*/
Page<EsCustomer> search = (Page<EsCustomer>) esCustomerRepository.search(builder);
List<EsCustomer> content = search.getContent();
map.put("list",content);
watch.stop();//结束计时
long totalMillis = watch.totalTime().getMillis();
map.put("duration",totalMillis);
}
}
测试效果

网友评论