赞
踩
设置web配置
选择NoSQL elasticsearch数据库+驱动
<dependencies> <!-- https://mvnrepository.com/artifact/org.springframework.data/spring-data-elasticsearch --> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-elasticsearch</artifactId> <version>3.1.2.RELEASE</version> </dependency> <dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>elasticsearch-rest-high-level-client</artifactId> <version>${elasticsearch.version}</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-elasticsearch</artifactId> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>2.1.6.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency> <!--json直接转对象,springboot可以自动选择版本--> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies>
指定cluster-name 具体文件在es安装的conf目录下的.yml结尾文件
设置端口为9300
spring:
data:
elasticsearch:
cluster-name: my-es
cluster-nodes: 192.168.56.100:9300
repositories:
enabled: true
分层开发包结构
实体类最重要的是如果使用构造器,必须在实体类中写好一个无参构造,否则会有错误,
实体类必须实现序列化接口
//这里指定自生成或插入的index名,以及type名 @Data @Document(indexName = "titleindex",type = "_doc") public class TitleDocument implements Serializable { public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getTitlename() { return titlename; } public void setTitlename(String titlename) { this.titlename = titlename; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } /** * */ @Id private Long id; /** * 选中ik 分词器 */ @Field(type = FieldType.Text, analyzer = "ik_max_word", searchAnalyzer = "ik_smart") private String titlename; @Field(type = FieldType.Text, analyzer = "ik_max_word", searchAnalyzer = "ik_smart") private String description; public TitleDocument() { } public TitleDocument(Long id, String titlename, String description) { this.id = id; this.titlename = titlename; this.description = description; } }
继承elasticsearch包的强大接口,实现CRUD功能
@Component
public interface TitleRepository extends ElasticsearchRepository<TitleDocument,Long> {
}
@Service public class TitleDocumentService { @Autowired private TitleRepository titleRepository; private TitleDocument document; /** * 创建索引 * @param titlename * @param description */ public void save(Long id,String titlename,String description) throws NoSuchAlgorithmException { TitleDocument titleDocument = new TitleDocument(id, titlename+id, description+id); titleRepository.save(titleDocument); } /** * 删除索引数据 * @param id * @return */ public boolean delete(Long id) { titleRepository.deleteById(id); return true; } /** * 查询所有的文章 * @param searchContent * @return */ public List<TitleDocument> getTitle(String searchContent) { QueryStringQueryBuilder builder = new QueryStringQueryBuilder(searchContent); System.out.println("查询的语句:"+builder); Iterable<TitleDocument> searchResult = titleRepository.search(builder); Iterator<TitleDocument> iterator = searchResult.iterator(); List<TitleDocument> list=new ArrayList<>(); while (iterator.hasNext()) { list.add(iterator.next()); } return list; } /** * 分页查询 * @param pageNumber * @param pageSize * @param searchContent * @return */ public List<TitleDocument> pageTitle(Integer pageNumber, Integer pageSize, String searchContent) { // 分页参数 PageRequest pageRequest = PageRequest.of(pageNumber, pageSize); QueryStringQueryBuilder queryBuilder = new QueryStringQueryBuilder(searchContent); NativeSearchQuery searchQuery = new NativeSearchQueryBuilder().withPageable(pageRequest).withQuery(queryBuilder).build(); System.out.println("查询的语句:" + searchQuery.getQuery().toString()); Page<TitleDocument> search = titleRepository.search(searchQuery); return search.getContent(); } public boolean deleteDoc() { titleRepository.deleteAll(); return true; } }
@RestController @RequestMapping public class TitleController { @Autowired private TitleDocumentService titleService; private TitleRepository repository; /** * 给索引添加数据 * * @return */ @PostMapping("save") public String save(Long id, String titlename, String description) throws NoSuchAlgorithmException { titleService.save(id, titlename, description); return "success"; } /** * 删除 * * @return */ @PostMapping("delete") public String delete(Long id) { titleService.delete(id); return "success"; } @GetMapping("get") public List<TitleDocument> getTitle(String param) { List<TitleDocument> titleDocumentList = titleService.getTitle(param); return titleDocumentList; } @GetMapping("page") public List<TitleDocument> pageTitle(Integer pageNumber, Integer pageSize, String searchContent) { List<TitleDocument> documentList = titleService.pageTitle(pageNumber, pageSize, searchContent); return documentList; } @PostMapping("deleteDoc") public String deleteDoc(){ titleService.deleteDoc(); return "success" ; } }
package com.njbdqn.springes.controller; import com.njbdqn.springes.service.TitleDocumentService; 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; @RunWith(SpringRunner.class) @SpringBootTest public class TitleControllerTest { @Autowired private TitleDocumentService titleService; @Test public void save() throws Exception { for (int i = 0; i < 20; i++) { titleService.save(Long.parseLong(String.valueOf(i)),"测试名称", "测试内容"); System.out.println("插入第"+i+"条"); } } }
成功即可在es中可以查看到相应数据
并且插入的过程会在控制台打印
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。