Elasticsearch安装
1、官网下载 https://www.elastic.co/downloads/elasticsearch
2、下载zip,解压
linux运行 ./bin/elasticsearch
windows运行 bin目录下 elasticsearch.bat
kibana安装 同Elasticsearch 官网地址 https://www.elastic.co/downloads/kibana
入门学习命令集合
可直接复制到kibana--》dev tools执行
kibana默认地址 http://localhost:5601/app/kibana
Elasticsearch默认地址 http://localhost:9200/?pretty
#查看集群健康状态 status green正常 yellow主分片正常 unassigned_shards表示有5个副本分片没有分配到任何节点
#索引在默认情况下会被分配5个主分片
GET /_cluster/health
#添加数据
#megacorp 索引名称
#employee 类型名称
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" ]
}
#查找一个指定id的文档
GET /megacorp/employee/1
#检查文档是否存在
HEAD /megacorp/employee/1
#简单搜索,搜索指定索引,指定类型的所有文档
GET /megacorp/employee/_search
#使用查询表达式 查询指定filed
GET /megacorp/employee/_search
{
"query": {
"match": {
"last_name": "Smith"
}
}
}
#使用查询表达式+过滤 查询指定filed last_name 并且age大于等于30
GET /megacorp/employee/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"last_name": "Smith"
}
}
],
"filter": {
"range": {
"age": {
"gte": 30
}
}
}
}
}
}
#全文检索,搜索about属性上是否包含 rock climbing 结果按相关性强弱排序,具体见结果 相关性是Elasticsearch很重要的概念,与传统关系性数据库不同,传统的要么匹配,要么不匹配
GET /megacorp/employee/_search
{
"query": {
"match": {
"about": "rock climbing"
}
}
}
#短语搜索,必须包含所有搜索内容
GET /megacorp/employee/_search
{
"query" : {
"match_phrase" : {
"about" : "rock climbing"
}
}
}
#高亮搜索 返回一个highlight 的部分,高亮内容用<em></em>包装
GET /megacorp/employee/_search
{
"query" : {
"match_phrase" : {
"about" : "rock climbing"
}
},
"highlight": {
"fields" : {
"about" : {}
}
}
}
#分析搜索 返回一个aggregations,统计每个搜索filed的内容出现次数 与group by类似,但功能更强
GET /megacorp/employee/_search
{
"aggs": {
"all_interests": {
"terms": { "field": "interests.keyword" }
}
}
}
#简单组合查询 搜索last_name为指定内容的员工的兴趣分析
GET /megacorp/employee/_search
{
"query": {
"match": {
"last_name": "smith"
}
},
"aggs": {
"all_interests": {
"terms": { "field": "interests.keyword" }
}
}
}
#聚合还支持分级汇总 查询特定兴趣爱好的平均年龄
GET /megacorp/employee/_search
{
"query": {
"match": {
"last_name": "smith"
}
},
"aggs": {
"all_interests": {
"terms": {
"field": "interests.keyword"
},
"aggs" : {
"avg_age" : {
"avg" : {
"field" : "age"
}
}
}
}
}
}