https://es.xiaoleilu.com/010_Intro/10_Installing_ES.html
安装
https://www.elastic.co/cn/downloads/
在上面ES官网下载ES7.5和kibana6.5版本。后者是可视化操作软件。同时下载页面也有配置启动的方法,很简单,es基本直接启动,kibana只需要改下elasticsearch.hosts即可。
一般首先在本地安装,之后打开http://localhost:5601/app/kibana
可以发现新版的(7.5)可以直接在界面上操作安装一些插件。
以下下的操作都在es7.5版本。改版本已经强制单索引单类型。类型推荐使用_doc,当然也可以指定。但只能有一个。
概念
索引:index。可以对应于m数据库中数据库。
类型:type。可以对应与数据库中的表。但又有不同,参考Es
中type理解。type 字段会和文档的 _id 一起生成一个 _uid 字段,因此在同一个索引下的不同类型的文档的 _id 可以具有相同的值。参考:es中索引与类型前世今生
索引:倒排索引。
创建索引
新版的es已经不要求使用type,而是直接操作index,用_doc统一表示。因为type也只是一个逻辑概念。
1 | PUT /megacorp/_doc/1 |
2 | { |
3 | "first_name" : "John", |
4 | "last_name" : "Smith", |
5 | "age" : 25, |
6 | "about" : "I love to go rock climbing", |
7 | "interests": [ "sports", "music" ] |
8 | } |
上面的操作可以自动创建索引megacorp,并且添加了一个文档。可以多次执行,后面的执行将会update该文档。返回如下:
1 | { |
2 | "_index" : "megacorp", |
3 | "_type" : "_doc", |
4 | "_id" : "1", |
5 | "_version" : 2, |
6 | "result" : "updated", |
7 | "_shards" : { |
8 | "total" : 2, |
9 | "successful" : 1, |
10 | "failed" : 0 |
11 | }, |
12 | "_seq_no" : 1, |
13 | "_primary_term" : 1 |
14 | } |
检索
1 | GET /megacorp/_doc/1 |
返回:
1 | { |
2 | "_index" : "megacorp", |
3 | "_type" : "_doc", |
4 | "_id" : "1", |
5 | "_version" : 2, |
6 | "_seq_no" : 1, |
7 | "_primary_term" : 1, |
8 | "found" : true, |
9 | "_source" : { |
10 | "first_name" : "John", |
11 | "last_name" : "Smith", |
12 | "age" : 25, |
13 | "about" : "I love to go rock climbing", |
14 | "interests" : [ |
15 | "sports", |
16 | "music" |
17 | ] |
18 | } |
19 | } |
简单搜索
1 | GET /megacorp/_search |
返回结果:
1 | { |
2 | "took" : 1, |
3 | "timed_out" : false, |
4 | "_shards" : { |
5 | "total" : 1, |
6 | "successful" : 1, |
7 | "skipped" : 0, |
8 | "failed" : 0 |
9 | }, |
10 | "hits" : { |
11 | "total" : { |
12 | "value" : 2, |
13 | "relation" : "eq" |
14 | }, |
15 | "max_score" : 1.0, |
16 | "hits" : [ |
17 | { |
18 | "_index" : "megacorp", |
19 | "_type" : "employee", |
20 | "_id" : "1", |
21 | "_score" : 1.0, |
22 | "_source" : { |
23 | "first_name" : "John", |
24 | "last_name" : "Smith", |
25 | "age" : 25, |
26 | "about" : "I love to go rock climbing", |
27 | "interests" : [ |
28 | "sports", |
29 | "music" |
30 | ] |
31 | } |
32 | }, |
33 | { |
34 | "_index" : "megacorp", |
35 | "_type" : "employee", |
36 | "_id" : "2", |
37 | "_score" : 1.0, |
38 | "_source" : { |
39 | "first_name" : "John", |
40 | "last_name" : "jack", |
41 | "age" : 27, |
42 | "about" : "I love to go swimming", |
43 | "interests" : [ |
44 | "sports", |
45 | "movie" |
46 | ] |
47 | } |
48 | } |
49 | ] |
50 | } |
51 | } |
搜索last name包含smith的员工:
1 | GET /megacorp/_search?q=last_name:Smith |
将会返回id为1的文档。
使用DSL查询
DSL(Domain Specific Language特定领域语言)以JSON请求体的形式出现。
之前的查询last name为smith的员工可以这样查询:
1 | GET /megacorp/_search |
2 | { |
3 | "query" : { |
4 | "match" : { |
5 | "last_name" : "Smith" |
6 | } |
7 | } |
8 | } |
将得到和之前一样的结果。
更复杂的查询
让搜索稍微再变的复杂一些。我们依旧想要找到姓氏为“Smith”的员工,但是我们只想得到年龄大于30岁的员工。我们的语句将添加过滤器(filter),它使得我们高效率的执行一个结构化搜索:
1 | GET /megacorp/_search |
2 | { |
3 | "query" : { |
4 | "filtered" : { |
5 | "filter" : { |
6 | "range" : { |
7 | "age" : { "gt" : 30 } |
8 | } |
9 | }, |
10 | "query" : { |
11 | "match" : { |
12 | "last_name" : "smith" |
13 | } |
14 | } |
15 | } |
16 | } |
17 | } |
到目前为止,上面的查询将会返回错误:
1 | { |
2 | "error": { |
3 | "root_cause": [ |
4 | { |
5 | "type": "parsing_exception", |
6 | "reason": "no [query] registered for [filtered]", |
7 | "line": 3, |
8 | "col": 22 |
9 | } |
10 | ], |
11 | "type": "parsing_exception", |
12 | "reason": "no [query] registered for [filtered]", |
13 | "line": 3, |
14 | "col": 22 |
15 | }, |
16 | "status": 400 |
17 | } |
原因之后将会讲述。
全文索引
搜索所有喜欢“rock climbing”的员工:
1 | GET /megacorp/_search |
2 | { |
3 | "query" : { |
4 | "match" : { |
5 | "about" : "rock climbing" |
6 | } |
7 | } |
8 | } |
将得到id为1的文档。
短语搜索
目前我们可以在字段中搜索单独的一个词,这挺好的,但是有时候你想要确切的匹配若干个单词或者短语(phrases)。例如我们想要查询同时包含”rock”和”climbing”(并且是相邻的)的员工记录。
要做到这个,我们只要将match查询变更为match_phrase查询即可:
1 | GET /megacorp/_search |
2 | { |
3 | "query" : { |
4 | "match_phrase" : { |
5 | "about" : "rock climbing" |
6 | } |
7 | } |
8 | } |
高亮搜索
在Elasticsearch中高亮片段是非常容易的。让我们在之前的语句上增加highlight参数:
1 | GET /megacorp/_search |
2 | { |
3 | "query" : { |
4 | "match_phrase" : { |
5 | "about" : "rock climbing" |
6 | } |
7 | }, |
8 | "highlight": { |
9 | "fields" : { |
10 | "about" : {} |
11 | } |
12 | } |
13 | } |
得到的结果中将会增加一个字段highlight:
1 | { |
2 | "took" : 195, |
3 | "timed_out" : false, |
4 | "_shards" : { |
5 | "total" : 1, |
6 | "successful" : 1, |
7 | "skipped" : 0, |
8 | "failed" : 0 |
9 | }, |
10 | "hits" : { |
11 | "total" : { |
12 | "value" : 1, |
13 | "relation" : "eq" |
14 | }, |
15 | "max_score" : 1.3365866, |
16 | "hits" : [ |
17 | { |
18 | "_index" : "megacorp", |
19 | "_type" : "employee", |
20 | "_id" : "1", |
21 | "_score" : 1.3365866, |
22 | "_source" : { |
23 | "first_name" : "John", |
24 | "last_name" : "Smith", |
25 | "age" : 25, |
26 | "about" : "I love to go rock climbing", |
27 | "interests" : [ |
28 | "sports", |
29 | "music" |
30 | ] |
31 | }, |
32 | "highlight" : { |
33 | "about" : [ |
34 | "I love to go <em>rock</em> <em>climbing</em>" |
35 | ] |
36 | } |
37 | } |
38 | ] |
39 | } |
40 | } |
分析
最后,我们还有一个需求需要完成:允许管理者在职员目录中进行一些分析。 Elasticsearch有一个功能叫做聚合(aggregations),它允许你在数据上生成复杂的分析统计。它很像SQL中的GROUP BY但是功能更强大。
这段内容将放到后面来说。