温馨提示:
es,kibana,ik的安装可看之前的文章
docker安装elasticsearch,kibana,ik分词器_Give_time_to_Bug的博客-CSDN博客
再次强调:
elasticsearch版本与springboot版本一定要对应,否则bug极多
我安装的elasticsearch是7.6.2版本,对应使用的springboot是2.3.2.RELEASE
接下来介绍两种接入方式:
一.使用high-level-client的方式
1.pom依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<!-- 这里我的springboot的版本使用是2.3.2 -->
<version>2.3.2.RELEASE</version>
</parent>
<!--es客户端-->
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>7.6.2</version>
</dependency>
2.配置文件application.properties
es.ip=127.0.0.1
es.port=9200
3.客户端配置文件
@ConfigurationProperties("es")
@Data
@Component
public class ESProperties {
private String ip;
private int port;
}
@Configuration
public class ESConfig {
@Autowired
private ESProperties properties;
@Bean
public RestHighLevelClient esRestClient(){
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(
//在这里配置你的elasticsearch的情况
new HttpHost(properties.getIp(), properties.getPort(), "http")
)
);
return client;
}
}
4.service接口调用
@Autowired
private RestHighLevelClient client;
@Override
public List<Shop> query(String keyword) throws IOException {
SearchRequest request = new SearchRequest("shop");
SearchSourceBuilder builder = new SearchSourceBuilder();
BoolQueryBuilder query = QueryBuilders.boolQuery();
query.should(QueryBuilders.matchQuery("name", keyword));
query.should(QueryBuilders.matchQuery("location", keyword));
builder.query(query)
.from(0)
.size(5);
request.source(builder);
SearchResponse search = client.search(request, RequestOptions.DEFAULT);
SearchHit[] hits = search.getHits().getHits();
return Arrays.stream(hits).map(hit -> {
String str = hit.getSourceAsString();
return JSONObject.parseObject(str, Shop.class);
}).collect(Collectors.toList());
}
二.使用spring-boot-data的方式
1.依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
<exclusions>
<exclusion>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
</exclusion>
</exclusions>
</dependency>
注:因为我pom 中有high-level,所以我手动剔除了一下
2.application.properties配置
spring.elasticsearch.rest.uris=http://127.0.0.1:9200
3.实体类
@Data
@Accessors(chain = true)
@AllArgsConstructor
@NoArgsConstructor
@Document(indexName = "shop",shards = 1,replicas = 1)
public class Shop implements Serializable {
//@Id
@Field(type = FieldType.Integer,name = "id")
@ApiModelProperty(value = "主键")
private Integer id;
@ApiModelProperty(value = "名称")
@Field(type = FieldType.Text,name = "name", analyzer = "ik_max_word", searchAnalyzer = "ik_smart")
private String name;
@ApiModelProperty(value = "地址")
@Field(type = FieldType.Text,name = "location", analyzer = "ik_max_word", searchAnalyzer = "ik_smart")
private String location;
@ApiModelProperty(value = "平均消费")
@Field(type = FieldType.Integer,name = "price_per_man")
private Integer pricePerMan;
}
4.dao层接口
@Repository
public interface ShopDao extends ElasticsearchRepository<Shop, Long> {
List<Shop> findByNameOrLocation(String name,String location);
}
注:这个方法是自定义方法,es会自动解析为查询name和location,因为查询内容为两个属性,所以传参需要是两个文章来源:https://www.toymoban.com/news/detail-414827.html
5.service接口调用文章来源地址https://www.toymoban.com/news/detail-414827.html
@Autowired
private ShopDao shopDao;
@Override
public List<Shop> test(String keyword) {
return shopDao.findByNameOrLocation(keyword,keyword);
}
到了这里,关于springboot整合elasticsearch:7.6.2版本的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!