elastic 搜索
原文摘取位置:https://www.elastic.co/guide/cn/elasticsearch/guide/current/index.html
写这篇文章的时候是用的5.6.3 版本。
安装遇到的问题:
在:安装 ./bin/kibana plugin –install elastic/sense 的时候发现无法安装上去,提示:unknown command plugin ,然后经过各种尝试发现还是安装不上,其中kibana-plugin也尝试了一下。然后就查了一下这个问题。竟然在kibana 5.*以后已经默认安装这个了。所以打开http://localhost:5601/app/kibana#/dev_tools/console?_g=() 这个就是可以直接可以操作啦。
简单操作
计算集群数量
原始写法:
curl -XGET 'localhost:9200/_count?pretty' -d '
{
"query": {
"match_all": {}
}
}'
sense写法:
GET /_count
{
"query": {
"match_all": {}
}
}
员工的操作
添加员工
PUT /megacorp/employee/1
{
"first_name" : "John",
"last_name" : "Smith",
"age" : 25,
"about" : "I love to go rock climbing",
"interests": [ "sports", "music" ]
}
PUT /megacorp/employee/2
{
"first_name" : "Jane",
"last_name" : "Smith",
"age" : 32,
"about" : "I like to collect rock albums",
"interests": [ "music" ]
}
PUT /megacorp/employee/3
{
"first_name" : "Douglas",
"last_name" : "Fir",
"age" : 35,
"about": "I like to build cabinets",
"interests": [ "forestry" ]
}
解释:
注意,路径 /megacorp/employee/1 包含了三部分的信息:
- megacorp:索引名称
- employee:类型名称
- 1:特定雇员的ID
查询编号:
GET /megacorp/employee/1
查询所有员工
GET /megacorp/employee/_search
根据指定条件查询
GET /megacorp/employee/_search?q=last_name:Smith
根据指定表达式查询
GET /megacorp/employee/_search
{
"query" : {
"match" : {
"last_name" : "Smith"
}
}
}
复杂的表达式查询
GET /megacorp/employee/_search
{
"query" : {
"bool": {
"must": {
"match" : {
"last_name" : "smith"
}
},
"filter": {
"range" : {
"age" : { "gt" : 30 }
}
}
}
}
}
- 这部分与我们之前使用的 match 查询 一样。
- 这部分是一个 range 过滤器 , 它能找到年龄大于 30 的文档,其中 gt 表示_大于(_great than)
相关性查询
GET /megacorp/employee/_search
{
"query" : {
"match" : {
"about" : "rock climbing"
}
}
}
这时会有两个结果,按照相关性得分进行排序。
- Elasticsearch 默认按照相关性得分排序,即每个文档跟查询的匹配程度。第一个最高得分的结果很明显:John Smith 的 about 属性清楚地写着 “rock climbing” 。
- 但为什么 Jane Smith 也作为结果返回了呢?原因是她的 about 属性里提到了 “rock” 。因为只有 “rock” 而没有 “climbing” ,所以她的相关性得分低于 John 的。
- 这是一个很好的案例,阐明了 Elasticsearch 如何 在 全文属性上搜索并返回相关性最强的结果。Elasticsearch中的 相关性 概念非常重要,也是完全区别于传统关系型数据库的一个概念,数据库中的一条记录要么匹配要么不匹配。
精准匹配查询
GET /megacorp/employee/_search
{
"query" : {
"match_phrase" : {
"about" : "rock climbing"
}
}
}
高亮搜索
GET /megacorp/employee/_search
{
"query" : {
"match_phrase" : {
"about" : "rock climbing"
}
},
"highlight": {
"fields" : {
"about" : {}
}
}
}
分析函数
GET /megacorp/employee/_search
{
"aggs": {
"all_interests": {
"terms": { "field": "interests" }
}
}
}
官网上的这个我没有执行过去,报错:
org.elasticsearch.transport.RemoteTransportException:[node_1][127.0.0.1:9300][indices:data/read/search[phase/query]]
这个节点在启动的时候,已经查看到啦,然后将 terms去掉提示:
Missing definition for aggregation [all_interests] 说明这个是可以用的。由此将interests替换成age 就可以看到结果了
"aggregations": {
"all_interests": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": 25,
"doc_count": 1
},
{
"key": 32,
"doc_count": 1
},
{
"key": 35,
"doc_count": 1
}
]
}
}
找到答案:
搜了一下应该是5.x后对排序,聚合这些操作用单独的数据结构(fielddata)缓存到内存里了,需要单独开启,官方解释在此fielddata
简单来说就是在聚合前执行如下操作
PUT megacorp/_mapping/employee/
{
“properties”: {
“interests”: {
“type”: “text”,
“fielddata”: true
}
}
}
文章链接http://blog.youkuaiyun.com/u011403655/article/details/71107415
聚合附带查询条件
GET /megacorp/employee/_search
{
"query": {
"match": {
"last_name": "smith"
}
},
"aggs": {
"all_interests": {
"terms": {
"field": "interests"
}
}
}
}
聚合还支持分级汇总
GET /megacorp/employee/_search
{
"aggs" : {
"all_interests" : {
"terms" : { "field" : "interests" },
"aggs" : {
"avg_age" : {
"avg" : { "field" : "age" }
}
}
}
}
}
- 输出基本是第一次聚合的加强版。依然有一个兴趣及数量的列表,只不过每个兴趣都有了一个附加的 avg_age 属性,代表有这个兴趣爱好的所有员工的平均年龄。
- 即使现在不太理解这些语法也没有关系,依然很容易了解到复杂聚合及分组通过 Elasticsearch 特性实现得很完美。可提取的数据类型毫无限制。