一般再项目中都会用到 搜索,如果直接查询数据库,性能会存在瓶颈。 这时,用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);
    } 
 
未完.....

















