Elasticsearch的安装步骤和简单用法

简要摘要

以下是Elasticsearch的安装步骤和简单用法。

环境

    • CentOS 7.2

 

    • elasticsearch 5.6.3-1

 

    • openjdk version “1.8.0_144”

 

    • OpenJDK Runtime Environment (build 1.8.0_144-b01)

 

    OpenJDK 64-Bit Server VM (build 25.144-b01, mixed mode)

安装步骤

安装Java

请参考以下内容在CentOS 7上安装Java OpenJDK 8。

安装Elasticsearch

添加到仓库

$ sudo vi /etc/yum.repos.d/elasticsearch.repo

[elasticsearch-5.x]
name=Elasticsearch repository for 5.x packages
baseurl=https://artifacts.elastic.co/packages/5.x/yum
gpgcheck=1
gpgkey=https://artifacts.elastic.co/GPG-KEY-elasticsearch
enabled=1
autorefresh=1
type=rpm-md

安装

$ sudo yum -y install elasticsearch 

自启动设置

$ sudo systemctl start elasticsearch
$ sudo systemctl enable elasticsearch 

更改记忆设置

Elasticsearch的默认内存需求为2GB。
例如,如果您使用AWS EC2实例类型t2.micro,则只有1GB内存可用。
因此,您需要将内存使用量调整为更低的设置。
在这里,我们将其更改为512MB。
参考:https://qiita.com/tjinjin/items/7ea3ebd228748f9d5224

$ sudo vi /etc/elasticsearch/jvm.options

# Xms represents the initial size of total heap space
# Xmx represents the maximum size of total heap space

--Xms2g
--Xmx2g
+-Xms512m
+-Xmx512m

一旦更改设置,将重新启动elasticsearch。

$ sudo systemctl restart elasticsearch

请确认是否处于活动状态。

$ sudo systemctl status elasticsearch
  elasticsearch.service - Elasticsearch
   Loaded: loaded (/usr/lib/systemd/system/elasticsearch.service; enabled; vendor preset: disabled)
   Active: active (running) since Fri 2017-10-13 05:34:09 UTC; 6min ago

确保动作正确

$ curl http://127.0.0.1:9200
{
  "name" : "BEgoRLj",
  "cluster_name" : "elasticsearch",
  "cluster_uuid" : "mTb6VzKURV2jjjtqiEPhYg",
  "version" : {
    "number" : "5.6.3",
    "build_hash" : "1a2f265",
    "build_date" : "2017-10-06T20:33:39.012Z",
    "build_snapshot" : false,
    "lucene_version" : "6.6.1"
  },
  "tagline" : "You Know, for Search"
}

使得能够运用日语

进行安装分析-kuromoji。

$ sudo /usr/share/elasticsearch/bin/elasticsearch-plugin install analysis-kuromoji

确认安装

$ sudo /usr/share/elasticsearch/bin/elasticsearch-plugin list | grep analysis-kuromoji
analysis-kuromoji

简易使用法

创建索引

索引是指在关系型数据库(RDB)中的表。

$ curl -X PUT "http://127.0.0.1:9200/sample_index"

{"acknowledged":true,"shards_acknowledged":true,"index":"sample_index"}

确认目录列表

RDB: 显示所有表格;

$ curl http://127.0.0.1:9200/_aliases?pretty

{
  "sample_index" : {
    "aliases" : { }
  }
}

查看已创建的sample_index的详细信息

$ curl http://127.0.0.1:9200/sample_index/_settings?pretty

{
  "sample_index" : {
    "settings" : {
      "index" : {
        "creation_date" : "1507788470720",
        "number_of_shards" : "5",
        "number_of_replicas" : "1",
        "uuid" : "DQV3vNdWQVGOHA2xXDi9lQ",
        "version" : {
          "created" : "5060399"
        },
        "provided_name" : "sample_index"
      }
    }
  }
}

创建地图

映射是指索引的结构。
如果输入数据,则会自动创建映射,但也可以事先手动定义。

输入数据

我尝试输入数据。

RDB: 向sample_index表插入数据,字段包括标题(title),描述(description),数量(amount),值为(“样本1号”,”这是一条样本数据”,20)。

$ curl -X PUT "http://127.0.0.1:9200/sample_index/doc01/1" -d '{
    "title" : "Sample No.1",
    "description" : "This is a sample data",
    "amount" : 20
}'

{"_index":"sample_index","_type":"doc01","_id":"1","_version":1,"result":"created","_shards":{"total":2,"successful":1,"failed":0},"created":true}

确认地图映射

通过数据输入,确认地图创建完成。

RDB: 描述sample_index。

$ curl "http://127.0.0.1:9200/sample_index/_mapping/doc01?pretty"

{
  "sample_index" : {
    "mappings" : {
      "doc01" : {
        "properties" : {
          "amount" : {
            "type" : "long"
          },
          "description" : {
            "type" : "text",
            "fields" : {
              "keyword" : {
                "type" : "keyword",
                "ignore_above" : 256
              }
            }
          },
          "title" : {
            "type" : "text",
            "fields" : {
              "keyword" : {
                "type" : "keyword",
                "ignore_above" : 256
              }
            }
          }
        }
      }
    }
  }
}

确认数据

确认数据已经被输入。

RDB:从sample_index表中选择所有数据。

$ curl "http://127.0.0.1:9200/sample_index/doc01/1?pretty"

{
  "_index" : "sample_index",
  "_type" : "doc01",
  "_id" : "1",
  "_version" : 1,
  "found" : true,
  "_source" : {
    "title" : "Sample No.1",
    "description" : "This is a sample data",
    "amount" : 20
  }
}

搜索数据

我将尝试搜索已投入数据中的记录。我将搜索含有“sample”内容的description。

在选择`sample_index`表中,如查找到`description`字段中包含”sample”的所有记录。

$ curl "http://127.0.0.1:9200/sample_index/doc01/_search?q=description:sample&pretty=true"

{
  "took" : 16,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 1,
    "max_score" : 0.2824934,
    "hits" : [
      {
        "_index" : "sample_index",
        "_type" : "doc01",
        "_id" : "1",
        "_score" : 0.2824934,
        "_source" : {
          "title" : "Sample No.1",
          "description" : "This is a sample data",
          "amount" : 20
        }
      }
    ]
  }
}

请下列提供的内容进行本地化翻译成中文,只需要一种选择即可:

Reference.

    Elastic Stack : Elasticsearch インストール
广告
将在 10 秒后关闭
bannerAds