elasticsearch小白入门

一般再项目中都会用到 搜索,如果直接查询数据库,性能会存在瓶颈。 这时,用ES就很好的解决这个问题。 

ES组件很多:包括 elasticsearch kibana beats  logstash

安装 elasticsearch

下载:

Elasticsearch 7.10.2 | Elastic

 

elasticsearch解压后,运行 bin目录下的 elasticsearch-7.10.2\bin>elasticsearch.bat

浏览器输入 127.0.0.1:9200 ,安装成功了。 

安装kibana

下载:

Kibana 7.10.2 | Elastic

解压后修改配置 kibana-7.10.2-windows-x86_64\config\kibana.yml

 

执行 kibana-7.10.2-windows-x86_64\bin>kibana.bat

浏览器访问:  http://localhost:5601

以上是 windows安装elasticsearch,kibana,用来开发环境测试用。

Java语言 使用 Java High Level REST Client 开发

添加依赖

<dependency>
    <groupId>org.elasticsearch.client</groupId>
    <artifactId>elasticsearch-rest-high-level-client</artifactId>
    <version>7.10.2</version>
</dependency>


        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
        </dependency>

向索引中新增数据

public void index(){

        IndexResponse indexResponse = null;
        try {

            // 连接ES服务
            RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(
                    new HttpHost("localhost", 9200, "http")));

            //构建 IndexRequest 并设置索引名称
            IndexRequest request = new IndexRequest("defindex");

            // 索引内容
            String jsonString = "{" +
                    "\"user\":\"zs\"," +
                    "\"time\":\"0000-11-09\"," +
                    "\"message\":\"hello,es"" +
                    "}";
            request.source(jsonString, XContentType.JSON);
            indexResponse = client.index(request, RequestOptions.DEFAULT);
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println(indexResponse);
    }

未完.....

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值