博客插上Elasticsearch的翅膀

docker安装环境

必须要有openjdk 查看java版本 java -version

安装Elasticsearch

docker pull docker.elastic.co/elasticsearch/elasticsearch:6.0.0

启动docker

docker run -d --name es -p 9200:9200 -p 9300:9300 -v /var/lib/elasticsearch/logs:/usr/share/elasticsearch/logs -v /var/lib/elasticsearch/data:/usr/share/elasticsearch/data -e "discovery.type=single-node" docker.elastic.co/elasticsearch/elasticsearch:6.0.0

安装ik分词插件

./bin/elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v6.0.0/elasticsearch-analysis-ik-6.0.0.zip
#也可以本地安装文件
./bin/elasticsearch-plugin install file:///usr/share/elasticsearch/elasticsearch-analysis-ik-6.0.0.zip

./bin/elasticsearch-plugin list

卸载插件

只保留ik即可

./bin/elasticsearch-plugin remove ingest-geoip
./bin/elasticsearch-plugin remove ingest-user-agent

设置全局变量

echo 'export PATH="/usr/local/opt/elasticsearch@5.6/bin:$PATH"' >> ~/.zshrc

检查是否启动

ps aux | grep java
curl -XGET 'localhost:9200/?pretty'

修改jvm.options的内存分配

默认是分配2G的内存,这里我们修改为512m

-Xms512m
-Xmx512m

修改配置打开端口

vi /etc/elasticsearch/elasticsearch.yml
// 签名的 ; 号去掉
:http.port: 9200

常用命令

我们通常用用_cat API检测集群是否健康。 确保9200端口号可用

绿色表示一切正常, 黄色表示所有的数据可用但是部分副本还没有分配,红色表示部分数据因为某些原因不可用

curl 'localhost:9200/_cat/health?v'
通过如下语句,我们可以获取集群的节点列表
curl 'localhost:9200/_cat/nodes?v'

通过如下语句,列出所有索引

curl 'localhost:9200/_cat/indices?v'

设置跨域

问题

启动不起来查看错误service elasticsearch status

➜  share service elasticsearch status
● elasticsearch.service - Elasticsearch
   Loaded: loaded (/usr/lib/systemd/system/elasticsearch.service; enabled; vendor preset: disabled)
   Active: failed (Result: exit-code) since Thu 2018-04-05 12:53:14 CST; 33s ago
     Docs: http://www.elastic.co
  Process: 3302 ExecStart=/usr/share/elasticsearch/bin/elasticsearch -p ${PID_DIR}/elasticsearch.pid --quiet -Edefault.path.logs=${LOG_DIR} -Edefault.path.data=${DATA_DIR} -Edefault.path.conf=${CONF_DIR} (code=exited, status=1/FAILURE)
  Process: 3300 ExecStartPre=/usr/share/elasticsearch/bin/elasticsearch-systemd-pre-exec (code=exited, status=0/SUCCESS)
 Main PID: 3302 (code=exited, status=1/FAILURE)

Apr 05 12:53:14 izm5e5n9efbv0mqjokcoesz systemd[1]: Starting Elasticsearch...
Apr 05 12:53:14 izm5e5n9efbv0mqjokcoesz systemd[1]: Started Elasticsearch.
Apr 05 12:53:14 izm5e5n9efbv0mqjokcoesz elasticsearch[3302]: OpenJDK 64-Bit Server VM warning: INFO: os::commit_memory(0x00000000ea660000, 362414080, 0) failed; error='Cann...(errno=12)
Apr 05 12:53:14 izm5e5n9efbv0mqjokcoesz systemd[1]: elasticsearch.service: main process exited, code=exited, status=1/FAILURE
Apr 05 12:53:14 izm5e5n9efbv0mqjokcoesz systemd[1]: Unit elasticsearch.service entered failed state.
Apr 05 12:53:14 izm5e5n9efbv0mqjokcoesz systemd[1]: elasticsearch.service failed.
Hint: Some lines were ellipsized, use -l to show in full.

我这里启动不起来是内存分配问题,我就设置虚拟内存了 启动之后,很卡..最后分析是因为我服务器上的gitlab服务占用内存快2.5G了,,gitlab真的很吃内存

laravel 框架抛出异常 No alive nodes found in your cluster

有可能是没有连接上es

可以打印出es的信息看下是否正确

dd(config('scout.elasticsearch'));

解决状态是yellow问题

查询到那些是有问题的

curl -s "http://127.0.0.1:9200/_cat/shards" | grep UNASSIGNED
.monitoring-es-6-2019.01.18 0 r UNASSIGNED

explain一下

curl -XGET 'http://127.0.0.1:9200/_cluster/allocation/explain?pretty'
{
  "index" : ".monitoring-es-6-2019.01.18",
  "shard" : 0,
  "primary" : false,
  "current_state" : "unassigned",
  "unassigned_info" : {
    "reason" : "CLUSTER_RECOVERED",
    "at" : "2019-01-18T16:04:45.905Z",
    "last_allocation_status" : "no_attempt"
  },
  "can_allocate" : "no",
  "allocate_explanation" : "cannot allocate because allocation is not permitted to any of the nodes",
  "node_allocation_decisions" : [
    {
      "node_id" : "3D0FdvBhTkClpGT6nTyk_Q",
      "node_name" : "master",
      "transport_address" : "172.17.0.5:9300",
      "node_attributes" : {
        "ml.max_open_jobs" : "10",
        "ml.enabled" : "true"
      },
      "node_decision" : "no",
      "deciders" : [
        {
          "decider" : "same_shard",
          "decision" : "NO",
          "explanation" : "the shard cannot be allocated to the same node on which a copy of the shard already exists [[.monitoring-es-6-2019.01.18][0], node[3D0FdvBhTkClpGT6nTyk_Q], [P], s[STARTED], a[id=ZUjc_D19QBCu1NtAy_NYSw]]"
        }
      ]
    }
  ]
}

不知道为什么 我直接粗暴的删掉这个index

curl -XDELETE 'http://127.0.0.1:9200/.monitoring-es-6-2019.01.18/'
{"acknowledged":true}

blocked by: [FORBIDDEN/12/index read-only / allow delete (api)];

就是这个索引只读

curl -X PUT \
  http://127.0.0.1:9200/people/_settings \
  -H 'Content-Type: application/json' \
  -H 'Postman-Token: 39feab6d-803e-476a-8e86-8acdd8afa5e7' \
  -H 'cache-control: no-cache' \
  -d '{
    "index": {
        "blocks": {
            "read_only_allow_delete": false
        }
    }
}'

docker 安装Elasticsearch-head

5.x以下 mobz/elasticsearch-head

#!/bin/bash
docker rm -f es-head
docker run --name es-head \
-d -p 9100:9100 \
mobz/elasticsearch-head:5

6.x以上 tobias74/elasticsearch-head

#!/bin/bash
docker rm -f es-head
docker run --name es-head \
-d -p 9100:9100 \
tobias74/elasticsearch-head:6

参考文章

Elasticsearch下一步学习计划

看一遍文档elasticsearch手册

如何使用tamayo/laravel-scout-elastic包结合搜索,我现在是用的原生

参考文献