centos下springboot2.1.3集成Elasticsearch6.7.0

  • centos下springboot2.1.3集成Elasticsearch6.7.0已关闭评论
  • 13 views
  • A+
所属分类:elasticsearch

Elasticsearch下载安装

安装之前你需要关闭防火墙,关闭selinux,修改系统文件句柄数

关闭防火墙命令:

systemctl stop firewalld.service

关闭selinux:

修改/etc/selinux/config如下就行:

# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
# enforcing - SELinux security policy is enforced.
# permissive - SELinux prints warnings instead of enforcing.
# disabled - No SELinux policy is loaded.
SELINUX=disabled
# SELINUXTYPE= can take one of these two values:
# targeted - Targeted processes are protected,
# minimum - Modification of targeted policy. Only selected processes are protected. 
# mls - Multi Level Security protection.
SELINUXTYPE=targeted

修改系统文件句柄数:

修改/etc/security/limits.conf成如下就行:

*              hard    nofile          65536
*              soft    nofile          65536

注:如果你系统对一些用户用户有特殊配置的话,你可已针对启动本次的elasticsearch用户授权就行了。我这里是为了省事。

修改内核参数:

etc/sysctl.conf文件中添加一行,如下:

vm.max_map_count = 262144

使其生效,如下:

sysctl -p

安装jdk

这一块不说了,自己直接搜索安装吧。

下载安装elasticsearch

下载如下:

 wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.7.0.tar.gz

解压到/usr/local

tar -xzf elasticsearch-6.7.0.tar.gz -C /usr/local/

修改配置

 vi /usr/local/elasticsearch-6.7.0/config/elasticsearch.yml

需要修改两处,如下:

network.host: 192.168.241.128
http.port: 9200
cluster.name: fitnesssearch

network.host为你本机ip,端口你自己根据自己情况修改,我这里9200,群集名称待会连接的时候需要用

 

创建普通用户,并给安装目录授权

useradd elastic
chown -R elastic /usr/local/elasticsearch-6.7.0/

启动,root下启动如下:

su elastic -c "/usr/local/elasticsearch-6.7.0/bin/elasticsearch -d"

浏览器输入http://192.168.241.128:9200/结果如下:

{
 "name" : "AAL3K9N",
 "cluster_name" : "elasticsearch",
 "cluster_uuid" : "N1x1t2diSk-rbmDR360h1Q",
 "version" : {
 "number" : "6.7.0",
 "build_flavor" : "default",
 "build_type" : "tar",
 "build_hash" : "8453f77",
 "build_date" : "2019-03-21T15:32:29.844721Z",
 "build_snapshot" : false,
 "lucene_version" : "7.7.0",
 "minimum_wire_compatibility_version" : "5.6.0",
 "minimum_index_compatibility_version" : "5.0.0"
 },
 "tagline" : "You Know, for Search"
}

则ok了,如果失败,查看日志,日志在/usr/local/elasticsearch-6.7.0/logs下

安装中文分词器

我这里选elasticsearch-analysis-ik,elasticsearch对应的版本我选择ikv6.7.0的版本,安装如下:

# cd  /usr/local/elasticsearch-6.7.0/bin
# ./elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v6.7.0/elasticsearch-analysis-ik-6.7.0.zip

安装成功后重新启动el,查看日志如下,则说明安装成功:

[2019-08-01T12:06:13,316][INFO ][o.e.p.PluginsService ] [AAL3K9N] loaded plugin [analysis-ik]

现在我们验证一下,如下:

[root@k3s ~]# curl -XGET http://192.168.241.128:9200/_analyze?pretty -H 'Content-Type:application/json' -d' { "analyzer": "ik_smart", "text": "你大爷还是你大爷"}'

{
 "tokens" : [
 {
 "token" : "你",
 "start_offset" : 0,
 "end_offset" : 1,
 "type" : "CN_CHAR",
 "position" : 0
 },
 {
 "token" : "大爷",
 "start_offset" : 1,
 "end_offset" : 3,
 "type" : "CN_WORD",
 "position" : 1
 },
 {
 "token" : "还是",
 "start_offset" : 3,
 "end_offset" : 5,
 "type" : "CN_WORD",
 "position" : 2
 },
 {
 "token" : "你",
 "start_offset" : 5,
 "end_offset" : 6,
 "type" : "CN_CHAR",
 "position" : 3
 },
 {
 "token" : "大爷",
 "start_offset" : 6,
 "end_offset" : 8,
 "type" : "CN_WORD",
 "position" : 4
 }
 ]
}

 

安装简繁体转换插件

# cd  /usr/local/elasticsearch-6.7.0/bin
#./elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-stconvert/releases/download/v6.7.0/elasticsearch-analysis-stconvert-6.7.0.zip

安装拼音分词插件

# cd  /usr/local/elasticsearch-6.7.0/bin
#./elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-pinyin/releases/download/v6.7.0/elasticsearch-analysis-pinyin-6.7.0.zip

 

springboot连接

主要引用的jar包如下:

<dependency>
    <groupId>org.elasticsearch</groupId>
    <artifactId>elasticsearch</artifactId>
   <version>6.4.3</version>
</dependency>
<dependency>
    <groupId>org.elasticsearch.client</groupId>
    <artifactId>transport</artifactId>
    <version>6.4.3</version>
    <exclusions>
        <exclusion>
            <groupId>org.elasticsearch</groupId>
           <artifactId>elasticsearch</artifactId>
        </exclusion>
    </exclusions>
</dependency>
<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <version>1.16.20</version>
</dependency>

 

主要连接代码如下:

Settings esSetting = Settings.builder()
        .put("cluster.name", clusterName) //集群名字
        .put("client.transport.sniff", true)//增加嗅探机制,找到ES集群
        .put("thread_pool.search.size", Integer.parseInt(poolSize))//增加线程池个数,暂时设为5
        .build();
//配置信息Settings自定义
transportClient = new PreBuiltTransportClient(esSetting);
TransportAddress transportAddress = new TransportAddress(InetAddress.getByName(hostName), Integer.valueOf(port));
transportClient.addTransportAddresses(transportAddress);

创建索引代码如下:

CreateIndexResponse indexresponse = client.admin().indices().prepareCreate(index).execute().actionGet();
LOGGER.info("执行建立成功?" + indexresponse.isAcknowledged());
return indexresponse.isAcknowledged();

然后我们写个测试api,如下:

/**
 *el创建索引
 * @param
 * @return
 */
@RequestMapping(value = "/search/list", method = RequestMethod.POST)
@ResponseBody
@CrossOrigin
public String createIndex() {
    String indexName = "indextest";
    if (!ElasticsearchUtil.isIndexExist(indexName)) {
        ElasticsearchUtil.createIndex(indexName);
    } else {
        return "索引已存在";
    }
    return "OK";

}

然后我们执行以下,通过客户端看看结果如下:

[root@k3s logs]# curl '192.168.241.128:9200/_cat/indices?v'
health status index uuid pri rep docs.count docs.deleted store.size pri.store.size
yellow open indextest  ZooksK7pSyeLXSXJmC11yw 5 1 0 0 1.1kb 1.1kb

好到处结束,下一篇写以下如何通过中文分词器以及拼音和繁体字搜索

  • 安卓客户端下载
  • 微信扫一扫
  • weinxin
  • 微信公众号
  • 微信公众号扫一扫
  • weinxin
avatar