Elasticsearch 入门教程

5 篇文章 0 订阅

1.下载

2.安装

3.Spring整合Elasticsearch

4.ES注解

5.接口测试


1.下载

Elasticsearchhttps://www.elastic.co/cn/downloads/past-releases/elasticsearch-6-6-2

Kibana(Elasticsearch的客户端)https://www.elastic.co/cn/downloads/past-releases/kibana-6-6-2

 

下载好的包

2.安装

全部解压:

安装 ik 中文分词插件:

在elasticsearch-6.6.2\bin目录下执行以下命令:elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v6.6.2/elasticsearch-analysis-ik-6.6.2.zip

启动Elasticsearch:

运行bin目录下的elasticsearch.bat启动

执行命令 curl localhost:9200,会得到以下说明信息即成功

启动 Kibana:

运行bin目录下的 kibana.bat,启动 Kibana 的用户界面

访问http://localhost:5601 即可打开Kibana的用户界面

启动 Kibana 不成功:

则要修改 Elasticsearch 安装目录的config/elasticsearch.yml文件,去掉network.host的注释,将它的值改成0.0.0.0,然后重新启动 Elasticsearch。

3.Spring整合Elasticsearch

添加依赖

<!--Elasticsearch相关依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
        </dependency>

修改spring配置

  data:
    elasticsearch:
      repositories:
        enabled: true
      cluster-nodes: 127.0.0.1:9300 #es连接地址及端口
      cluster-name: elasticsearch #es集群的名称

添加文档对象EsProduct

不需要中文分词的字段设置成@Field(type = FieldType.Keyword)类型,需要中文分词的设置成@Field(analyzer = "ik_max_word",type = FieldType.Text)类型。

@Data
@Document(indexName = "mall",type = "product",shards = 1,replicas = 0)
public class EsProduct implements Serializable {

    private static final long serialVersionUID = -4143445098613480553L;

    @Id
    private Long id;
    @Field(type = FieldType.Keyword)
    private String productSn;
    @Field(type = FieldType.Keyword)
    private String brandName;
    @Field(analyzer = "ik_max_word",type = FieldType.Text)
    private String name;
    @Field(analyzer = "ik_max_word",type = FieldType.Text)
    private String subTitle;
    @Field(analyzer = "ik_max_word",type = FieldType.Text)
    private String keywords;

}

添加EsProductRepository接口用于操作Elasticsearch

/**
 * 继承ElasticsearchRepository接口,这样就拥有了一些基本的Elasticsearch数据操作方法,同时定义了一个衍生查询方法
 */
public interface EsProductRepository extends ElasticsearchRepository<EsProduct,Long> {

    Page<EsProduct> findByNameOrSubTitleOrKeywords(String name, String subTitle, String keywords, Pageable page);

}

添加EsProductService接口和EsProductServiceImpl实现类

public interface EsProductService {

    /**
     * 从数据库中导入所有产品到ES
     */
    int importAll();

    /**
     * 根据关键字搜索名称或者副标题
     */
    Page<EsProduct> search(String keyword, Integer pageNum, Integer pageSize);
}
@Service
public class EsProductServiceImpl implements EsProductService {

    @Autowired
    private EsProductRepository productRepository;
    @Autowired
    private EsProductDao productDao;

    @Override
    public int importAll() {
        List<EsProduct> esProductList = productDao.getAllEsProductList();
        Iterable<EsProduct> esProductIterable = productRepository.saveAll(esProductList);
        Iterator<EsProduct> iterator = esProductIterable.iterator();
        int result = 0;
        while (iterator.hasNext()) {
            result++;
            iterator.next();
        }
        return result;
    }

    @Override
    public Page<EsProduct> search(String keyword, Integer pageNum, Integer pageSize) {
        Pageable pageable = PageRequest.of(pageNum, pageSize);
        return productRepository.findByNameOrSubTitleOrKeywords(keyword, keyword, keyword, pageable);

    }

}

添加EsProductController控制器

@Api(tags = "EsProductController", description = "搜索产品控制器")
@RestController
@RequestMapping("/es/product")
public class EsProductController {
    @Autowired
    private EsProductService esProductService;

    @ApiOperation(value = "将数据库中的产品数据写入到ES")
    @RequestMapping(value = "/importAll", method = RequestMethod.GET)
    public CommonResult<Integer> importAllList() {
        int count = esProductService.importAll();
        return CommonResult.success(count);
    }

    @ApiOperation(value = "全文检索产品")
    @RequestMapping(value = "/search", method = RequestMethod.GET)
    public CommonResult<CommonPage<EsProduct>> search(@RequestParam(required = false) String keyword,
                                                      @RequestParam(required = false, defaultValue = "0") Integer pageNum,
                                                      @RequestParam(required = false, defaultValue = "5") Integer pageSize) {
        Page<EsProduct> esProductPage = esProductService.search(keyword, pageNum, pageSize);
        return CommonResult.success(CommonPage.restPage(esProductPage));
    }
}

4.ES注解

@Document

//标示映射到Elasticsearch文档上的领域对象
//Index 里面单条的记录称为 Document(文档)。许多条 Document 构成了一个 Index。
public @interface Document {
  //索引库名次,mysql中数据库的概念
    String indexName();
  //文档类型,mysql中表的概念
    String type() default "";
  //默认分片数
    short shards() default 5;
  //默认副本数量
    short replicas() default 1;

}

@Id

//表示是文档的id,文档可以认为是mysql中表行的概念

@Field

public @interface Field {
  //文档中字段的类型
    FieldType type() default FieldType.Auto;
  //是否建立倒排索引
    boolean index() default true;
  //是否进行存储
    boolean store() default false;
  //分词器名次
    String analyzer() default "";
}
//为文档自动指定元数据类型
public enum FieldType {
    Text,//会进行分词并建了索引的字符类型
    Integer,
    Long,
    Date,
    Float,
    Double,
    Boolean,
    Object,
    Auto,//自动判断字段类型
    Nested,//嵌套对象类型
    Ip,
    Attachment,
    Keyword//不会进行分词建立索引的类型
}

5.接口测试

将数据库中数据导入到Elasticsearch

  • 数据库:

  • 测试结果

  • kibana查看写入数据

  • kibana检索数据

查询ES中的产品信息

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值