集成依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.2.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
</dependencies>
配置参数
spring:
elasticsearch:
uris: http://XXXX:9200
username: elastic
password: 'XXXX'
定义Entity
@Data
@Document(indexName = "resource_index")
public class ResourceEntity {
@Id
private String id;
@Field(type = FieldType.Text, analyzer = "ik_max_word", searchAnalyzer = "ik_max_word")
private String title;
public ResourceEntity() {
}
public ResourceEntity(String id, String title) {
this.id = id;
this.title = title;
}
}
定义Repository
public interface ResourceEntityRepository extends ElasticsearchRepository<ResourceEntity, String> {
@Highlight(
fields = {@HighlightField(name = "title")},
parameters = @HighlightParameters(preTags = {"<span style='color:red'>"}, postTags = {"</span>"}, numberOfFragments = 0)
)
List<SearchHit<ResourceEntity>> findByTitle(String title);
}
分页查询
@Highlight(
fields = {@HighlightField(name = "title")},
parameters = @HighlightParameters(preTags = {"<span style='color:red'>"}, postTags = {"</span>"}, numberOfFragments = 0)
)
ist<SearchHit<ResourceEntity>> findByTitle(String title, Pageable pageable);
}
测试
@Test
public void findByTitleTest(){
List<SearchHit<ResourceEntity>> testEntityList = repository.findByTitle("文档");
testEntityList.forEach(testEntity -> {
log.info("testEntity:{}",testEntity);
});
}
本站资源均来自互联网,仅供研究学习,禁止违法使用和商用,产生法律纠纷本站概不负责!如果侵犯了您的权益请与我们联系!
转载请注明出处: 免费源码网-免费的源码资源网站 » springboot3 集成elasticsearch(es)客户端(高亮查询)
发表评论 取消回复