关于ElasticSearch的使用

一、安装

        下载地址:https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.3.0-linux-x86_64.tar.gz,这是针对lunix的版本,其中7.3.0是自己的需要的版本号,换成自己对应的version就可以下载对应的版本

针对单机版的:

       在服务器上找个目录解压,然后修改配置文件 conf/elasticsearch.yml配置文件

1.需要打开配置文件中

node.name: node-1

2.配置network.host以便除本机以外也能访问

network.host: (自己的该服务的IP地址)

3.配置cluster.initial_master_nodes,对应第1的名字

cluster.initial_master_nodes: ["node-1"]

4.配置以便java作为访问端使用

http.port: 9200
transport.tcp.port: 9300
transport.tcp.compress: true

然后可以启动bin目录下的./elasticsearch

找个服务端,或者postman访问 http://127.0.0.1:9200?pretty,获得如下结果表明安装成功即可使用

注:pretty是按照格式化的json返回响应结果

{
  "name": "node-1",
  "cluster_name": "elasticsearch",
  "cluster_uuid": "RYw3p_UFT_qGuIWD2Hg3oA",
  "version": {
    "number": "7.2.0",
    "build_flavor": "default",
    "build_type": "tar",
    "build_hash": "508c38a",
    "build_date": "2019-06-20T15:54:18.811730Z",
    "build_snapshot": false,
    "lucene_version": "8.0.0",
    "minimum_wire_compatibility_version": "6.8.0",
    "minimum_index_compatibility_version": "6.0.0-beta1"
  },
  "tagline": "You Know, for Search"
}

二,配置分词插件

按照https://github.com/KennFalcon/elasticsearch-analysis-hanlp即可安装成功,并可使用hanlp作为分词,但是自己好像hanlp_index,这种分词还是报错,因为不涉及,所以没有继续关注了

三,使用logstash 插件将mysql中的数据导入elasticsearch中

安装logstash

下载https://www.elastic.co/cn/downloads/,下载logstash,然后,没有什么大的问题可以直接按照以下配置,启动进行从mysql导入到elasticsearch中了 

input {
        jdbc {
        #连接数据库的驱动jar包位置,可以自己定义一个位置,最后能找大就行
        jdbc_driver_library => "./mysql-connector-java-5.1.45.jar"
        #连接mysql必须配置 
        jdbc_driver_class => "com.mysql.jdbc.Driver"
        jdbc_connection_string =>"*****"
        jdbc_user => "name"
        jdbc_password => "password"
        #分页配置  默认100000
        jdbc_page_size => 100000
        jdbc_paging_enabled => true
        #连接数据库的查询语句
        #statement_filepath => "filename.sql"  sql写到文件中,填写能找到文件的位置
        statement => "select * from dx_weak_bind"
        #记录位置
        last_run_metadata_path => "./syncpoint_table4.log"
      }
    }
    output {
        elasticsearch { 
            # es 地址
            hosts => "ip:9200"
            # 索引名称
            index => "dx_weak_bind"
            # document_id  默认自己生成 可也有数据库赋值
            document_id => "%{id}" 
            manage_template => true
            template_overwrite => true
            # 模板名称对应下面的模板文件名称
            template_name => "dx_weak_bind"
            # 模板位置
            template => "./config/template-mysql4.json"
      }
    }

注:对于该配置文件,只需要配置自己需要的就行,不用那么麻烦

四、针对springboot嵌入elasticsearch使用

java端报错

Exception in thread "main" NoNodeAvailableException[None of the configured nodes are available: [{#transport#-1}{GRphuiRLRgOVTP0707mGFQ}{192.168.11.216}{192.168.11.216:9200}]]

服务端也报错,类似于

java.lang.IllegalStateException: Received message from unsupported version: [6.4.2] minimal compatible version is: [6.8.0]

以上很有可能就是,elasticsearch版本和springboot的端口,对应不上,目前好像最新springboot的也没有跟上elasticsearch的版本,如果elasticsearch使用的最新版本,那么建议自己配一个client

EXAMPLE

maven导入一以下包

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

java端的client


@Component
public class EsClient {

    @Autowired
    private Config config;
    private static RestHighLevelClient client = null;

    public RestHighLevelClient getClient() {
        if (null != client) {
            return client;
        }
        client = new RestHighLevelClient(RestClient.builder(new HttpHost(config.getHost(), config.getPort(), "http")));
        return client;
    }
}

使用,查询

@Override
public Map<String, Object> relationJdSn(String skuName) {

        try {
            MatchQueryBuilder builder = QueryBuilders.matchQuery("sku_name", skuName).analyzer("hanlp");
            SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
            sourceBuilder.query(builder);
            SearchRequest searchRequest = new SearchRequest(conf.getIndex());
            searchRequest.source(sourceBuilder);
            RestHighLevelClient client = esClient.getClient();
            SearchResponse search = client.search(searchRequest, RequestOptions.DEFAULT);               
            if (null != search) {
                SearchHit[] hits = search.getHits().getHits();
                for (int i = 0; i < hits.length; i++) {
                    // 处理获得的每一个元素
                    }
           }

            return hm;
        } catch (Exception e) {
            throw new InternalServerException(e);
        }
    }

关于application.properties的配置

# elasticsearch
elasticsearch.proxy.host=ip地址
elasticsearch.proxy.port=9200
elasticsearch.proxy.index=索引名称

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值