使用Homebrew在macOS上构建Elasticsearch

环境

2020年10月2日
Elasticsearch7
Homebrew 2.5.2


请参照以下内容,在中文中进行本地化表述,只需要一种选择:

使用Homebrew在macOS上安装Elasticsearch | Elasticsearch参考【7.9】| Elastic

安装

要在Homebrew中安装,请首先添加ElasticHomebrew仓库。
添加了ElasticHomebrew仓库后,您可以使用brew install命令安装Elasticsearch的默认发行版。

brew tap elastic/tap

brew install elastic/tap/elasticsearch-full

设定

Elasticsearch有三个配置文件。

elasticsearch.yml for configuring Elasticsearch

jvm.options for configuring Elasticsearch JVM settings

log4j2.properties for configuring Elasticsearch logging

这些文件位于config目录中。
如果使用Homebrew进行安装,它们创建在/usr/local/etc/elasticsearch/目录中。

这里有配置文件的说明。

动作

创建一个文件夹以放置设置文件(可以在任何地方)。

前往创建的文件夹,并从默认的配置文件中复制最低限度所需的内容。

cp /usr/local/etc/elasticsearch/elasticsearch.yml .
cp /usr/local/etc/elasticsearch/jvm.options .
cp /usr/local/etc/elasticsearch/log4j2.properties .

elasticsearch.yml的意思是“elasticsearch配置文件”,指的是Elasticsearch的配置文件。

将elasticsearch.yml文件的内容按照以下方式修改。

※ 你的用户名可根据需要随时更改

cluster:
  name: mycluster
node:
  name: mynode
network:
  host: localhost
path:
  data: /Users/your user name/elastic_stack/data/
  logs: /Users/your user name/elastic_stack/log/

环境变量

请将上面创建的文件夹设置为环境变量ES_PATH_CONF的值。
※请根据实际情况修改您的用户名。

export ES_PATH_CONF=/Users/your user name/elastic_stack/elastic_local

展示环境变量

export -p

echo $ES_PATH_CONF

启动 Elasticsearch

用Elasticsearch命令启动

elasticsearch

验证 (què

curl http://localhost:9200/

结果

{
  "name" : "mynode",
  "cluster_name" : "mycluster",
  "cluster_uuid" : "2bxVVndhT3qYmc410DSmDA",
  "version" : {
    "number" : "7.9.2",
    "build_flavor" : "default",
    "build_type" : "tar",
    "build_hash" : "d34da0ea4a966c4e49417f2da2f244e3e97b4e6e",
    "build_date" : "2020-09-23T00:45:33.626720Z",
    "build_snapshot" : false,
    "lucene_version" : "8.6.2",
    "minimum_wire_compatibility_version" : "6.8.0",
    "minimum_index_compatibility_version" : "6.0.0-beta1"
  },
  "tagline" : "You Know, for Search"
}

kibana 可以用于数据可视化和分析。

安装

brew install kibana

启动Kibana

启动

kibana

...省略
==> kibana
Config: /usr/local/etc/kibana/
If you wish to preserve your plugins upon upgrade, make a copy of
/usr/local/opt/kibana/plugins before upgrading, and copy it into the
new keg location after upgrading.

To have launchd start kibana now and restart at login:
  brew services start kibana
Or, if you don't want/need a background service you can just run:
  kibana
スクリーンショット 2020-10-02 20.03.36.png

添加数据

创建索引

创建一个名为foo_index的索引。

curl --include -XPUT "http://localhost:9200/foo_index?pretty"

结果

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

确认结果

curl --include -XGET "http://localhost:9200/foo_index?pretty"

结果

{
  "foo_index" : {
    "aliases" : { },
    "mappings" : { },
    "settings" : {
      "index" : {
        "creation_date" : "1601883316655",
        "number_of_shards" : "1",
        "number_of_replicas" : "1",
        "uuid" : "KMD3KGVMSNSJEtUEJBT-xg",
        "version" : {
          "created" : "7090299"
        },
        "provided_name" : "foo_index"
      }
    }
  }
}

添加文件

curl --include -XPOST "http://localhost:9200/foo_index/_doc?pretty" \
-H 'Content-Type: application/json' \
-d '{
  "foo_user": "Alice",
  "foo_message": "The night was young, and so was he. But the night was sweet, and he was sour."
}'

结果

HTTP/1.1 201 Created
Location: /foo_index/_doc/i32x93QByjoFzGo__-wj
content-type: application/json; charset=UTF-8
content-length: 242

生成了一个ID,编号为i32x93QByjoFzGo__-wj。

确定结果

curl --include -XGET "http://localhost:9200/foo_index/_doc/i32x93QByjoFzGo__-wj?pretty"

结果

{
  "_index" : "foo_index",
  "_type" : "_doc",
  "_id" : "i32x93QByjoFzGo__-wj",
  "_version" : 1,
  "_seq_no" : 0,
  "_primary_term" : 1,
  "found" : true,
  "_source" : {
    "foo_user" : "Alice",
    "foo_message" : "The night was young, and so was he. But the night was sweet, and he was sour."
  }
}

追加第二个文件

curl --include -XPOST "http://localhost:9200/foo_index/_doc?pretty" \
-H 'Content-Type: application/json' \
-d '{
  "foo_user": "ボブ",
  "foo_message": "夜は若く、彼も若かった。が、夜の空気は甘いのに、彼の気分は苦かった。"
}'

结果

HTTP/1.1 201 Created
Location: /foo_index/_doc/jH2193QByjoFzGo_t-w7
content-type: application/json; charset=UTF-8
content-length: 242

确认结果

curl --include -XGET "http://localhost:9200/foo_index/_doc/jH2193QByjoFzGo_t-w7?pretty"

结果

{
  "_index" : "foo_index",
  "_type" : "_doc",
  "_id" : "jH2193QByjoFzGo_t-w7",
  "_version" : 1,
  "_seq_no" : 1,
  "_primary_term" : 1,
  "found" : true,
  "_source" : {
    "foo_user" : "ボブ",
    "foo_message" : "夜は若く、彼も若かった。が、夜の空気は甘いのに、彼の気分は苦かった。"
  }
}

全文检索数据

执行一个查询来搜索包含”夜的空气”的文档,查询的字段为foo_message。

curl --include -XGET "http://localhost:9200/foo_index/_search?pretty" \
-H 'Content-Type: application/json' \
-d '
{
  "query": {
    "match": {
      "foo_message": "夜の空気"
    }
  }
}'

结果

{
  "took" : 3,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 3.4337347,
    "hits" : [
      {
        "_index" : "foo_index",
        "_type" : "_doc",
        "_id" : "jH2193QByjoFzGo_t-w7",
        "_score" : 3.4337347,
        "_source" : {
          "foo_user" : "ボブ",
          "foo_message" : "夜は若く、彼も若かった。が、夜の空気は甘いのに、彼の気分は苦かった。"
        }
      }
    ]
  }
}

请参阅

在 macOS 上搭建 Elasticsearch 7,并进行全文搜索 – Qiita

配置Elasticsearch | Elasticsearch参考 [master] | Elastic

bannerAds