Elasticsearch学习记录(入门篇)

1、 Elasticsearch的请求与结果

请求结构

curl -X<VERB> '<PROTOCOL>://<HOST>:<PORT>/<PATH>?<QUERY_STRING>' -d '<BODY>'
  • VERB HTTP方法:GET, POST, PUT, HEAD, DELETE
  • PROTOCOL http或者https协议(只有在Elasticsearch前面有https代理的时候可用)
  • HOST Elasticsearch集群中的任何一个节点的主机名,如果是在本地的节点,那么就叫localhost
  • PORT Elasticsearch HTTP服务所在的端口,默认为9200
  • PATH API路径(例如_count将返回集群中文档的数量),PATH可以包含多个组件,例如_cluster/stats或者_nodes/stats/jvm
  • QUERY_STRING 一些可选的查询请求参数,例如?pretty参数将使请求返回更加美观易读的JSON数据

    BODY 一个JSON格式的请求主体(如果请求需要的话)

PUT创建(索引创建)

$ curl -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" ]

}

{

"_index" : "megacorp",

"_type" : "employee",

"_id" : "3",

"_version" : 1,

"_shards" : {

"total" : 2,

"successful" : 1,

"failed" : 0

},

"created" : true

}

##GET请求(搜索)
###检索文档

$ curl -XGET 'http://localhost:9200/megacorp/employee/1?pretty'

{

"_index" : "megacorp",

"_type" : "employee",

"_id" : "1",

"_version" : 1,

"found" : true,

"_source" : {

"first_name" : "John",

"last_name" : "Smith",

"age" : 25,

"about" : "I love to go rock climbing",

"interests" : [ "sports", "music" ]

}

}

###简单搜索
使用`megacorp`索引和`employee`类型,但是我们在结尾使用关键字\_search来取代原来的文档ID。响应内容的hits数组中包含了我们所有的三个文档。默认情况下搜索会返回前10个结果。

$ curl -XGET 'http://localhost:9200/megacorp/employee/_search?pretty'

{

"took" : 2,

"timed_out" : false,

"_shards" : {

"total" : 5,

"successful" : 5,

"failed" : 0

},

"hits" : {

"total" : 3,

"max_score" : 1.0,

"hits" : [ {

"_index" : "megacorp",

"_type" : "employee",

"_id" : "2",

"_score" : 1.0,

"_source" : {

"first_name" : "Jane",

"last_name" : "Smith",

"age" : 32,

"about" : "I like to collect rock albums",

"interests" : [ "music" ]

}

}, {

"_index" : "megacorp",

"_type" : "employee",

"_id" : "1",

"_score" : 1.0,

"_source" : {

"first_name" : "John",

"last_name" : "Smith",

"age" : 25,

"about" : "I love to go rock climbing",

"interests" : [ "sports", "music" ]

}

}, {

"_index" : "megacorp",

"_type" : "employee",

"_id" : "3",

"_score" : 1.0,

"_source" : {

"first_name" : "Douglas",

"last_name" : "Fir",

"age" : 35,

"about" : "I like to build cabinets",

"interests" : [ "forestry" ]

}

} ]

}

}

接下来,让我们搜索姓氏中包含“Smith”的员工。我们将在命令行中使用轻量级的搜索方法。这种方法常被称作查询字符串(query string)搜索,因为我们像传递URL参数一样去传递查询语句:

$ curl -XGET 'http://localhost:9200/megacorp/employee/_search?q=last_name:Smith&pretty'


{

"took" : 4,

"timed_out" : false,

"_shards" : {

"total" : 5,

"successful" : 5,

"failed" : 0

},

"hits" : {

"total" : 2,

"max_score" : 0.30685282,

"hits" : [ {

"_index" : "megacorp",

"_type" : "employee",

"_id" : "2",

"_score" : 0.30685282,

"_source" : {

"first_name" : "Jane",

"last_name" : "Smith",

"age" : 32,

"about" : "I like to collect rock albums",

"interests" : [ "music" ]

}

}, {

"_index" : "megacorp",

"_type" : "employee",

"_id" : "1",

"_score" : 0.30685282,

"_source" : {

"first_name" : "John",

"last_name" : "Smith",

"age" : 25,

"about" : "I love to go rock climbing",

"interests" : [ "sports", "music" ]

}

} ]

}

}

