先跑起来再说

What

Elasticsearch是一个基于Apache Lucene(TM)的开源搜索引擎。

Why

Elasticsearch使用Java开发并使用Lucene作为其核心来实现所有索引和搜索的功能,但是它的目的是通过简单的RESTful API来隐藏Lucene的复杂性,从而让全文搜索变得简单。

Elasticsearch不仅仅是Lucene和全文搜索,还支持:

  • 分布式的实时文件存储,每个字段都被索引并可被搜索
  • 分布式的实时分析搜索引擎
  • 可以扩展到上百台服务器,处理PB级结构化或非结构化数据

Elasticsearch在Apache 2 license下许可使用,可以免费下载、使用和修改。

定制高级特性,都一切都是可以灵活配置的。

安装

安装包

Mac安装Elasticsearch

homebrew 方式安装:

  • To install with Homebrew, you first need to tap the Elastic Homebrew repository:
1
brew tap elastic/tap
  • Once you’ve tapped the Elastic Homebrew repo, you can use brew install to install the default distribution of Elasticsearch:
1
brew install elastic/tap/elasticsearch-full
  • 有可能会报错:
1
2
3
> Install the Command Line Tools:
> xcode-select --install
>

那就执行对应命令就可以啦:

1
2
> xcode-select --installs
>
  • 还有可能会报错
1
2
3
> future versions of Elasticsearch will require Java 11; your Java version from [/Library/Java/JavaVirtualMachines/jdk-9.0.1.jdk/Contents/Home] does not meet this requirement
> Exception in thread "main" java.lang.RuntimeException: starting java failed with [1]
>

那就安装高版本的JDK咯:

1
2
> brew cask install java
>

路径说明

以下为brew安装的对应路径,如果直接下载的安装包,则进入根路径下查看所有目录

home:Elasticsearch 根目录 -> $ES_HOME

/usr/local/var/homebrew/linked/elasticsearch-full

bin:启动一个节点或安装插件的二进制脚本

/usr/local/var/homebrew/linked/elasticsearch-full/bin

config:配置文件(elasticsearch.yml)

/usr/local/etc/elasticsearch

data:每一个索引的数据文件/在节点上分配的分片。可以容纳多个位置

/usr/local/var/lib/elasticsearch

logs:日志文件

/usr/local/var/log/elasticsearch

plugin:插件目录。每一个插件都有一个子目录。

/usr/local/var/homebrew/linked/elasticsearch/plugins

运行

  • 如果用homebrew方式安装,直接输入elasticsearch即可启动。
  • 安装包方式: bin/elasticsearch

启动成功后,访问http://localhost:9200,会输出:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
{
"name": "username",
"cluster_name": "elasticsearch_username",
"cluster_uuid": "dca-l0K_QF2xP_JHi5iqtA",
"version": {
"number": "7.2.0",
"build_flavor": "default",
"build_type": "tar",
"build_hash": "508c38a",
"build_date": "2019-06-20T15:54:18.811730Z",
"build_snapshot": false,
"lucene_version": "8.0.0",
"minimum_wire_compatibility_version": "6.8.0",
"minimum_index_compatibility_version": "6.0.0-beta1"
},
"tagline": "You Know, for Search"
}

说明Elasticsearch集群已经启动并且正常运行,接下来就可以进行各种试验了。

插件

查看插件列表

elasticsearch-plugin list

安装插件

elasticsearch-plugin install pluginName

移除插件

elasticsearch-plugin remove pluginName

示例

  • 安装分词插件:bin/elasticsearch-plugin install analysis-icu
  • 运行:bin/elasticsearch
  • 查看:http://localhost:9200/_cat/plugins
  • 输出:username analysis-icu 7.2.0

集群运行

  • 执行命令
1
bin/elasticsearch -E node.name=节点名称 -E cluster.name=集群名称 -E path.data=节点对应数据 -d
  • 查看节点
1
http://localhost:9200/_cat/nodes?v

参考资料

Elasticsearch and JVM

Directory layout for Homebrew installs