项目驱动下学习了花了几天时间学习了ES,官方文档:
https://elasticsearch.cn/book/elasticsearch_definitive_guide_2.x/intro.html
-
安装运行
从 elastic 的官网 elastic.co/downloads/elasticsearch 获取最新版本的 Elasticsearch,当你解压文件之后,Elasticsearch 已经准备好运行了。
cd elasticsearch-<version>
./bin/elasticsearch
运行好后,另开一个终端,
curl 'http://localhost:9200/?pretty'
即可返回 name 版本等信息。
还可以用http请求,
curl -H "Content-Type: application/json" -XGET 'http://localhost:9200/_count?pretty' -d '
{
"query": {
"match_all": {}
}
}
'
具体参数说明可以查看
https://elasticsearch.cn/book/elasticsearch_definitive_guide_2.x/_talking_to_elasticsearch.html
为表达简单,以上http请求下文中简写为:
GET /_count
{
"query": {
"match_all": {}
}
}
-
面向文档
于传统数据库的关系型存储不同,Elasticsearch 是 面向文档 的,它存储整个对象或文档_,比如一个对象会转化成JSON格式。Elasticsearch 不仅存储文档,而且 _索引每个文档的内容使之可以被检索。在 Elasticsearch 中,你是对文档进行索引、检索、排序和过滤--而不是对行列数据。这是一种完全不同的思考数据的方式,也是 Elasticsearch 能支持复杂全文检索的原因。
-
索引
存储数据到 Elasticsearch 的行为叫做 索引 ,一个 Elasticsearch 集群可以 包含多个 索引 ,相应的每个索引可以包含多个 类型 。 这些不同的类型存储着多个 文档 ,每个文档又有 多个 属性
索引 这个词在 Elasticsearch 语境中包含多重意思:
索引(名词):
如前所述,一个 索引 类似于传统关系数据库中的一个 数据库 ,是一个存储关系型文档的地方。 索引 (index) 的复数词为 indices 或 indexes 。
索引(动词):
索引一个文档 就是存储一个文档到一个 索引 (名词)中以便它可以被检索和查询到。这非常类似于 SQL 语句中的
INSERT
关键词,除了文档已存在时新文档会替换就文档情况之外。倒排索引:
关系型数据库通过增加一个 索引 比如一个 B树(B-tree)索引 到指定的列上,以便提升数据检索速度。Elasticsearch 和 Lucene 使用了一个叫做 倒排索引 的结构来达到相同的目的。
+ 默认的,一个文档中的每一个属性都是 被索引 的(有一个倒排索引)和可搜索的。一个没有倒排索引的属性是不能被搜索到的。
http请求索引员工信息:
curl -H "Content-Type: application/json" -XPUT 'http://localhost:9200/megacorp/employee/1?pretty' -d '
{
"first_name" : "John",
"last_name" : "Smith",
"age" : 25,
"about" : "I love to go rock climbing",
"interests": [ "sports", "music" ]
}
'
curl -H "Content-Type: application/json" -XPUT 'http://localhost:9200/megacorp/employee/2?pretty' -d '
{
"first_name" : "Jane",
"last_name" : "Smith",
"age" : 32,
"about" : "I like to collect rock albums",
"interests": [ "music" ]
}
'
curl -H "Content-Type: application/json" -XPUT 'http://localhost:9200/megacorp/employee/3?pretty' -d '
{
"first_name" : "Douglas",
"last_name" : "Fir",
"age" : 35,
"about": "I like to build cabinets",
"interests": [ "forestry" ]
}
'
更新索引信息只需再次发动请求,ES会覆盖
-
简单查询
查询第一个员工的信息:
curl -H "Content-Type: application/json" -XGET 'http://localhost:9200/megacorp/employee/1?pretty'
搜索所有员工:
curl -H "Content-Type: application/json" -XGET 'http://localhost:9200/megacorp/employee/_search?pretty'
搜索姓为的Smith员工:
curl -H "Content-Type: application/json" -XGET 'http://localhost:9200/megacorp/employee/_search?q=last_name:Smith&pretty'
-
查询表达式查询
Elasticsearch 提供一个丰富灵活的查询语言叫做 查询表达式 , 它支持构建 query-string比更加复杂和健壮的查询。领域特定语言 (DSL), 指定了使用一个 JSON 请求。
将 搜索姓为的Smith员工用查询表达式重写:
curl -H "Content-Type: application/json" -XGET 'http://localhost:9200/megacorp/employee/_search?pretty' -d '
{
"query" : {
"match" : {
"last_name" : "Smith"
}
}
}
'
搜索姓氏为 Smith 且年龄大于 30的员工:
curl -H "Content-Type: application/json" -XGET 'http://localhost:9200/megacorp/employee/_search?pretty' -d '
{
"query" : {
"bool": {
"must": {
"match" : {
"last_name" : "smith"
}
},
"filter": {
"range" : {
"age" : { "gt" : 30 }
}
}
}
}
}
'
搜素所有喜欢攀岩(rock climbing)的员工
curl -H "Content-Type: application/json" -XGET 'http://localhost:9200/megacorp/employee/_search?pretty' -d '
{
"query" : {
"match" : {
"about" : "rock climbing"
}
}
}
'
返回结果为:
{ ... "hits": { "total": 2, "max_score": 0.16273327, "hits": [ { ... "_score": 0.16273327, "_source": { "first_name": "John", "last_name": "Smith", "age": 25, "about": "I love to go rock climbing", "interests": [ "sports", "music" ] } }, { ... "_score": 0.016878016, "_source": { "first_name": "Jane", "last_name": "Smith", "age": 32, "about": "I like to collect rock albums", "interests": [ "music" ] } } ] } }
相关性得分 |
Elasticsearch 默认按照相关性得分排序,即每个文档跟查询的匹配程度。第一个最高得分的结果很明显:John Smith 的 about
属性清楚地写着 “rock climbing” 。
但为什么 Jane Smith 也作为结果返回了呢?原因是她的 about
属性里提到了 “rock” 。因为只有 “rock” 而没有 “climbing” ,所以她的相关性得分低于 John 的。
这是一个很好的案例,阐明了 Elasticsearch 如何 在 全文属性上搜索并返回相关性最强的结果。Elasticsearch中的 相关性 概念非常重要,也是完全区别于传统关系型数据库的一个概念,数据库中的一条记录要么匹配要么不匹配。
找出一个属性中的独立单词是没有问题的,但有时候想要精确匹配一系列单词或者短语 。 比如, 我们想执行这样一个查询,仅匹配同时包含 “rock” 和 “climbing” ,并且 二者以短语 “rock climbing” 的形式紧挨着的雇员记录。
为此对 match
查询稍作调整,使用一个叫做 match_phrase
的查询
GET /megacorp/employee/_search
{
"query" : {
"match_phrase" : {
"about" : "rock climbing"
}
},
"highlight": {
"fields" : {
"about" : {}
}
}
}
highlight
参数表示高亮,当执行该查询时,结果中还多了一个叫做 highlight
的部分。这个部分包含了 about
属性匹配的文本片段,并以 HTML 标签 <em></em>
封装。
-
聚合查询
挖掘出雇员中最受欢迎的兴趣爱好:
GET /megacorp/employee/_search
{
"aggs": {
"all_interests": {
"terms": { "field": "interests" }
}
}
}
ps:会统计每一个兴趣的喜欢人数,并按人数从多到少排序输出。
姓Smith 的员工中最受欢迎的兴趣爱好是什么,可以直接添加适当的查询来组合查询:
GET /megacorp/employee/_search
{
"query": {
"match": {
"last_name": "smith"
}
},
"aggs": {
"all_interests": {
"terms": {
"field": "interests"
}
}
}
}
ps:会筛选 再聚合
聚合还支持分级汇总 。比如,查询特定兴趣爱好员工的平均年龄:
GET /megacorp/employee/_search
{
"aggs" : {
"all_interests" : {
"terms" : { "field" : "interests" },
"aggs" : {
"avg_age" : {
"avg" : { "field" : "age" }
}
}
}
}
}
结果为:
... "all_interests": { "buckets": [ { "key": "music", "doc_count": 2, "avg_age": { "value": 28.5 } }, { "key": "forestry", "doc_count": 1, "avg_age": { "value": 35 } }, { "key": "sports", "doc_count": 1, "avg_age": { "value": 25 } } ] }
ps:有一个兴趣及数量的列表,每个兴趣都有了一个附加的 avg_age
属性,代表有这个兴趣爱好的所有员工的平均年龄