###使用DSL语句查询
查询字符串搜索便于通过命令行完成特定(ad hoc)的搜索,但是它也有局限性(参阅简单搜索章节)。Elasticsearch提供丰富且灵活的查询语言叫做DSL查询(Query DSL),它允许你构建更加复杂、强大的查询。 DSL(Domain Specific Language特定领域语言)以JSON请求体的形式出现。我们可以这样表示之前关于“Smith”的查询:

$ curl -XGET 'http://localhost:9200/megacorp/employee/_search?pretty' -d '

{

"query" : {

"match" : {

"last_name" : "Smith"

}

}

}

'

###更复杂的搜索
我们让搜索稍微再变的复杂一些。我们依旧想要找到姓氏为“Smith”的员工,但是我们只想得到年龄大于30岁的员工。我们的语句将添加过滤器(filter),它使得我们高效率的执行一个结构化搜索:

$ curl -XGET 'http://localhost:9200/megacorp/employee/_search?pretty' -d '

{

"query" : {

"filtered" : {

"filter" : {

"range" : {

"age" : { "gt" : 30 } --<1>

}

},

"query" : {

"match" : {

"last_name" : "smith" --<2>

}

}

}

}

}

'


* <1> 这部分查询属于区间过滤器(range filter),它用于查找所有年龄大于30岁的数据——gt为"greater than"的缩写。
* <2> 这部分查询与之前的match语句(query)一致。

{

"took" : 2,

"timed_out" : false,

"_shards" : {

"total" : 5,

"successful" : 5,

"failed" : 0

},

"hits" : {

"total" : 1,

"max_score" : 0.30685282,

"hits" : [ {

"_index" : "megacorp",

"_type" : "employee",

"_id" : "2",

"_score" : 0.30685282,

"_source" : {

"first_name" : "Jane",

"last_name" : "Smith",

"age" : 32,

"about" : "I like to collect rock albums",

"interests" : [ "music" ]

}

} ]

}

}

###全文搜索
到目前为止搜索都很简单:搜索特定的名字,通过年龄筛选。让我们尝试一种更高级的搜索,全文搜索——一种传统数据库很难实现的功能。 我们将会搜索所有喜欢“rock climbing”的员工:

$ curl -XGET 'http://localhost:9200/megacorp/employee/_search?pretty' -d '

{

"query" : {

"match" : {

"about" : "rock climbing"

}

}

}

'

你可以看到我们使用了之前的`match`查询,从`about`字段中搜索"**rock climbing**",我们得到了两个匹配文档:

{

"took" : 3,

"timed_out" : false,

"_shards" : {

"total" : 5,

"successful" : 5,

"failed" : 0

},

"hits" : {

"total" : 2,

"max_score" : 0.16273327,

"hits" : [ {

"_index" : "megacorp",

"_type" : "employee",

"_id" : "1",

"_score" : 0.16273327,<1>

"_source" : {

"first_name" : "John",

"last_name" : "Smith",

"age" : 25,

"about" : "I love to go rock climbing",

"interests" : [ "sports", "music" ]

}

}, {

"_index" : "megacorp",

"_type" : "employee",

"_id" : "2",

"_score" : 0.016878016,<2>

"_source" : {

"first_name" : "Jane",

"last_name" : "Smith",

"age" : 32,

"about" : "I like to collect rock albums",

"interests" : [ "music" ]

}

} ]

}

}


* <1><2> 结果相关性评分。 默认情况下,Elasticsearch根据结果相关性评分来对结果集进行排序,所谓的「结果相关性评分」就是文档与查询条件的匹配程度。很显然,排名第一的`John Smith`的`about`字段明确的写到“**rock climbing**” 但是为什么`Jane Smith`也会出现在结果里呢?原因是“**rock**”在她的abuot字段中被提及了。因为只有“**rock**”被提及而“**climbing**”没有,所以她的`_score`要低于John。 ###短语搜索
目前我们可以在字段中搜索单独的一个词,这挺好的,但是有时候你想要确切的匹配若干个单词或者短语(phrases)。例如我们想要查询同时包含"rock"和"climbing"(并且是相邻的)的员工记录。 要做到这个,我们只要将`match`查询变更为`match_phrase`查询即可:

$ curl -XGET 'http://localhost:9200/megacorp/employee/_search?pretty' -d '

{

"query" : {

"match_phrase" : {

"about" : "rock climbing"

}

}

}

'

