elasticsearch - wtdig/study GitHub Wiki

elasticsearch安装

一、软件安装

1、环境要求

jdk至少需要在1.8.0_73以上版本。

linux的内核版本需要在2.6以上(我用的是centos7.x)

2、下载软件

cd /usr/local

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

tar -zxvf elasticsearch-5.6.7.tar.gz

mv elasticsearch-5.6.7 elasticsearch

3、在elasticsearch/config目录下修改jvm.options参数

-Xms512m

-Xmx512m

4、修改elasticsearch.yml文件 Memory\Network

# ----------------------------------- Memory -----------------------------------
#
# Lock the memory on startup:
#
#bootstrap.memory_lock: true
#
# Make sure that the heap size is set to about half the memory available
# on the system and that the owner of the process is allowed to use this
# limit.
#
# Elasticsearch performs poorly when the system is swapping the memory.
bootstrap.memory_lock: false
bootstrap.system_call_filter: false
#
# ---------------------------------- Network -----------------------------------
#
# Set the bind address to a specific IP (IPv4 or IPv6):
#
network.host: 0.0.0.0
#
# Set a custom port for HTTP:
#
#http.port: 9200
#
# For more information, consult the network module documentation.

退出保存后执行如下命令:

sysctl -p

5、修改vi /etc/security/limits.conf文件

# testes为登录服务器的用户名
testes soft nofile 65536
testes hard nofile 65536
testes soft nproc  4096
testes hard nproc  4096

6、不允许root用户登陆,新建一个非系统用户

useradd wtdig

7、错误提示:ERROR: [1] bootstrap checks failed                   [1]: max number of threads [1024] for user [elasticsearch] is too low, increase to at least [2048]

vi /etc/security/limits.d/90-nproc.conf 在root用户下执行

将1024修改为2048

8、启动 ./bin/elasticsearch

访问网址 http://45.78.9.159:9200/

9、安装中文分词器,要和版本对应上 https://github.com/medcl/elasticsearch-analysis-ik/releases?after=v5.6.8 中文分词器地址

10、平常登陆使用:

登陆系统, su - wtdig 使用wtdig用户

二、常用操作

参考资料

三、java客户端使用

1、pom文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.wt</groupId>
    <artifactId>elastcsearch</artifactId>
    <version>1.0-SNAPSHOT</version>
    <!-- https://mvnrepository.com/artifact/org.elasticsearch.client/transport -->
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.9</version>
        </dependency>
        <dependency>
            <groupId>jdk.tools</groupId>
            <artifactId>jdk.tools</artifactId>
            <version>1.8</version>
            <scope>system</scope>
            <systemPath>C:/Program Files/Java/jdk1.8.0_111/lib/tools.jar</systemPath>
        </dependency>

        <dependency>
            <groupId>org.elasticsearch</groupId>
            <artifactId>elasticsearch</artifactId>
            <version>5.6.7</version>
        </dependency>
        <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>transport</artifactId>
            <version>5.6.7</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-api</artifactId>
            <version>2.7</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.7</version>
        </dependency>
    </dependencies>
</project>

2、日志

resource下的log4j.properties

appender.console.type = Console
appender.console.name = console
appender.console.layout.type = PatternLayout

rootLogger.level = info
rootLogger.appenderRef.console.ref = console

resource下的log4j2.xml

<?xml version="1.0" encoding="UTF-8"?>

<Configuration status="WARN">

    <Appenders>

        <Console name="Console" target="SYSTEM_OUT">

            <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>

        </Console>

    </Appenders>

    <Loggers>

        <Root level="error">

            <AppenderRef ref="Console"/>

        </Root>

    </Loggers>

</Configuration>

3、测试代码

import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.transport.client.PreBuiltTransportClient;

import java.net.InetAddress;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

public class testClient {
    public static void main(String[] args) {
        try {

            //设置集群名称

            Settings settings = Settings.builder().put("cluster.name", "elasticsearch").build();
            //创建client
            @SuppressWarnings("resource")
            TransportClient client = new PreBuiltTransportClient(settings)
                    .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("45.78.9.159"), 9300));
            //写入数据
            // createDate(client);
            //搜索数据
            GetResponse response = client.prepareGet("accounts", "person", "1").execute().actionGet();
            //输出结果
            System.out.println(response.getSource());

            //关闭client
            client.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 插入单条数据
     *
     * @param client
     */
    public static void createDate(Client client) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("title", "红楼梦");
        map.put("content", "重生之我是贾宝玉");
        map.put("newcontent", new String[]{"first", "我是贾宝玉"});
        map.put("about", "i'd like to play");
        try {
            IndexResponse response = client.prepareIndex("blog", "article", UUID.randomUUID().toString())
                    .setSource(map).execute().actionGet();
            System.out.println("写入数据结果=" + response.status().getStatus() + "!id=" + response.getId());
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
    }

}
⚠️ **GitHub.com Fallback** ⚠️