2017- 全文搜索引擎Elasticsearch 入门教程
全文搜索引擎Elasticsearch 入门教程
全文搜索属于最常见的需求,开源的 Elasticsearch (以下简称
它可以快速地储存、搜索和分析海量数据。维基百科、Stack Overflow、

本文从零开始,讲解如何使用
一、安装
JAVA_HOME
正确设置。
安装完
$ wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-5.5.1.zip $ unzip elasticsearch-5.5.1.zip $ cd elasticsearch-5.5.1/
接着,进入解压后的目录,运行下面的命令,启动
$ ./bin/elasticsearch
如果这时报错“max virtual memory areas vm.maxmapcount [65530] is too low”,要运行下面的命令。
$ sudo sysctl -w vm.max_map_count=262144
如果一切正常,
$ curl localhost:9200 { "name" : "atntrTf", "cluster_name" : "elasticsearch", "cluster_uuid" : "tf9250XhQ6ee4h7YI11anA", "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
,然后重新启动
network.host: 0.0.0.0
上面代码中,设成0.0.0.0
让任何人都可以访问。线上服务不要这样设置,要设成具体的
二、基本概念
2.1 Node 与Cluster
单个
2.2 Index
所以,
下面的命令可以查看当前节点的所有
$ curl -X GET 'http://localhost:9200/_cat/indices?v'
2.3 Document
{ "user": "张三", "title": "工程师", "desc": "数据库管理" }
同一个
2.4 Type
weather
这个
不同的id
字段不能在这个组是字符串,在另一个组是数值。这是与关系型数据库的表的一个区别。性质完全不同的数据(比如products
和logs
)应该存成两个
下面的命令可以列出每个
$ curl 'localhost:9200/_mapping?pretty=true'
根据规划,
三、新建和删除Index
新建weather
的
$ curl -X PUT 'localhost:9200/weather'
服务器返回一个acknowledged
字段表示操作成功。
{ "acknowledged":true, "shards_acknowledged":true }
然后,我们发出
$ curl -X DELETE 'localhost:9200/weather'
四、中文分词设置
首先,安装中文分词插件。这里使用的是 ik,也可以考虑其他插件(比如 smartcn
$ ./bin/elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v5.5.1/elasticsearch-analysis-ik-5.5.1.zip
上面代码安装的是
接着,重新启动
然后,新建一个
$ 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
的person
的person
有三个字段。
- user
- title
- desc
这三个字段都是中文,而且类型都是文本(text
"user": { "type": "text", "analyzer": "ik_max_word", "search_analyzer": "ik_max_word" }
上面代码中,analyzer
是字段文本的分词器,search_analyzer
是搜索词的分词器。ik_max_word
分词器是插件ik
提供的,可以对文本进行最大数量的分词。
五、数据操作
5.1 新增记录
向指定的/accounts/person
发送请求,就可以新增一条人员记录。
$ curl -X PUT 'localhost:9200/accounts/person/1' -d ' { "user": "张三", "title": "工程师", "desc": "数据库管理" }'
服务器返回的
{ "_index":"accounts", "_type":"person", "_id":"1", "_version":1, "result":"created", "_shards":{"total":2,"successful":1,"failed":0}, "created":true }
如果你仔细看,会发现请求路径是/accounts/person/1
,最后的1
是该条记录的abc
)都可以。
新增记录的时候,也可以不指定
$ curl -X POST 'localhost:9200/accounts/person' -d ' { "user": "李四", "title": "工程师", "desc": "系统管理" }'
上面代码中,向/accounts/person
发出一个_id
字段就是一个随机字符串。
{ "_index":"accounts", "_type":"person", "_id":"AV3qGfrC6jMbsbXb6k1p", "_version":1, "result":"created", "_shards":{"total":2,"successful":1,"failed":0}, "created":true }
注意,如果没有先创建accounts
5.2 查看记录
向/Index/Type/Id
发出
$ curl 'localhost:9200/accounts/person/1?pretty=true'
上面代码请求查看/accounts/person/1
这条记录,pretty=true
表示以易读的格式返回。
返回的数据中,found
字段表示查询成功,_source
字段返回原始记录。
{ "_index" : "accounts", "_type" : "person", "_id" : "1", "_version" : 1, "found" : true, "_source" : { "user" : "张三", "title" : "工程师", "desc" : "数据库管理" } }
如果found
字段就是false
。
$ curl 'localhost:9200/weather/beijing/abc?pretty=true' { "_index" : "accounts", "_type" : "person", "_id" : "abc", "found" : false }
5.3 删除记录
删除记录就是发出
$ curl -X DELETE 'localhost:9200/accounts/person/1'
这里先不要删除这条记录,后面还要用到。
5.4 更新记录
更新记录就是使用
$ curl -X PUT 'localhost:9200/accounts/person/1' -d ' { "user" : "张三", "title" : "工程师", "desc" : "数据库管理,软件开发" }' { "_index":"accounts", "_type":"person", "_id":"1", "_version":2, "result":"updated", "_shards":{"total":2,"successful":1,"failed":0}, "created":false }
上面代码中,我们将原始数据从
"_version" : 2, "result" : "updated", "created" : false
可以看到,记录的1
变成2
,操作类型(result)从created
变成updated
,created
字段变成false
,因为这次不是新建记录。
六、数据查询
6.1 返回所有记录
使用/Index/Type/_search
,就会返回所有记录。
$ curl 'localhost:9200/accounts/person/_search' { "took":2, "timed_out":false, "_shards":{"total":5,"successful":5,"failed":0}, "hits":{ "total":2, "max_score":1.0, "hits":[ { "_index":"accounts", "_type":"person", "_id":"AV3qGfrC6jMbsbXb6k1p", "_score":1.0, "_source": { "user": "李四", "title": "工程师", "desc": "系统管理" } }, { "_index":"accounts", "_type":"person", "_id":"1", "_score":1.0, "_source": { "user" : "张三", "title" : "工程师", "desc" : "数据库管理,软件开发" } } ] } }
上面代码中,返回结果的 took
字段表示该操作的耗时(单位为毫秒timed_out
字段表示是否超时,hits
字段表示命中的记录,里面子字段的含义如下。
total
:返回记录数,本例是2 条。max_score
:最高的匹配程度,本例是1.0
。hits
:返回的记录组成的数组。
返回的记录中,每条记录都有一个_score
字段,表示匹配的程序,默认是按照这个字段降序排列。
6.2 全文搜索
$ curl 'localhost:9200/accounts/person/_search' -d ' { "query" : { "match" : { "desc" : "软件" }} }'
上面代码使用 desc
字段里面包含
{ "took":3, "timed_out":false, "_shards":{"total":5,"successful":5,"failed":0}, "hits":{ "total":1, "max_score":0.28582606, "hits":[ { "_index":"accounts", "_type":"person", "_id":"1", "_score":0.28582606, "_source": { "user" : "张三", "title" : "工程师", "desc" : "数据库管理,软件开发" } } ] } }
size
字段改变这个设置。
$ curl 'localhost:9200/accounts/person/_search' -d ' { "query" : { "match" : { "desc" : "管理" }}, "size": 1 }'
上面代码指定,每次只返回一条结果。
还可以通过from
字段,指定位移。
$ curl 'localhost:9200/accounts/person/_search' -d ' { "query" : { "match" : { "desc" : "管理" }}, "from": 1, "size": 1 }'
上面代码指定,从位置
6.3 逻辑运算
如果有多个搜索关键字, or
关系。
$ curl 'localhost:9200/accounts/person/_search' -d ' { "query" : { "match" : { "desc" : "软件 系统" }} }'
上面代码搜索的是软件 or 系统
。
如果要执行多个关键词的and
搜索,必须使用布尔查询。
$ curl 'localhost:9200/accounts/person/_search' -d ' { "query": { "bool": { "must": [ { "match": { "desc": "软件" } }, { "match": { "desc": "系统" } } ] } } }'