3、ElasticSearch单节点安装

ES安装


官方网站下载链接:https://www.elastic.co/cn/downloads/elasticsearch
1、配置jdk路径

在当前用户下配置环境变量
vi ~/.bashrc

JAVA_HOME=/root/jdk-12.0.1
CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
PATH=$JAVA_HOME/bin:$PATH

source ~/.bashrc
java -version


2、安装es
默认在本地启动
修改方法:
    1、修改config/elasticsearch.yml文件
        network.host: 192.168.81.131
    2、在启动es的时候指定参数
        ./bin/elasticsearch   -Dnetwork.host=192.168.81.131
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.5.2-linux-x86_64.tar.gz
tar zxvf elasticsearch-7.5.2-linux-x86_64.tar.gz
vim elasticsearch-7.5.2/config/elasticsearch.yml
# cluster.initial_master_nodes: ["node-1"]
# node.name: node-1
# network.host: 0.0.0.0
cd elasticsearch
-7.5.2

 

 
Java HotSpot(TM) 64-Bit Server VM warning: INFO: os::commit_memory(0x0000000085330000, 2060255232, 0) failed; error='Cannot allocate memory' (errno=12)
由于 elasticsearch5.0 默认分配 jvm 空间大小为2g,修改 jvm空间分配
# vim config/jvm.options  
-Xms2g  
-Xmx2g
修改为
-Xms512m
-Xmx512m
对于低内存的虚拟机来说,要使用 ES 搜索,修改这个配置时必须的。

删除不用的elasticsearch插件

/bin/elasticsearch-plugin list > /tmp/zgw_elasticsearch.log

cat  /tmp/zgw_elasticsearch.log | xargs -I {} /bin/elasticsearch-plugin remove {}
内存小
https://www.cnblogs.com/hellxz/p/11057234.html
其他错误
https://blog.csdn.net/roshy/article/details/101053647
Native controller process has stopped
健康检查
curl http://127.0.0.1:9200/_cat/health?v
查询索引
curl    127.0.0.1:9200:/_cat/indices?v
索引
curl    -X    PUT    127.0.0.1:9200/www
curl -X DELETE 127.0.0.1:9200/www

 

记录
curl -H "ContentType:application/json" -X POST 127.0.0.1:9200/user/person -d '
{
    "name": "dsb",
    "age": 9000,
    "married": true
}'


curl -H "ContentType:application/json" -X PUT 127.0.0.1:9200/user/person/4 -d '
{
    "name": "sb",
    "age": 9,
    "married": false
}'
检索
全检索:curl -X GET 127.0.0.1:9200/user/person/_search
按条件检索
curl -H "ContentType:application/json" -X PUT 127.0.0.1:9200/user/person/4 -d '
{
    "query":{
        "match": {"name": "sb"}
    }    
}'

 

package main

import (
    "context"
    "fmt"

    "github.com/olivere/elastic/v7"
)

// Elasticsearch demo

type Person struct {
    Name    string `json:"name"`
    Age     int    `json:"age"`
    Married bool   `json:"married"`
}

func main() {
    client, err := elastic.NewClient(elastic.SetURL("http://192.168.1.7:9200"))
    if err != nil {
        // Handle error
        panic(err)
    }

    fmt.Println("connect to es success")
    p1 := Person{Name: "rion", Age: 22, Married: false}
    put1, err := client.Index().
        Index("user").
        BodyJson(p1).
        Do(context.Background())
    if err != nil {
        // Handle error
        panic(err)
    }
    fmt.Printf("Indexed user %s to index %s, type %s\n", put1.Id, put1.Index, put1.Type)
}

 

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import requests
import json
from elasticsearch import Elasticsearch

data = json.dumps({'b': 123121})
header = {'Content-Type': 'application/json'}
host = '192.168.96.131'
port = '9200'
es = Elasticsearch(hosts=host, port=port)

# 添加或更新数据,index,doc_type名称可以自定义,id可以根据需求赋值,body为内容
# es.index(index="my_index",doc_type="test_type",id=1,body={"name":"python","addr":"深圳"})

# 或者:ignore=409忽略文档已存在异常
# es.create(index="my_index",doc_type="test_type",id=1,ignore=409,body={"name":"python","addr":"深圳"})


# es = Elasticsearch(hosts="192.168.96.130")
# # 9200
# result = es.search(index="logstash_test", doc_type="system")
#
# # 打印所有数据
# for item in result["hits"]["hits"]:
#     print(item["_source"])
#     print(item["_id"])
#     result = es.delete(index="logstash_test", doc_type="system", id=item["_id"])

# 删除id=1的数据
# result = es.delete(index="logstash_test", doc_type="doc")

# res = requests.get(f"http://{host}:{port}/_cat/indices?v")  # 查询所有索引
# res = requests.put(f"http://{host}:{port}/www")  # 创建索引
# res = requests.delete(f"http://{host}:{port}/www")  # 删除索引

# res = requests.post(f"http://{host}:{port}/www/course", data=data, headers=header)  # 创建数据,不指定Id->随机
# res = requests.put(f"http://{host}:{port}/www/course/1", data=data, headers=header)  # 更新数据
# res = requests.get(f"http://{host}:{port}/www/course/1?pretty=true")  # 查看记录
# res = requests.delete(f"http://{host}:{port}/www/course/1")  # 删除记录

# res = requests.get(f"http://{host}:{port}/_mapping?pretty=true")  # 查询索引下的type
# res = requests.get(f"http://{host}:{port}/www/course/_search")
res = requests.get(f"http://{host}:{port}/www/course/_search", data=json.dumps({
    "query": {"match": {"a": "1"}}
}), headers=header)  # 数据查询

# http://192.168.96.130:9200/_cat/health?v
print(json.dumps(json.loads(res.text), indent=4))

 

 

常见错误

[1]: max file descriptors [4096] for elasticsearch process is too low, increase to at least [65536]
    vim /etc/security/limits.conf
    添加以下内容
    *    soft    nofile    65536
    *    hard    nofile    131072
    *    soft    nproc    2048
    *    hard    nproc    4096
    
[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
[3]: max number of threads[1024] for users [es] likely too low, increase to at least [2018]
    vim /etc/security/limits.d/90-nproc.conf
    * soft nproc 4096

 

head插件安装

默认端口号9100

功能: 显示集群的拓扑,并且能够执行索引和节点级别操作 搜索接口能够查询集群中原始json或表格格式的检索数据 能够快速访问并显示集群的状态 有一个输入窗口,允许任意调用restful api
yum -y install epel-release
yum -y install nodejs  
npm install -g cnpm --registry=https://registry.npm.taobao.org
yum -y install git
git clone git://github.com/mobz/elasticsearch-head.git
cd elasticsearch-head/
cnpm install 
# npm config set strict-ssl false
1、修改配置文件Gruntfile.js
在connect - server - options 下
增加hostname:'*'

2、修改_site/app.js文件    4360行
修改head连接es的地址(修改localhost为本机的IP地址)

3、es配置
修改elasticsearch.yml,增加跨域的配置
vim config/elasticsearch.yml
http.cors.enabled: true
http.cors.allow-origin: '*'
启动
cd node_modules/grunt/bin/
./grunt server &
netstat -lntp

 

posted @ 2019-06-13 12:46  慕沁  阅读(1099)  评论(0)    收藏  举报