讲师:瓦力
Postman直接导入版本在最下边,当前使用elasticsearch-6.4.0。
在 ES2.x 版本字符串数据是没有 keyword 和 text 类型的,只有string类型
ES更新到5版本后,取消了 string 数据类型,代替它的是 keyword 和 text 数据类型
text:可分词
keyword:无法被分词
"index": "analyzed" 新版本中直接删除
• "number_of_shards": "5", 默认分片数
• "number_of_replicas": "1", 默认副本数
一、创建索引
方式1:先创建,后结构化
1.非结构化索引(索引信息中"mappings": { })
2.给索引添加mappings结构体:
方式2:直接创建并结构化
二、插入数据
方式1:指定文档id插入,put
方式2:自动生成文档id,post
三、修改数据
方式1:指定文档id修改数据,post
方式2:使用脚本语言修改数据,post
三、删除文档
四、删除索引
1.可以通过head插件直接删除。
2.通过API删除。
五、简单查询
1.根据文档id查询
2.查询所有
3.分页查询:
4.关键词查询:
5.聚合查询:group by
6.统计查询:
查询最小值:
六、高级查询
Query context:含匹配度
Filter context:是否匹配(不计算score,会缓存->性能好)
除了需要匹配程度的查询,其余的查询都应该使用filter。
全文本查询:针对文本类型数据
模糊匹配(默认分词)
{
"query":{
"match":{
"author":"瓦力"
}
}
}
短语匹配
{
"query":{
"match_phrase":{
"title":"Elastic搜索"
}
}
}
字段匹配
{
"query":{
"multi_match":{
"query":"Elastic搜索",
"filter": ["author","title"]
}
}
}
语法查询
{
"query":{
"query_string": {
"query":"(Elastic搜索 AND 入门) OR Python"
}
}
}
语法+字段
{
"query":{
"query_string": {
"query":"Elastic OR Python",
"fields":["title","author"]
}
}
}
字段级别查询:针对结构化数据,如数字、日期等
精确查询(不会分词)
{
"query":{
"term":{
"word_count":1000
}
}
}
范围查询
{
"query":{
"range":{
"word_count":{
"gte":1000, //后面这个e就是equal
"lte":2000
}
}
}
}
日期查询
{
"query":{
"range":{
"date":{
"gte":"2017-1-1", //后面这个e就是equal
"lte":"now"
}
}
}
}
Filter context:
{
"query": {
"bool":{
"filter":{
"term":{
"word_count":1000
}
}
}
}
}
复合查询:
固定分数查询
{
"query":{
"constant_score":{ //只支持filter,不支持match
"filter":{
"match":{
"title":"Elastic搜索"
}
},
"boost":2 //指定分数为2
}
}
}
shuld查询:
{
"query":{
"should":[// 相当于OR
{
"match":{
"author":"瓦力"
}
},
{
"match":{
"title":"Elastic搜索"
}
}
]
}
}
must查询:
{
"query":{
"bool":{
"must":[// 相当于AND
{
"match":{
"author":"瓦力"
}
},
{
"match":{
"title":"Elastic搜索"
}
}
],
"filter":[
"term":{
"word_count":1000
}
]
}
}
}
must_not查询:
{
"query":{
"bool":{
"must_not":{// 相当于非
"term":{
"author":"瓦力"
}
}
}
}
}
Postman :
{
"info": {
"_postman_id": "c8cddaa9-194e-4868-b71d-3ca54c149b11",
"name": "ES_Demo",
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
},
"item": [
{
"name": "cat API",
"item": [
{
"name": "检测集群是否健康",
"request": {
"method": "GET",
"header": [
{
"key": "Content-Type",
"name": "Content-Type",
"value": "application/json",
"type": "text"
}
],
"body": {
"mode": "raw",
"raw": ""
},
"url": {
"raw": "http://localhost:9200/_cat/health?v",
"protocol": "http",
"host": [
"localhost"
],
"port": "9200",
"path": [
"_cat",
"health"
],
"query": [
{
"key": "v",
"value": null
}
]
}
},
"response": []
},
{
"name": "获取所有索引",
"request": {
"method": "GET",
"header": [
{
"key": "Content-Type",
"name": "Content-Type",
"value": "application/json",
"type": "text"
}
],
"body": {
"mode": "raw",
"raw": ""
},
"url": {
"raw": "http://localhost:9200/_cat/indices?v",
"protocol": "http",
"host": [
"localhost"
],
"port": "9200",
"path": [
"_cat",
"indices"
],
"query": [
{
"key": "v",
"value": null
}
]
}
},
"response": []
},
{
"name": "获取集群的节点列表",
"request": {
"method": "GET",
"header": [
{
"key": "Content-Type",
"name": "Content-Type",
"value": "application/json",
"type": "text"
}
],
"body": {
"mode": "raw",
"raw": ""
},
"url": {
"raw": "http://localhost:9200/_cat/nodes?v",
"protocol": "http",
"host": [
"localhost"
],
"port": "9200",
"path": [
"_cat",
"nodes"
],
"query": [
{
"key": "v",
"value": null
}
]
}
},
"response": []
}
]
},
{
"name": "restful API",
"item": [
{
"name": "创建索引不结构化",
"request": {
"method": "PUT",
"header": [
{
"key": "Content-Type",
"name": "Content-Type",
"type": "text",
"value": "application/json"
}
],
"body": {
"mode": "raw",
"raw": ""
},
"url": {
"raw": "http://localhost:9200/test?pretty",
"protocol": "http",
"host": [
"localhost"
],
"port": "9200",
"path": [
"test"
],
"query": [
{
"key": "pretty",
"value": null
}
]
}
},
"response": []
},
{
"name": "查询settings",
"request": {
"method": "GET",
"header": [
{
"key": "Content-Type",
"name": "Content-Type",
"value": "application/json",
"type": "text"
}
],
"body": {
"mode": "raw",
"raw": ""
},
"url": {
"raw": "http://localhost:9200/test/_settings?pretty",
"protocol": "http",
"host": [
"localhost"
],
"port": "9200",
"path": [
"test",
"_settings"
],
"query": [
{
"key": "pretty",
"value": null
}
]
}
},
"response": []
},
{
"name": "添加mappings",
"request": {
"method": "POST",
"header": [
{
"key": "Content-Type",
"name": "Content-Type",
"type": "text",
"value": "application/json"
}
],
"body": {
"mode": "raw",
"raw": "{\n\t\"book\":{\n\t\t\"properties\":{\n\t\t\t\"title\":{\n\t\t\t\t\"type\" : \"text\"\n\t\t\t}\n\t\t}\n\t}\n}"
},
"url": {
"raw": "http://localhost:9200/test/book/_mappings",
"protocol": "http",
"host": [
"localhost"
],
"port": "9200",
"path": [
"test",
"book",
"_mappings"
]
}
},
"response": []
},
{
"name": "创建索引并结构化",
"request": {
"method": "PUT",
"header": [
{
"key": "Content-Type",
"name": "Content-Type",
"type": "text",
"value": "application/json"
}
],
"body": {
"mode": "raw",
"raw": "{\n \"setting\": {\n \"number_of_shards\": 3,\n \"number_of_replicas\": 1\n },\n \"mappings\": {\n \"book\": {\n \"properties\": {\n \"title\": {\n \"type\": \"text\"\n }\n }\n }\n }\n}"
},
"url": {
"raw": "http://localhost:9200/test",
"protocol": "http",
"host": [
"localhost"
],
"port": "9200",
"path": [
"test"
]
}
},
"response": []
},
{
"name": "插入数据",
"request": {
"method": "PUT",
"header": [
{
"key": "Content-Type",
"name": "Content-Type",
"type": "text",
"value": "application/json"
}
],
"body": {
"mode": "raw",
"raw": "{\n \"title\":\"标题\"\n}"
},
"url": {
"raw": "http://localhost:9200/test/book/1",
"protocol": "http",
"host": [
"localhost"
],
"port": "9200",
"path": [
"test",
"book",
"1"
]
}
},
"response": []
},
{
"name": "修改数据",
"request": {
"method": "POST",
"header": [
{
"key": "Content-Type",
"name": "Content-Type",
"type": "text",
"value": "application/json"
}
],
"body": {
"mode": "raw",
"raw": "{\n \"doc\": {\n \"title\": \"SD卡T恤U型枕新中式全$铜¥吸不成顶灯长方形客厅灯复古大气卧室灯具方形玻璃家用铜灯\"\n }\n}"
},
"url": {
"raw": "http://localhost:9200/test/book/1/_update",
"protocol": "http",
"host": [
"localhost"
],
"port": "9200",
"path": [
"test",
"book",
"1",
"_update"
]
}
},
"response": []
},
{
"name": "删除Index",
"request": {
"method": "DELETE",
"header": [
{
"key": "Content-Type",
"name": "Content-Type",
"value": "application/json",
"type": "text"
}
],
"body": {
"mode": "raw",
"raw": ""
},
"url": {
"raw": "http://localhost:9200/test",
"protocol": "http",
"host": [
"localhost"
],
"port": "9200",
"path": [
"test"
]
}
},
"response": []
},
{
"name": "删除Type",
"request": {
"method": "DELETE",
"header": [
{
"key": "Content-Type",
"name": "Content-Type",
"value": "application/json",
"type": "text"
}
],
"body": {
"mode": "raw",
"raw": ""
},
"url": {
"raw": "http://localhost:9200/esbaiyou/goods/IlNtqGYBOhGO8rInOoUh",
"protocol": "http",
"host": [
"localhost"
],
"port": "9200",
"path": [
"esbaiyou",
"goods",
"IlNtqGYBOhGO8rInOoUh"
]
}
},
"response": []
},
{
"name": "查询mappings",
"request": {
"method": "GET",
"header": [
{
"key": "Content-Type",
"name": "Content-Type",
"value": "application/json",
"type": "text"
}
],
"body": {
"mode": "raw",
"raw": ""
},
"url": {
"raw": "http://localhost:9200/esbaiyou/goods/_mapping?pretty",
"protocol": "http",
"host": [
"localhost"
],
"port": "9200",
"path": [
"esbaiyou",
"goods",
"_mapping"
],
"query": [
{
"key": "pretty",
"value": null
}
]
}
},
"response": []
},
{
"name": "根据文档ID查询",
"protocolProfileBehavior": {
"disableBodyPruning": true
},
"request": {
"method": "GET",
"header": [
{
"key": "Content-Type",
"name": "Content-Type",
"value": "application/json",
"type": "text"
}
],
"body": {
"mode": "raw",
"raw": ""
},
"url": {
"raw": "http://localhost:9200/test/book/1",
"protocol": "http",
"host": [
"localhost"
],
"port": "9200",
"path": [
"test",
"book",
"1"
]
}
},
"response": []
},
{
"name": "查询:模糊匹配",
"request": {
"method": "POST",
"header": [
{
"key": "Content-Type",
"name": "Content-Type",
"value": "application/json",
"type": "text"
}
],
"body": {
"mode": "raw",
"raw": "{\r\n \"query\": {\r\n \"match\": {\r\n \"title\": \"SD卡T恤U型枕新中式全\"\r\n }\r\n }\r\n}"
},
"url": {
"raw": "http://localhost:9200/test/book/_search",
"protocol": "http",
"host": [
"localhost"
],
"port": "9200",
"path": [
"test",
"book",
"_search"
]
}
},
"response": []
},
{
"name": "查询:短语匹配",
"request": {
"method": "POST",
"header": [
{
"key": "Content-Type",
"name": "Content-Type",
"value": "application/json",
"type": "text"
}
],
"body": {
"mode": "raw",
"raw": "{\r\n \"query\":{\r\n \"match_phrase\":{\r\n \"title\":\"SD卡\"\r\n }\r\n }\r\n}\r\n"
},
"url": {
"raw": "http://localhost:9200/test/book/_search",
"protocol": "http",
"host": [
"localhost"
],
"port": "9200",
"path": [
"test",
"book",
"_search"
]
}
},
"response": []
}
]
}
]
}
错误集锦:
问题:
[2016-11-15 15:34:44,053][WARN ][transport.netty] [Korg] exception caught on transport layer [[id:0x64ce55ac,/127.0.0.1:52049 => /127.0.0.1:9300]],closing connection
java.io.IOException: 杩滅▼涓绘満寮鸿揩鍏抽棴浜嗕竴涓幇鏈夌殑杩炴帴銆?
解决:
1.处理乱码:Elasticsearch 的 config/jvm.options 文件里把“-Dfile.encoding=UTF-8”改为“-Dfile.encoding=GBK”
然后重启 Elasticsearch 。
错误会变成:
[2016-11-15 15:34:44,053][WARN ][transport.netty] [Korg] exception caught on transport layer [[id:0x64ce55ac,/127.0.0.1:52049 => /127.0.0.1:9300]],closing connection
java.io.IOException: 远程主机强迫关闭了一个现有的连接。
2.问题分析:一般是客户端断开连接。