ElasticSearch:进阶,Java操作ES-配置

ElasticSearch:进阶,Java操作ES-配置

ElasticSearch:进阶,Java操作ES-配置

ElasticSearch:进阶,Java操作ES-配置

为什么不用JS来操作ES呢,安全,主要是安全, ES集群一般不暴露给客户端,一般是由Java来操作,客户端访问Java就行了。

第二个原因,JS对ES的支持度不高。

ElasticSearch:进阶,Java操作ES-配置

low和high的关系就类似  mybatis和JDBC的关系。

 

 

 

========

创建新项目,导入依赖 

<!-- 导入es的rest-high-level-client-->
<dependency>
    <groupId>org.elasticsearch.client</groupId>
    <artifactId>elasticsearch-rest-high-level-client</artifactId>
    <version>7.4.2</version>
</dependency>

ElasticSearch:进阶,Java操作ES-配置

ElasticSearch:进阶,Java操作ES-配置

更改版本:

ElasticSearch:进阶,Java操作ES-配置

 

=====================

配置ES

common中引用了注册中心

<dependency>
    <groupId>com.atguigu.gulimall</groupId>
    <artifactId>gulimall-common</artifactId>
    <version>0.0.1-SNAPSHOT</version>
</dependency>
# 应用名称
spring.application.name=gulimall-search
# 应用服务 WEB 访问端口
server.port=8080
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848

 启用注册发现:

@EnableDiscoveryClient
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
public class GulimallSearchApplication {

    public static void main(String[] args) {
        SpringApplication.run(GulimallSearchApplication.class, args);
    }

}

 这里排除了数据源,数据源在commons项目中引用的,这里没使用

配置ES:

package com.atguigu.gulimall.search.config;

import org.apache.http.HttpHost;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 整合es
 * 1.导入依赖
 * 2.编写配置  给容器注入一个 RestHighLevelClient
 * 3.参照api
 *
 */
@Configuration
public class GuliMallElasticSearchConfig {
 

    @Bean
    public RestHighLevelClient esRestClient(){
        //String hostname, int port, String scheme
        RestHighLevelClient client = new RestHighLevelClient(
                RestClient.builder(
                        new HttpHost("192.168.155.3",9200,"http")));//http协议不写也行,默认就是
        return client;
    }
}

 

测试是否创建对象成功:

@RunWith(SpringRunner.class) // 指定用spring的驱动来跑
@SpringBootTest
class GulimallSearchApplicationTests {

    @Autowired
    private RestHighLevelClient client;

    @Test
    public void contextLoads() {
        System.out.println(client);
    }



}

ElasticSearch:进阶,Java操作ES-配置

 

上一篇:根据上一行填充本行的空白栏位,SQL处理方式


下一篇:es 7.x 核心概念