栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

springboot整合elasticsearch

springboot整合elasticsearch

maven依赖
       
            org.springframework.boot
            spring-boot-starter-data-elasticsearch
        
yml配置
# es 服务地址
elasticsearch.host=127.0.0.1
# es 服务端口
elasticsearch.port=9200
Java配置类
import lombok.Data;
import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.elasticsearch.config.AbstractElasticsearchConfiguration;


@ConfigurationProperties(prefix = "elasticsearch")
@Configuration
@Data
public class ElasticsearchConfig extends AbstractElasticsearchConfiguration {
    private String host;
    private Integer port;
    @Override
    public RestHighLevelClient elasticsearchClient() {
        RestClientBuilder builder = RestClient.builder(new HttpHost(host, port));
        RestHighLevelClient restHighLevelClient = new
                RestHighLevelClient(builder);
        return restHighLevelClient;
    }
}
实体类
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.document;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;


@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
@document(indexName = "product",shards = 3,replicas = 1)//shards主分片replicas副本
public class Product {
    @Id()
    private Long id;//商品唯一标识
    @Field(type = FieldType.Text,analyzer = "ik_max_word")
    private String title;//商品名称
    @Field(type = FieldType.Keyword)
    private String category;//分类名称
    @Field(type = FieldType.Double)
    private Double price;//商品价格
    @Field(type = FieldType.Keyword,index = false)
    private String images;//图片地址
}
Dao
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;

public interface ProductDao extends ElasticsearchRepository {
}
写个controller

类似下面代码。项目启动时索引会自动创建

   public R es(@RequestBody VO request) {
        log.info("######参数是{}", JSON.toJSonString(request));
        Product product = new Product();
//        product.setId(3L);
        product.setTitle("华为手机");
        product.setCategory("手机");
        product.setPrice(2999.00);
        product.setImages("http://www.gh.com");
        return R.ok(productDao.save(product));
    }
采用ik分词,类型text支持分词 spring官网配置elasticsearch

Spring Data Elasticsearch - Reference documentation

其他ES笔记地址

Elasticsearch7学习笔记(上) - 简书Elasticsearch7学习笔记(上)[https://www.jianshu.com/p/29e5b1a7ce61]Elasticsearch7学习笔记(中)[http...https://www.jianshu.com/p/29e5b1a7ce61

上一篇:介绍了es采用docker-compose的方式安装

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/730438.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号