1. 百科

ElasticSearch是一个基于Lucene的搜索服务器。它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口。Elasticsearch是用Java开发的,并作为Apache许可条款下的开放源码发布,是当前流行的企业级搜索引擎。设计用于云计算中,能够达到实时搜索,稳定,可靠,快速,安装使用方便。

2. 安装

依赖Java8,本文在Linux上运行

下载、解压

$ wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-5.5.1.zip
$ unzip elasticsearch-5.5.1.zip
$ cd elasticsearch-5.5.1/

启动

curl localhost:9200

返回

$curl localhost:9200
{
"name" : "Jt1bemT",
"cluster_name" : "elasticsearch",
"cluster_uuid" : "I_R2FCz8SUypT80rkbLeKw",
"version" : {
"number" : "5.5.1",
"build_hash" : "19c13d0",
"build_date" : "2017-07-18T20:44:24.823Z",
"build_snapshot" : false,
"lucene_version" : "6.6.0"
},
"tagline" : "You Know, for Search"
}

 

默认是只能本地调用,设置远程访问:

修改config/elasticsearch.yml

network.host: 0.0.0.0

重启,还是访问不了,报错

ERROR: [1] bootstrap checks failed
[1]: system call filters failed to install; check the logs and fix your configuration or disable system call filters at your own risk

原因:这是在因为Centos6不支持SecComp,而ES5.2.0默认bootstrap.system_call_filter为true进行检测,所以导致检测失败,失败后直接导致ES不能启动

修改

在elasticsearch.yml中配置bootstrap.system_call_filter为false

bootstrap.memory_lock: false
bootstrap.system_call_filter: false

启动,就可以通过:ip:port访问了

3. 基本概念

Node与Cluster

Elastic本质是一个分布式数据库,允许多台服务器协同工作,每台机器可以运行多个Elastic实例。

单个Elastic实例称为一个节点(Node),一组节点构成一个集群(Cluster)

Index

Elastic会索引所有的字段,经处理后写入一个反向索引。查找数据时,直接查找该索引。

所以Elastic数据管理的顶层单位交Index,它是单个数据库的同义词(注:每个索引必须是小写)

查看当前节点所有的Index

curl -X GET 'http://localhost:9200/_cat/indices?v'

Document

Index里面的记录称为Document,许多Document构成一个Index

Document采用的设计Json格式

同一个Index里的Document不要求有相同的结构,但是最好一样,这样利于提高搜索效率。

Type

Document分组,比如地体有1号线、2号线。不同的Type里应有相同的结构

可以通过以下命令查看一个Index中所有的Type

curl 'localhost:9200/_mapping?pretty=true'

存储结构和关系数据库对比

关系数据库       ⇒ 数据库        ⇒ 表          ⇒ 行             ⇒ 列(Columns)

Elasticsearch ⇒ 索引(Index) ⇒ 类型(type) ⇒ 文档(Docments) ⇒ 字段(Fields)

4. 增加和删除Index

curl -X PUT 'localhost:9200/subway'

新建了一个叫subway的Index,返回

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

删除叫subway的Index

curl -X DELETE 'localhost:9200/subway'

5. 中文分词设置

安装中文分词插件,如ik

$ ./bin/elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v5.5.1/elasticsearch-analysis-ik-5.5.1.zip

重启Elastic,就可以加载安装的ik了。

下面新建一个Index,指定需要分词的字段。

$ curl -X PUT 'localhost:9200/accounts' -d '
{
"mappings": {
"person": {
"properties": {
"user": {
"type": "text",
"analyzer": "ik_max_word",
"search_analyzer": "ik_max_word"
},
"title": {
"type": "text",
"analyzer": "ik_max_word",
"search_analyzer": "ik_max_word"
},
"desc": {
"type": "text",
"analyzer": "ik_max_word",
"search_analyzer": "ik_max_word"
}
}
}
}
}'

上述代码新建了名叫accounts的Index,名叫person的Index,person里有3个字段

  • user
  • title
  • desc

这3个都是中文,并且都是文本(text),所以需要指定中文分词,而不使用英文分词器

search_analyzer指定:ik_max_word,指明实用ik对文本进行最大数量的分词

6. 增删改

新增3条记录

curl -X PUT 'http://10.125.15.70:9200/accounts/person/zs' -d '{"user":"张三","title":"数据库工程师","desc":"数据库管理"}'
curl -X PUT 'http://10.125.15.70:9200/accounts/person/ls' -d '{"user":"李四","title":"运维工程师","desc":"系统维护"}'
curl -X PUT 'http://10.125.15.70:9200/accounts/person/ww' -d '{"user":"王五","title":"开发工程师","desc":"软件开发"}'

curl -X DELETE 'http://10.125.15.70:9200/accounts/person/zs'

curl -X PUT 'localhost:9200/accounts/person/ls' -d '{"user" : "李四2","title" : "工程师","desc" : "软件开发"}'

7. 查

返回所有记录

curl 'localhost:9200/accounts/person/_search'

结果

{
"took" : 3,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"failed" : 0
},
"hits" : {
"total" : 2,
"max_score" : 1.0,
"hits" : [
{
"_index" : "accounts",
"_type" : "person",
"_id" : "ls",
"_score" : 1.0,
"_source" : {
"user" : "李四2",
"title" : "工程师",
"desc" : "软件开发"
}
},
{
"_index" : "accounts",
"_type" : "person",
"_id" : "ww",
"_score" : 1.0,
"_source" : {
"user" : "王五",
"title" : "开发工程师",
"desc" : "软件开发"
}
}
]
}
}

全文搜索

curl 'localhost:9200/accounts/person/_search' -d '{"query":{"match":{"title":"工程"}}}

结果

{
"took": 21,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 0.5649868,
"hits": [{
"_index": "accounts",
"_type": "person",
"_id": "ww",
"_score": 0.5649868,
"_source": {
"user": "王五",
"title": "开发工程师",
"desc": "软件开发"
}
}, {
"_index": "accounts",
"_type": "person",
"_id": "ls",
"_score": 0.5063205,
"_source": {
"user": "李四2",
"title": "工程师",
"desc": "软件开发"
}
}]
}
}

逻辑运算

OR

curl 'localhost:9200/accounts/person/_search'  -d '{"query" : { "match":{ "desc":"系统 开发" }}}'

AND

curl 'localhost:9200/accounts/person/_search'  -d '"query": {"bool": {"must": [{ "match": { "desc": "软件" } }, { "match": { "desc": "开发" } }]}}}'

 

参考

​Elasticsearch5.2.0部署过程的坑​

​全文搜索引擎 Elasticsearch 入门教程​