#拉取镜像
docker pull elasticsearch:6.5.4
#创建容器
docker create --name elasticsearch -e "discovery.type=single-node" -p 9200:9200 elasticsearch:6.5.4
#启动
docker start elasticsearch
#拉取镜像
docker pull mobz/elasticsearch-head:5
#创建容器
docker create --name elasticsearch-head -p 9100:9100 mobz/elasticsearch-head:5
#启动容器
docker start elasticsearch-head
由于前后端分离开发,所以会存在跨域问题,需要在服务端做CORS的配置,如下:
docker exec -it elasticsearch /bin/bash
cd config/
vi elasticsearch.yml
http.cors.enabled: true
http.cors.allow-origin: "*"
Elasticsearch-head 请求头 Content-Type header 设置
docker exec -it elasticsearch-head /bin/bash
vi /usr/src/app/_site/vendor.js
apt-get update
apt-get install vim -y
vi /usr/src/app/_site/vendor.js
:set number 开启行号 :6886 跳到6886行
修改:
1.第6886行 /contentType: "application/x-www-form-urlencoded
改为 contentType: "application/json;charset=UTF-8"
2.第7574行 var inspectData = s.contentType === "application/x-www-form-urlencoded" &&
改为 var inspectData = s.contentType === "application/json;charset=UTF-8" &&
docker restart elasticsearch-head
插入数据
POST http://127.0.0.1:9200/haoke/user/1001
{
"id": 1001,
"name": "张p",
"age": 18,
"sex": "女"
}
POST http://127.0.0.1:9200/haoke/user/1002
{
"id": 1002,
"name": "张三",
"age": 20,
"sex": "男"
}
搜索 GET http://127.0.0.1:9200/haoke/user/_search?q=age:20
DSL搜索:match,bool,must,term,terms,range,exists, 高亮显示,聚合搜索 见文档
查询结果处理 _source只返回原始数据,?_source=id,name只返回指定列 GET http://127.0.0.1:9200/haoke/user/1001/_source?_source=id,name
判断文档是否存在 HEAD http://127.0.0.1:9200/haoke/user/1002
批量操作
批量插入(注意最后一行的回车):POST http://127.0.0.1:9200/haoke/user/_bulk
{"create":{"_index":"haoke","_type":"user","_id":2001}}
{"id":2001,"name":"name1","age": 20,"sex": "男"}
{"create":{"_index":"haoke","_type":"user","_id":2002}}
{"id":2002,"name":"name2","age": 20,"sex": "男"}
{"create":{"_index":"haoke","_type":"user","_id":2003}}
{"id":2003,"name":"name3","age": 20,"sex": "男"}
批量删除(注意最后一行的回车) POST http://127.0.0.1:9200/haoke/user/_bulk
{"delete":{"_index":"haoke","_type":"user","_id":2001}}
{"delete":{"_index":"haoke","_type":"user","_id":2002}}
{"delete":{"_index":"haoke","_type":"user","_id":2003}}
批量操作一次请求多少性能最高?
一个好的批次最好保持在5-15MB大小间
分页
Elasticsearch接受from 和size 参数
size: 结果数,默认10
from: 跳过开始的结果数,默认0
注意:网络搜索引擎中任何语句不能返回多于1000个(深度分页的问题)
明确指定列类型(略)
内置分词:Standard,Simple,Whitespace,Stop,Keyword
中文分词:IK