ES学习之ES语法入门

背景:

 公司最近的项目涉及到ES查询,空余之中对ES进行了一个入门的学习,ES真是个强大的搜索引擎。

ES 入门语法(ES对document 文档的增删改查)

  1 #获取所有索引
  2 GET _search
  3 {
  4   "query": {
  5     "match_all": {}
  6   }
  7 }
  8 #获取集群的健康状态
  9 GET /_cat/health?v
 10 #获取ES节点
 11 GET /_cat/nodes?v
 12 #获取所有index
 13 GET /_cat/indices?v
 14 #获取es的健康状态,索引
 15 GET /_cat/indices?v&h=health,status,index
 16 PUT /xinhua
 17 {
 18   "acknowledged": true,
 19   "shards_acknowledged": true,
 20   "index": "xinhua"
 21 }
 22 GET /question_content/all
 23 #在es里创建一个索引为megacorp,索引为employee ,id 为1的第一条数据
 24 
 25 #将 HTTP 命令由 PUT 改为 GET 可以用来检索文档,同样的,可以使用 DELETE 命令来删除文档,以及使用 HEAD 指令来检查文档是否存在。如果想更新已存在的文档,只需再次 PUT 
 26 PUT /megacorp/employee/1
 27 {
 28     "first_name" : "John",
 29     "last_name" :  "Smith",
 30     "age" :        25,
 31     "about" :      "I love to go rock climbing",
 32     "interests": [ "sports", "music" ]
 33 }
 34 
 35 GET /megacorp/employe/1
 36 PUT /megacorp/employee/2
 37 {
 38     "first_name" :  "Jane",
 39     "last_name" :   "Smith",
 40     "age" :         32,
 41     "about" :       "I like to collect rock albums",
 42     "interests":  [ "music" ]
 43 }
 44 PUT /megacorp/employee/3
 45 {
 46     "first_name" :  "Douglas",
 47     "last_name" :   "Fir",
 48     "age" :         35,
 49     "about":        "I like to build cabinets",
 50     "interests":  [ "forestry" ]
 51 }
 52 GET /megacorp/employee/1
 53 #查看文档是否存在
 54 HEAD /megacorp/employee/1
 55 #检索文档
 56 GET /megacorp/employee/_search
 57 #条件检索文档,查找出last_name = Smith的雇员
 58 GET /megacorp/employee/_search?q=last_name:Smith
 59 #ES DSL 查询语句,可以拼接更多的查询条件,后面会介绍
 60 GET /megacorp/employee/_search
 61 {
 62   "query": {
 63     "match": {
 64       "last_name": "Smith"
 65     }
 66   }
 67  
 68 }
 69 
 70 
 71 
 72 
 73 #按照组合条件搜索,查询出last_name = Smith,且年龄大于30岁的雇员,gte为大于
 74 GET /megacorp/employee/_search
 75 {
 76   "query": {
 77     "bool": {
 78     "must": 
 79       {
 80         "match": {
 81           "last_name": "Smith"
 82         }
 83       },
 84       "filter": 
 85         {"range": {
 86           "age": {
 87             "gte": 30
 88           }
 89         }}
 90 
 91     }
 92   }
 93 }
 94 #ES默认全文匹配含有的搜索
 95 GET /megacorp/employee/_search
 96 {
 97   "query": {
 98     "match": {
 99       "about": "rock climbing"
100     }
101   }
102 }
103 #短语搜索,查询 "rock climbing" 短语存在的雇员
104 GET /megacorp/employee/_search
105 {
106   "query": {
107     "match_phrase": {
108       "about": "rock climbing"
109     }
110   }
111 }
112 #高亮搜索
113 GET /megacorp/employee/_search
114 {
115   "query": {
116     "match_phrase": {
117       "about": "rock climbing"
118     }
119   },
120   "highlight": {
121     "fields": {
122       "about":{}
123     }
124   }
125 }
126 #分析
127 #终于到了最后一个业务需求:支持管理者对员工目录做分析。 #Elasticsearch 有一个功能叫聚合(aggregations),允许我们基于##数据生成一些精细的分析结果。聚合与 SQL 中的 GROUP BY #类似但更强大。
128 #举个例子,挖掘出员工中最受欢迎的兴趣爱好:
129 
130 GET /megacorp/employee/_search
131 {
132   "aggs": {
133     "all_interests": {
134       "terms": { "field": "interests" }
135     }
136   }
137 }
138 #坑1:从异常信息中可以看出,是因为我要聚合的字段【interests】没有进行优化,也类似没有加索引。没有优化的字段es默认是禁止聚合/排序操作的。所以需要将要聚合的字段添加优化
139 PUT /megacorp/_mapping
140 {
141   "properties": {
142     "interests": { 
143       "type":     "text",
144       "fielddata": true
145     }
146   }
147 }

官方文档:https://www.elastic.co/guide/cn/elasticsearch/guide/current/index.html

原文地址:https://www.cnblogs.com/Neotester/p/12791903.html