{

"took" : 16,

"timed_out" : false,

"_shards" : {

"total" : 5,

"successful" : 5,

"failed" : 0

},

"hits" : {

"total" : 1,

"max_score" : 0.23013961,

"hits" : [ {

"_index" : "megacorp",

"_type" : "employee",

"_id" : "1",

"_score" : 0.23013961,

"_source" : {

"first_name" : "John",

"last_name" : "Smith",

"age" : 25,

"about" : "I love to go rock climbing",

"interests" : [ "sports", "music" ]

}

} ]

}

}

###高亮我们的搜索
很多应用喜欢从每个搜索结果中**高亮(highlight)**匹配到的关键字,这样用户可以知道为什么这些文档和查询相匹配。在Elasticsearch中高亮片段是非常容易的。 让我们在之前的语句上增加`highlight`参数:

$ curl -XGET 'http://localhost:9200/megacorp/employee/_search?pretty' -d '

{

"query" : {

"match_phrase" : {

"about" : "rock climbing"

}

},

"highlight": {

"fields" : {

"about" : {}

}

}

}

'

当我们运行这个语句时,会命中与之前相同的结果,但是在返回结果中会有一个新的部分叫做`highlight`,这里包含了来自`about`字段中的文本,并且用\<em>\</em>来标识匹配到的单词。

{

"took" : 33,

"timed_out" : false,

"_shards" : {

"total" : 5,

"successful" : 5,

"failed" : 0

},

"hits" : {

"total" : 1,

"max_score" : 0.23013961,

"hits" : [ {

"_index" : "megacorp",

"_type" : "employee",

"_id" : "1",

"_score" : 0.23013961,

"_source" : {

"first_name" : "John",

"last_name" : "Smith",

"age" : 25,

"about" : "I love to go rock climbing",

"interests" : [ "sports", "music" ]

},

"highlight" : {

"about" : [ "I love to go rock climbing" ]

}

} ]

}

}

##聚合
###分析
最后,我们还有一个需求需要完成:允许管理者在职员目录中进行一些分析。 Elasticsearch有一个功能叫做**聚合(aggregations)**,它允许你在数据上生成复杂的分析统计。它很像SQL中的`GROUP BY`但是功能更强大。

$ curl -XGET 'http://localhost:9200/megacorp/employee/_search?pretty' -d '

{

"aggs": {

"all_interests": {

"terms": { "field": "interests" }

}

}

}

'

查询结果:

{...

"aggregations" : {

"all_interests" : {

"doc_count_error_upper_bound" : 0,

"sum_other_doc_count" : 0,

"buckets" : [ {

"key" : "music",

"doc_count" : 2

}, {

"key" : "forestry",

"doc_count" : 1

}, {

"key" : "sports",

"doc_count" : 1

} ]

}

}

}

这些数据并没有被预先计算好,它们是实时的从匹配查询语句的文档中动态计算生成的。

如果我们想知道所有姓"Smith"的人最大的共同点(兴趣爱好),我们只需要增加合适的语句既可:

$ curl -XGET 'http://localhost:9200/megacorp/employee/_search?pretty' -d '

{

"query": {

"match": {

"last_name": "smith"

}

},

"aggs": {

"all_interests": {

"terms": {

"field": "interests"

}

}

}

}

'

all_interests聚合已经变成只包含和查询语句相匹配的文档了:

...

"all_interests": {

"buckets": [

{

"key": "music",

"doc_count": 2

},

{

"key": "sports",

"doc_count": 1

}

]

}


聚合也允许分级汇总。例如,让我们统计每种兴趣下职员的平均年龄:

$ curl -XGET 'http://localhost:9200/megacorp/employee/_search?pretty' -d '

{

"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

}

}

]

}

该聚合结果比之前的聚合结果要更加丰富。我们依然得到了兴趣以及数量(指具有该兴趣的员工人数)的列表,但是现在每个兴趣额外拥有`avg_age`字段来显示具有该兴趣员工的平均年龄。

