feat: es原生
This commit is contained in:
@@ -15,7 +15,7 @@
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>8</maven.compiler.source>
|
||||
<maven.compiler.target>8</maven.compiler.target>
|
||||
<maven.compiler.target>8</maven.compiler.target>
|
||||
<maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
|
||||
|
@@ -13,7 +13,7 @@
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>8</maven.compiler.source>
|
||||
<maven.compiler.target>8</maven.compiler.target>
|
||||
<maven.compiler.target>8</maven.compiler.target>
|
||||
<java.version>1.8</java.version>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
|
||||
|
@@ -1,5 +1,6 @@
|
||||
package com.landaiqing.subject.application.controller;
|
||||
|
||||
import com.landaiqing.subject.infra.basic.service.SubjectEsService;
|
||||
import com.landaiqing.subject.infra.entity.UserInfo;
|
||||
import com.landaiqing.subject.infra.rpc.UserRpc;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
@@ -23,10 +24,31 @@ public class TestFeignController {
|
||||
@Resource
|
||||
private UserRpc userRpc;
|
||||
|
||||
@Resource
|
||||
private SubjectEsService subjectEsService;
|
||||
@GetMapping("testFeign")
|
||||
public void testFeign() {
|
||||
UserInfo userInfo = userRpc.getUserInfo("jichi");
|
||||
log.info("testFeign.userInfo:{}", userInfo);
|
||||
}
|
||||
@GetMapping("testCreateIndex")
|
||||
public void testCreateIndex() {
|
||||
subjectEsService.createIndex();
|
||||
}
|
||||
|
||||
@GetMapping("addDoc")
|
||||
public void addDoc() {
|
||||
subjectEsService.addDocs();
|
||||
}
|
||||
|
||||
@GetMapping("find")
|
||||
public void find() {
|
||||
subjectEsService.find();
|
||||
}
|
||||
|
||||
@GetMapping("search")
|
||||
public void search() {
|
||||
subjectEsService.search();
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -56,5 +56,10 @@
|
||||
<artifactId>qing-yu-club-auth-api</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
|
||||
<version>2.4.2</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
|
@@ -0,0 +1,42 @@
|
||||
package com.landaiqing.subject.infra.basic.entity;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
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.FieldType;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* @Classname SubjectInfoEs
|
||||
* @BelongsProject: qing-yu-club
|
||||
* @BelongsPackage: com.landaiqing.subject.infra.basic.entity
|
||||
* @Author: landaiqing
|
||||
* @CreateTime: 2024-03-04 18:14
|
||||
* @Description: TODO
|
||||
* @Version: 1.0
|
||||
*/
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
@Document(indexName = "subject_index",createIndex = false)
|
||||
public class SubjectInfoEs {
|
||||
@Field(type = FieldType.Long)
|
||||
@Id
|
||||
private Long id;
|
||||
|
||||
@Field(type = FieldType.Text, analyzer = "ik_smart")
|
||||
private String subjectName;
|
||||
|
||||
@Field(type = FieldType.Text, analyzer = "ik_smart")
|
||||
private String subjectAnswer;
|
||||
|
||||
@Field(type = FieldType.Keyword)
|
||||
private String createUser;
|
||||
|
||||
@Field(type = FieldType.Date,index = false)
|
||||
private Date createTime;
|
||||
}
|
@@ -0,0 +1,14 @@
|
||||
package com.landaiqing.subject.infra.basic.es;
|
||||
|
||||
/**
|
||||
* @Classname EsClusterConfig
|
||||
* @BelongsProject: qing-yu-club
|
||||
* @BelongsPackage: com.landaiqing.subject.infra.basic.es
|
||||
* @Author: landaiqing
|
||||
* @CreateTime: 2024-03-05 21:15
|
||||
* @Description: TODO
|
||||
* @Version: 1.0
|
||||
*/
|
||||
public class EsClusterConfig {
|
||||
private String name;
|
||||
}
|
@@ -0,0 +1,16 @@
|
||||
package com.landaiqing.subject.infra.basic.esRepo;
|
||||
|
||||
import com.landaiqing.subject.infra.basic.entity.SubjectInfoEs;
|
||||
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
|
||||
|
||||
/**
|
||||
* @Classname SubjectEsRepository
|
||||
* @BelongsProject: qing-yu-club
|
||||
* @BelongsPackage: com.landaiqing.subject.infra.basic.esRepo
|
||||
* @Author: landaiqing
|
||||
* @CreateTime: 2024-03-04 18:19
|
||||
* @Description: TODO
|
||||
* @Version: 1.0
|
||||
*/
|
||||
public interface SubjectEsRepository extends ElasticsearchRepository<SubjectInfoEs,Long> {
|
||||
}
|
@@ -0,0 +1,18 @@
|
||||
package com.landaiqing.subject.infra.basic.service;
|
||||
|
||||
/**
|
||||
* @Classname SubjectEsService
|
||||
* @BelongsProject: qing-yu-club
|
||||
* @BelongsPackage: com.landaiqing.subject.infra.basic.service
|
||||
* @Author: landaiqing
|
||||
* @CreateTime: 2024-03-04 18:21
|
||||
* @Description: TODO
|
||||
* @Version: 1.0
|
||||
*/
|
||||
public interface SubjectEsService {
|
||||
void createIndex();
|
||||
void addDocs();
|
||||
|
||||
void search();
|
||||
void find();
|
||||
}
|
@@ -0,0 +1,73 @@
|
||||
package com.landaiqing.subject.infra.basic.service.impl;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.landaiqing.subject.infra.basic.entity.SubjectInfoEs;
|
||||
import com.landaiqing.subject.infra.basic.esRepo.SubjectEsRepository;
|
||||
import com.landaiqing.subject.infra.basic.service.SubjectEsService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.elasticsearch.index.query.QueryBuilders;
|
||||
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
|
||||
import org.springframework.data.elasticsearch.core.IndexOperations;
|
||||
import org.springframework.data.elasticsearch.core.SearchHit;
|
||||
import org.springframework.data.elasticsearch.core.SearchHits;
|
||||
import org.springframework.data.elasticsearch.core.document.Document;
|
||||
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
|
||||
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.Date;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @Classname SubjectEsServiceImpl
|
||||
* @BelongsProject: qing-yu-club
|
||||
* @BelongsPackage: com.landaiqing.subject.infra.basic.service.impl
|
||||
* @Author: landaiqing
|
||||
* @CreateTime: 2024-03-04 18:22
|
||||
* @Description: TODO
|
||||
* @Version: 1.0
|
||||
*/
|
||||
@Service
|
||||
@Slf4j
|
||||
public class SubjectEsServiceImpl implements SubjectEsService {
|
||||
@Resource
|
||||
private ElasticsearchRestTemplate elasticsearchRestTemplate;
|
||||
@Resource
|
||||
private SubjectEsRepository subjectEsRepository;
|
||||
|
||||
@Override
|
||||
public void createIndex() {
|
||||
IndexOperations indexOperations =
|
||||
elasticsearchRestTemplate.indexOps(SubjectInfoEs.class);
|
||||
indexOperations.create();
|
||||
Document mapping = indexOperations.createMapping(SubjectInfoEs.class);
|
||||
indexOperations.putMapping(mapping);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addDocs() {
|
||||
List<SubjectInfoEs> list = new LinkedList<>();
|
||||
list.add(new SubjectInfoEs(1L, "redis 是什么", "redis是一个缓存", "landaiqing", new Date()));
|
||||
list.add(new SubjectInfoEs(2L, "mysql 是什么", "mysql是一个数据库", "landaiqing", new Date()));
|
||||
subjectEsRepository.saveAll(list);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void search() {
|
||||
NativeSearchQuery nativeSearchQuery = new NativeSearchQueryBuilder()
|
||||
.withQuery(QueryBuilders.matchQuery("subjectName", "redis")).build();
|
||||
SearchHits<SubjectInfoEs> search = elasticsearchRestTemplate.search(nativeSearchQuery, SubjectInfoEs.class);
|
||||
List<SearchHit<SubjectInfoEs>> searchHits = search.getSearchHits();
|
||||
log.info("searchHits:{}", JSON.toJSONString(searchHits));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void find() {
|
||||
Iterable<SubjectInfoEs> all = subjectEsRepository.findAll();
|
||||
for (SubjectInfoEs subjectInfoEs: all){
|
||||
log.info("subjectInfoEs:{}",JSON.toJSONString(subjectInfoEs));
|
||||
}
|
||||
}
|
||||
}
|
@@ -27,6 +27,9 @@ spring:
|
||||
enabled: true
|
||||
config:
|
||||
enabled: true
|
||||
elasticsearch:
|
||||
rest:
|
||||
uris: http://116.196.80.239:9200
|
||||
publicKey: MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAJ9zUefu5PFeiy4nNNRCIaNT5IY3IxRrlHMiotffSPstMensKg4PoSWJsRRrp/zQEzWegxz2Bkv3F5vfGqqM9N0CAwEAAQ==
|
||||
logging:
|
||||
config: classpath:log4j2-spring.xml
|
||||
|
Reference in New Issue
Block a user