springboot快速整合elaticsearch
匯入所需的jar包
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
(沒有配置start-parent需要指定版本號)
</dependency>
配置elaticsearch配置類
@Configuration
public class ElasticSearchConfig {
@Bean
public RestHighLevelClient restHighLevelClient(){
return new RestHighLevelClient(RestClient.builder(new HttpHost("ip號",9200,"http")));
}
}
啟動專案便可以使用了
簡單使用dome
@RestController
@RequestMapping("/blogtest/weblog")
@CrossOrigin //配置跨域
public class WeblogController {
@Autowired
WeblogService weblogService;
@GetMapping("add")
public result addWeblog(@RequestBody Weblog weblog){
boolean save = weblogService.insertOne(weblog);
return save?result.success():result.error();
}
}
Service
public class WeblogServiceImpl extends ServiceImpl<WeblogMapper, Weblog> implements WeblogService {
@Autowired
RestHighLevelClient restHighLevelClient;
@Autowired
LabelWeblogService labelWeblogService;
//批量插入(定時插入)
public Boolean insertWeblog() {
BulkRequest bulkRequest=new BulkRequest();
bulkRequest.timeout("2m");
List<Weblog> weblogs = baseMapper.selectList(null);
weblogs.forEach(x->{
bulkRequest.add(new IndexRequest("weblog").source(JSON.toJSONString(x), XContentType.JSON));
});
try{
BulkResponse bulk = restHighLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT);
return ! bulk.hasFailures();
}catch (Exception e){
throw new MyException();
}
}
//高亮搜尋
public List<Map<String,Object>> dataSearch(String keyword,Integer pageOne ,Integer pageEdn){
SearchRequest request=new SearchRequest();
SearchSourceBuilder builder=new SearchSourceBuilder();
builder.from(pageOne);
builder.size(pageEdn);
//模糊查詢
MatchQueryBuilder matchQueryBuilder = new MatchQueryBuilder("desc",keyword);
builder.query(matchQueryBuilder);
builder.timeout(new TimeValue(60, TimeUnit.SECONDS));
request.source(builder);
//高亮顯示
HighlightBuilder highlightBuilder=new HighlightBuilder();
highlightBuilder.field("desc");
highlightBuilder.preTags("<span style='color:#cccccc'>");
highlightBuilder.postTags("</span");
highlightBuilder.requireFieldMatch(false);
builder.highlighter(highlightBuilder);
List<Map<String,Object>>list=new ArrayList<>();
//返回資料
try {
SearchResponse response = restHighLevelClient.search(request, RequestOptions.DEFAULT);
//hit屬性
for(SearchHit hit:response.getHits().getHits()){
Map<String, Object> result = hit.getSourceAsMap();
//解析高亮欄位
Map<String, HighlightField> highlightFields = hit.getHighlightFields();
HighlightField desc = highlightFields.get("desc");
if(desc!=null){
Text[] fragments = desc.fragments();
String w_desc="";
for(Text t:fragments){
w_desc+=t;
}
result.put("desc",w_desc);
}
list.add(result);
}
return list;
} catch (IOException e) {
e.printStackTrace();
}
return list;
}
//插入並放入搜尋引擎
@Override
public Boolean insertOne(Weblog weblog) {
IndexRequest request = new IndexRequest("weblog").source(JSON.toJSONString(weblog), XContentType.JSON);
baseMapper.insert(weblog);
try {
IndexResponse response = restHighLevelClient.index(request, RequestOptions.DEFAULT);
return response!=null;
} catch (IOException e) {
e.printStackTrace();
}
return false;
}
}
這樣就可以快樂的使用es了
相關文章
- 【Springboot】快速整合QuartzSpring Bootquartz
- 「Elasticsearch」SpringBoot快速整合ESElasticsearchSpring Boot
- SpringBoot快速整合通用MapperSpring BootAPP
- SpringBoot整合RabbitMQ(一)快速入門Spring BootMQ
- Elaticsearch倒排索引索引
- SpringBoot快速整合Mybatis(去XML化+註解進階)Spring BootMyBatisXML
- SpringBoot快速整合SpringBootAdmin管控臺監控服務Spring Boot
- Elaticsearch基礎概念
- SpringBoot(19)---SpringBoot整合ApolloSpring Boot
- SpringBoot(17)---SpringBoot整合RocketMQSpring BootMQ
- SpringBoot(十六)_springboot整合JasperReSpring Boot
- SpringBoot整合系列-整合JPASpring Boot
- 在 laravel 中使用 elaticsearchLaravel
- 【入門教程】5分鐘教你快速學會整合Java springboot ~JavaSpring Boot
- SpringBoot 整合 rabbitmqSpring BootMQ
- SpringBoot 整合 apolloSpring Boot
- SpringBoot 整合 elkSpring Boot
- SpringBoot 整合 elasticsearchSpring BootElasticsearch
- SpringBoot整合elasticsearchSpring BootElasticsearch
- RocketMQ整合SpringBootMQSpring Boot
- springboot整合redis?Spring BootRedis
- SpringBoot整合MinIOSpring Boot
- Springboot整合pagehelperSpring Boot
- ElasticSearch 整合 SpringBootElasticsearchSpring Boot
- springboot 整合LogBackSpring Boot
- SpringBoot整合ESSpring Boot
- springBoot 整合 mybatisSpring BootMyBatis
- SpringBoot整合NacosSpring Boot
- SpringBoot 整合 RedisSpring BootRedis
- SpringBoot整合DubboSpring Boot
- springboot 整合jeagerSpring Boot
- 【SpringBoot】整合RedisSpring BootRedis
- Springboot整合MybatisSpring BootMyBatis
- SpringBoot整合SwaggerSpring BootSwagger
- SpringBoot 整合 dockerSpring BootDocker
- flowable 整合 springbootSpring Boot
- Springboot整合RabbitMQSpring BootMQ
- springBoot整合flowableSpring Boot