安装
docker 安装 es
#新建es的config配置⽂件夹
mkdir -p /data/elasticsearch/config
#新建es的data⽬录
mkdir -p /data/elasticsearch/data
#新建es的plugins⽬录
mkdir -p /data/elasticsearch/plugins
#给⽬录设置权限
chmod 777 -R /data/elasticsearch
#写⼊配置到elasticsearch.yml中, 下⾯的 > 表示覆盖的⽅式写⼊, >>表示追加的⽅式写⼊,但是要确
echo "http.host: 0.0.0.0" >> /data/elasticsearch/config/elasticsearch.yml
#安装es
docker run --name elasticsearch -p 9200:9200 -p 9300:9300 \
-e "discovery.type=single-node" \
-e ES_JAVA_OPTS="-Xms128m -Xmx256m" \
-v /data/elasticsearch/config/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml \
-v /data/elasticsearch/data:/usr/share/elasticsearch/data \
-v /data/elasticsearch/plugins:/usr/share/elasticsearch/plugins \
-d elasticsearch:7.10.1
docker安装kibana
docker run -d --name kibana -e ELASTICSEARCH_HOSTS="http://172.19.141.58:9200" -p 5601:5601 kibana:7.10.1
命令执行
mapping
# 创建/修改 mapping
PUT my_index
{
"mappings": {
"properties": {
"name": {
"type": "text",
"analyzer": "ik_smart"
}
}
}
}
PUT my_index/_mapping
{
"properties": {
"name": {
"type": "text",
"analyzer": "ik_smart"
}
}
}
新增
# 创建索引
POST /users/_doc/1
{
"name": "sni",
"age": 21
}
查询
# 查询所有索引
GET /_cat/indices
# 获取users索引的详情
GET /users
# 获取id=1的索引的数据内容详细信息
GET /users/_doc/1
# 获取id=1的索引的数据内容
GET /users/_source/1
删除
# 删除id=1的数据
DELETE /users/_doc/1
# 删除索引
DELETE /users
修改
# 修改: 数据没有产生变动则不做处理
POST /users/_update/1
{
"doc": {
"age": 22
}
}
# 修改: 数据没有产生变动 也执行处理
POST /users/_doc/1
{
"age": 22
}
搜索-分词
# 根据条件搜索
GET /users/_search
{
"query": {
"match_all": {}
}
}
模糊匹配
不区分大小写
# field = address 模糊匹配 street
GET /users/_search
{
"query": {
"match": {
"address": "street"
}
}
}
精确匹配
GET /users/_search
{
"query": {
"match_phrase": {
"address": "Madison street"
}
}
}
多字段模糊匹配
POST resume/_doc/12
{
"title": "后端⼯程师",
"desc": "多年go语⾔开发经验, 熟悉go的基本语法, 熟悉常⽤的go语⾔库",
"want_learn":"python语⾔"
}
POST resume/_doc/13
{
"title": "go⼯程师",
"desc": "多年开发经验",
"want_learn":"java语⾔"
}
POST resume/_doc/14
{
"title": "后端⼯程师",
"desc": "多年开发经验",
"want_learn":"rust语⾔"
}
# title desc 模糊匹配
GET /resume/_search
{
"query": {
"multi_match": {
"query": "go",
"fields": ["title", "desc"]
}
}
}
# 增加字段权重 ^num
GET /resume/_search
{
"query": {
"multi_match": {
"query": "go",
"fields": ["title^2", "desc"]
}
}
}
match_all
GET /users/_search
{
"query": {
"match_all": {}
}
}
搜索
term
精确查询
注意:当条件输入有大写字母时无法搜索出数据
GET /users/_search
{
"query": {
"term": {
"address": "madison street"
}
}
}
范围查询
# 年龄大于等于20 小于等于30
GET user/_search
{
"query":{
"range": {
"age": {
"gte": 20,
"lte": 30
}
}
}
}
exists
# 查询所以存在age字段的数据
GET user/_search
{
"query": {
"exists": {
"field": "age"
}
}
}
fuzzy模糊查询
# fuzzy模糊查询 拼错单词可以纠错 streat -> street
GET /users/_search
{
"query": {
"fuzzy": {
"address": "streat"
}
}
}
# 在match中开启纠错
GET users/_search
{
"query": {
"match": {
"address": {
"query": "streat",
"fuzziness": 1
}
}
}
}
复合搜索
# must: 必须匹配,查询上下⽂,加分
# should: 应该匹配,查询上下⽂,加分
# must_not: 必须不匹配,过滤上下⽂,过滤
# filter: 必须匹配,过滤上下⽂,过滤
GET /users/_search
{
"query":{
"bool":{
"must":[
],
"should":[
],
"must_not":[
],
"filter":[
],
}
}
}
示例
GET /users/_search
{
"query":{
"bool":{
"must":[
{
"term": {
"state": "tn"
}
},
{
"range": {
"age": {
"gte": 20,
"lte": 30
}
}
}
],
"should": [
{
"match": {
"firstname": "Decker"
}
}
],
"must_not":[
{
"term": {
"gender": "male"
}
}
],
"filter":[
{
"range": {
"age": {
"gte": 25,
"lte": 30
}
}
}
]
}
}
}
批量操作
# 批量操作
POST _bulk
{ "index" : { "_index" : "test", "_id" : "1" } }
{ "field1" : "value1" }
{ "delete" : { "_index" : "test", "_id" : "2" } }
{ "create" : { "_index" : "test", "_id" : "3" } }
{ "field1" : "value3" }
{ "update" : {"_id" : "1", "_index" : "test"} }
{ "doc" : {"field2" : "value2"} }
批量导入数据
mock数据:https://mxshop-files.oss-cn-hangzhou.aliyuncs.com/accounts.json
# 批量导入
POST /users/_bulk
{"index":{"_id":"1"}}
{"account_number":1,"balance":39225,"firstname":"Amber","lastname":"Duke","age":32,"gender":"M","address":"880 Holmes Lane","employer":"Pyrami","email":"amberduke@pyrami.com","city":"Brogan","state":"IL"}
{"index":{"_id":"6"}}
{"account_number":6,"balance":5686,"firstname":"Hattie","lastname":"Bond","age":36,"gender":"M","address":"671 Bristol Street","employer":"Netagy","email":"hattiebond@netagy.com","city":"Dante","state":"TN"}
{"index":{"_id":"13"}}
{"account_number":13,"balance":32838,"firstname":"Nanette","lastname":"Bates","age":28,"gender":"F","address":"789 Madison Street","employer":"Quility","email":"nanettebates@quility.com","city":"Nogal","state":"VA"}
{"index":{"_id":"18"}}
{"account_number":18,"balance":4180,"firstname":"Dale","lastname":"Adams","age":33,"gender":"M","address":"467 Hutchinson Court","employer":"Boink","email":"daleadams@boink.com","city":"Orick","state":"MD"}
批量查询数据
# 批量查询
# index = users
# index = account
GET /_mget
{
"docs": [
{
"_index": "users",
"_id": "1"
},
{
"_index": "account",
"_id": "2"
}
]
}