elasticsearch 安装和使用

142 阅读1分钟

解压安装包

tar -zxvf elasticsearch-*.tar.gz

新建用户 es

group add elasticsearch 
user add es -g elasticsearch
chown -R es elasticsearch 

启动

切换es用户,启动

sh ./bin/elasticsearch -d

启动报错

1 max file descriptors [4096] for elasticsearch process is too low, increase to at least [65536]

查看当前用户打开最大文件数命令:

ulimit -n

1024

vim /etc/security/limits.conf

es soft nofile 65536
es hard nofile 65536

2 max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]

vim /etc/sysctl.conf

vm.max_map_count=655360

并执行命令生效: 

sysctl -p

IK 安装和使用

1 安装目录,执行命令

bin/elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v6.4.0/elasticsearch-analysis-ik-6.4.0.zip

2 重启es

3 config/analysis-ik目录下新建文件 word.dic 并且添加

出行

4 编辑 config/analysis-ik/IKAnalyzer.cfg.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
        <comment>IK Analyzer 扩展配置</comment>
        <!--用户可以在这里配置自己的扩展字典 -->
        <entry key="ext_dict">word.dic</entry>
         <!--用户可以在这里配置自己的扩展停止词字典-->
        <entry key="ext_stopwords"></entry>
        <!--用户可以在这里配置远程扩展字典 -->
         <entry key="remote_ext_dict">https://127.0.0.1/word</entry>
        <!--用户可以在这里配置远程扩展停止词字典-->
        <!-- <entry key="remote_ext_stopwords">words_location</entry> -->
</properties>

5 重启执行脚本验证分词效果

GET _analyze
{
  "analyzer": "ik_max_word",
  "text": "旅游出行"
}

ik_max_word:最细粒度拆分,即最大词数。

ik_smart:最粗粒度拆分,即最小词数。

同义词

1 config 目录新建synonyms.txt并且添加

番茄,西红柿 => 西红柿

新建索引时增加同义词

PUT test_index
{
  "settings": {
    "index": {
      "number_of_shards": "5",
      "analysis": {
        "filter": {
          "word_sync": {
            "type": "synonym",
            "synonyms_path": "synonyms.txt"
          }
        },
        "normalizer": {
          "my_normalizer": {
            "filter": [
              "lowercase",
              "asciifolding"
            ],
            "type": "custom"
          }
        },
        "analyzer": {
          "ik_sync_smart": {
            "filter": [
              "word_sync"
            ],
            "type": "custom",
            "tokenizer": "ik_smart"
          }
        }
      },
      "number_of_replicas": "1"
    }
  },
  "mappings": {
    "test": {
      "properties": {
        "dest": {
          "type": "text",
          "analyzer": "ik_sync_smart"
        },

验证同义词功能

GET test_index/_analyze
{
  "analyzer": "ik_sync_smart",
  "text": "西红柿"
}

{
  "tokens": [
    {
      "token": "西红柿",
      "start_offset": 0,
      "end_offset": 2,
      "type": "SYNONYM",
      "position": 0
    }
  ]
}

别名

1 新建索引时增加别名

PUT test_index
{
  "aliases": {
    "test_index_alias": {}
  },
  "settings": {
    "index": {
      "number_of_shards": "5",
      "analysis": {
        "analyzer": {
          "ik_sync_smart": {
            "type": "custom",
            "tokenizer": "ik_smart"
          }
        }
      },
      "number_of_replicas": "1"
    }
  },
  "mappings": {

2 使用脚本增加别名

POST /_aliases
{
    "actions": [
        { "remove": { "index": "test_index", "alias": "test_index_alias" }},
        { "add":    { "index": "test_index", "alias": "test_index_alias" }},
    ]
}

3 执行

PUT product_index_new/_alias/test_index_alias

4 查看所有别名

GET _cat/aliases?v

常用脚本

1 删除脚本

POST product_index_new/product/_delete_by_query?conflicts=proceed
{"query":{"bool":{"must":[{"range":{"addtime":{"lt":"2022-02-12 15:18:01"}}}]}}}