Elasticsearch学习记录(入门篇)的更多相关文章

  1. ElasticSearch学习,入门篇(一)

    概念解析 1.什么是搜索 搜索就是在任何场景下,找寻你想要的信息,这个时候你会输入一段要搜索的关键字,然后期望找到这个关键字相关的有效信息. 2.如果用数据库做搜素会怎么样 select * from ...

  2. Netty学习记录-入门篇

    你如果,缓缓把手举起来,举到顶,再突然张开五指,那恭喜你,你刚刚给自己放了个烟花. 模块介绍 netty-bio: 阻塞型网络通信demo. netty-nio: 引入channel(通道).buff ...

  3. ElasticSearch 学习记录之ES几种常见的聚合操作

    ES几种常见的聚合操作 普通聚合 POST /product/_search { "size": 0, "aggs": { "agg_city&quo ...

  4. ElasticSearch 学习记录之ES短语匹配基本用法

    短语匹配 短语匹配故名思意就是对分词后的短语就是匹配,而不是仅仅对单独的单词进行匹配 下面就是根据下面的脚本例子来看整个短语匹配的有哪些作用和优点 GET /my_index/my_type/_sea ...

  5. ElasticSearch 学习记录之 分布式文档存储往ES中存数据和取数据的原理

    分布式文档存储 ES分布式特性 屏蔽了分布式系统的复杂性 集群内的原理 垂直扩容和水平扩容 真正的扩容能力是来自于水平扩容–为集群添加更多的节点,并且将负载压力和稳定性分散到这些节点中 ES集群特点 ...

  6. ElasticSearch 学习记录之如任何设计可扩容的索引结构

    扩容设计 扩容的单元 一个分片即一个 Lucene 索引 ,一个 Elasticsearch 索引即一系列分片的集合 一个分片即为 扩容的单元 . 一个最小的索引拥有一个分片. 一个只有一个分片的索引 ...

  7. ElasticSearch 学习记录之ES高亮搜索

    高亮搜索 ES 通过在查询的时候可以在查询之后的字段数据加上html 标签字段,使文档在在web 界面上显示的时候是由颜色或者字体格式的 GET /product/_search { "si ...

  8. ElasticSearch 学习记录之ES查询添加排序字段和使用missing或existing字段查询

    ES添加排序 在默认的情况下,ES 是根据文档的得分score来进行文档额排序的.但是自己可以根据自己的针对一些字段进行排序.就像下面的查询脚本一样.下面的这个查询是根据productid这个值进行排 ...

  9. ElasticSearch 学习记录之父子结构的查询

    父子结构 父亲type属性查询子type 的类型 父子结构的查询,可以通过父亲类型的字段,查询出子类型的索引信息 POST /product/_search { "query": ...

随机推荐

  1. 九度OJ 1113:二叉树 (完全二叉树)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:5079 解决:1499 题目描述: 如上所示,由正整数1,2,3--组成了一颗特殊二叉树.我们已知这个二叉树的最后一个结点是n.现在的问题是 ...

  2. 前端photoshop 切图神器cutterman

    1. 切图真的是就件很费力的事情,接下有给大家提供一个工具,本人觉得还不错 http://www.cutterman.cn/ 请参考这个网站,安装方法也有,很简单,我就不说了,赶紧点连接去注册帐号吧

  3. linux常用命令与技巧(不断添加与更新)

    linux常用命令:linux命令有很多,不去做全面的介绍与总结,这里只是记录在使用linux过程中遇到的常用命令和技巧,以便以后查阅. # mkdir -p /usr/local/src/ ##递归 ...

  4. LightOJ1138 —— 阶乘末尾0、质因子分解

    题目链接:https://vjudge.net/problem/LightOJ-1138 1138 - Trailing Zeroes (III)    PDF (English) Statistic ...

  5. oracle添加表注释和表字段注释

    创建Oracle数据库表时加上注释 CREATE TABLE t1( id  varchar2(32) primary key,name VARCHAR2(8) NOT NULL, age numbe ...

  6. linux应用之mysql数据库指定版本的yum安装(centos)

    A Quick Guide to Using the MySQL Yum Repository Abstract The MySQL Yum repository provides RPM packa ...

  7. Struts2 Action 匹配的几种方式

    下面针对我所遇见的Action的配置方法进行一下总结: 1.基本的匹配方法

  8. HihoCoder 1570 : 小Hi与法阵(简单几何)

    描述 小Hi喜欢大,而小Ho喜欢小.他们所在的城市(视为二维平面)有N座法阵.现在他们各选三座法阵,以三座法阵为顶点组成三角形,并站在所选三角形的重心位置:二人选择的法阵可以有相同的.小Hi选择面积最 ...

  9. C++类对象之间的类型转换和重载

    类对象和其他类型对象的转换 转换场合有: 赋值转换 表达式中的转换 显式转换 函数调用, 传递参数时的转换 转换方向有: 由定义类向其他类型的转换 由其他类型向定义类的转换 #include < ...

  10. C++之static类成员,static类成员函数

    0.static修饰类中成员,表示类的共享数据 1.static类成员 在C++primer里面说过,static类成员不像普通的类数据成员,static类数据成员独立于一切类对象处在.static类 ...