SpringBoot_EasyEs_Elasticsearch
第一步
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>cn.easy-es</groupId>
<artifactId>easy-es-boot-starter</artifactId>
<version>1.1.0</version>
<exclusions>
<exclusion>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
</exclusion>
<exclusion>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>7.14.0</version>
</dependency>
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>7.14.0</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.26</version>
</dependency>
第二步
@SpringBootApplication
@EsMapperScan("com.example.springbooteasyeselasticsearch.dao")
public class SpringbootEasyesElasticsearchApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootEasyesElasticsearchApplication.class, args);
}
}
第三步
@Data
@AllArgsConstructor
@NoArgsConstructor
@IndexName("document")
public class Document implements Serializable {
@IndexId(type = IdType.NONE)
private String id;
@IndexField(fieldType = FieldType.KEYWORD)
private String title;
@IndexField(fieldType = FieldType.TEXT)
@HighLight
private String content;
@IndexField(fieldType = FieldType.KEYWORD_TEXT)
private String description;
}
第四步
- 定义 mapper 层实体类
DocumentMapper
public interface DocumentMapper extends BaseEsMapper<Document> {
}
第五步
- 定义 service 层接口
IDocumentService
public interface IDocumentService {
Integer insert(Document document);
}
第六步
- 实现 service 层接口
DocumentServiceImpl
@Service
public class DocumentServiceImpl implements IDocumentService {
@Resource
private DocumentMapper documentMapper;
@Override
public Integer insert(Document document) {
return documentMapper.insert(document);
}
}
第七步
- 定义 controller 层测试类
DocumentController
@RestController
@Slf4j
@RequestMapping("/document")
public class DocumentController {
@Resource
private IDocumentService documentService;
@Resource
private DocumentMapper documentMapper;
@PostMapping("/insert")
public Integer insert(@RequestBody Document document) {
return documentService.insert(document);
}
@PostMapping("/insertDocument")
public Integer insertDocument(@RequestBody Document document) {
return documentMapper.insert(document);
}
@PostMapping("/list")
public List<Document> getListDocument() {
LambdaEsQueryWrapper lambdaEsQueryWrapper = new LambdaEsQueryWrapper();
lambdaEsQueryWrapper.matchAllQuery();
return documentMapper.selectList(lambdaEsQueryWrapper);
}
@PostMapping("/page")
public EsPageInfo<Document> getPageDocument() {
LambdaEsQueryWrapper lambdaEsQueryWrapper = new LambdaEsQueryWrapper();
lambdaEsQueryWrapper.matchAllQuery();
return documentMapper.pageQuery(lambdaEsQueryWrapper, 1,10);
}
@PostMapping("/sa_page")
public SAPageInfo<Document> getSAPageDocument() {
LambdaEsQueryWrapper<Document> lambdaEsQueryWrapper = EsWrappers.lambdaQuery(Document.class);
lambdaEsQueryWrapper.size(10);
lambdaEsQueryWrapper.orderByDesc(Document::getId);
SAPageInfo<Document> saPageInfo = documentMapper.searchAfterPage(lambdaEsQueryWrapper, null, 5);
System.out.println("saPageInfo = " + saPageInfo.getList() + "; saPageInfo.getSearchAfter() = " + saPageInfo.getSearchAfter() + "; saPageInfo.getNextSearchAfter() = " + saPageInfo.getNextSearchAfter());
SAPageInfo<Document> searchAfterPage = documentMapper.searchAfterPage(lambdaEsQueryWrapper, saPageInfo.getNextSearchAfter(), 5);
System.out.println("searchAfterPage = " + searchAfterPage.getList() + "; searchAfterPage.getSearchAfter() = " + searchAfterPage.getSearchAfter() + "; searchAfterPage.getNextSearchAfter() = " + searchAfterPage.getNextSearchAfter());
return searchAfterPage;
}
}