【第001篇】Elasticsearch使用说明

GET _search
{
  "query": {
    "match_all": {}
  }
}

#方式一ik_max_word
GET /_analyze
{
  "analyzer": "ik_max_word",
  "text": "乒乓球明年总冠军"
}

#方式二ik_smart
GET /_analyze
{
  "analyzer": "ik_smart",
  "text": "乒乓球明年总冠军"
}


#创建索引
PUT person

#查询索引
GET person

GET itheima

GET itcast

#删除
DELETE person

#关闭
POST person/_close

#打开
POST person/_open

#添加映射
PUT /person/_mapping
{
  "properties":{
    "name":{
      "type":"text"
    },
    "age":{
      "type":"integer"
    }
  }
}

#创建索引并添加映射
PUT /itheima
{
  "mappings": {
      "properties":{
    "name":{
      "type":"text"
    },
    "age":{
      "type":"integer"
    }
  }
  }
}

#查询映射
GET /person/_mapping

#添加映射
PUT /person/_mapping
{
    "properties":{
      "address":{
        "type":"text"
      }
  }
}

#添加文档,指定id
PUT /person/_doc/5
{
  "name":"刘大",
  "age":18,
  "address":"石家庄长安区"
}

#添加文档,不指定id
POST /person/_doc
{
  "name":"李四",
  "age":19,
  "address":"石家庄桥西区"
}

#查询指定id的文档
GET /person/_doc/1

#查询所有文档
GET /person/_search

#删除指定id文档
DELETE /person/_doc/1


#词条查询:term
GET /person/_search
{
  "query": {
    "term": {
      "name": {
        "value": "李四"
      }
    }
  }
}

#全文查询:match
GET /person/_search
{
  "query": {
    "match": {
      "address": "石家庄"
    }
  }
}


#批量操作
POST _bulk
{"delete":{"_index":"person","_id":"5"}}
{"create":{"_index":"person","_id":"5"}}
{"name":"王五","age":5,"adress":"新华区"}
{"update":{"_index":"person","_id":"5"}}
{"doc":{"name":"赵六"}}

#删除索引
DELETE goods

#查询索引
GET goods

#创建索引并添加映射
PUT goods
{
	"mappings": {
		"properties": {
			"title": {
				"type": "text",
				"analyzer": "ik_smart"
			},
			"price": { 
				"type": "double"
			},
			"createTime": {
				"type": "date"
			},
			"categoryName": {	
				"type": "keyword"
			},
			"brandName": {	
				"type": "keyword"
			},
	
			"spec": {		
				"type": "object"
			},
			"saleNum": {	
				"type": "integer"
			},
			
			"stock": {	
				"type": "integer"
			}
		}
	}
}

#查询索引
GET goods/_search

#添加数据
POST goods/_doc/1
{
  "title":"小米手机",
  "price":1000,
  "createTime":"2019-12-01",
  "categoryName":"手机",
  "brandName":"小米",
  "saleNum":3000,
  "stock":10000,
  "spec":{
    "网络制式":"移动4G",
    "屏幕尺寸":"4.5"
  }
}

#match_all:查询所有文档
GET goods/_search
{
  "query": {
    "match_all": {}
  },
  "from": 0,
  "size": 20
}

#term:不会对查询条件进行分词
GET goods/_search
{
  "query": {
    "term": {
      "categoryName":"手机"
    }
  }
}

#matchQuery:
#会对查询条件进行分词。 
#然后将分词后的查询条件和词条进行等值匹配 
#默认取并集(OR) 求交集(AND)
GET goods/_search
{
  "query": {
    "match": {
      "title":"华为手机"
    }
  }
}

GET goods/_search
{
  "query": {
    "match": {
      "title":{
        "query": "华为手机",
        "operator": "and"
      }
    }
  }
}

#模糊查询:WildcardQuery 通配符查询
#会对查询条件进行分词。还可以使用通配符?(任意单个字符)和 * (0个或多个字符
GET goods/_search
{
  "query": {
    "wildcard": {
      "title":{
        "value": "华?"
      }
    }
  }
}

#模糊查询:regexpQuery 正则查询
GET goods/_search
{
  "query": {
    "regexp": {
      "title": "\\w+(.)*"
    }
  }
}

#模糊查询:perfixQuery 前缀查询
GET goods/_search
{
  "query": {
    "prefix": {
      "brandName":{
        "value":"三"
      }
    }
  }
}

#1. 范围查询:rangeQuery 查找指定字段在指定范围内包含值
#2. 排序
GET goods/_search
{
  "query": {
    "range": {
      "price":{
        "gte":"2000",
        "lte":"3000"
      }
    }
  },
  "sort": [
    {
      "price": {
        "order": "desc"
      }
    }
  ]
}

#queryString
#会对查询条件进行分词。 
#然后将分词后的查询条件和词条进行等值匹配
# 默认取并集(OR)
# 可以指定多个查#询字段
GET goods/_search
{
  "query": {
    "query_string": {
      "fields": ["title","categoryName","brandName"],
      "query": "华为 AND 手机"
    }
  }
}

GET goods/_search
{
  "query": {
    "simple_query_string": {
      "fields": ["title","categoryName","brandName"],
      "query": "华为 AND 手机"
    }
  }
}

#布尔查询:boolQuery
GET goods/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "brandName": {
              "value": "华为"
            }
          }
        }
      ],
      "filter": {
        "term": {
          "title": "手机"
        }
      }
    }
  }
}

#聚合查询:
# 桶聚合 
GET goods/_search
{
  "query": {
    "match": {
      "title": "华为"
    }
  },
  "aggs": {
    "max_price": {
      "max": {
        "field": "price"
      }
    }
  }
}


#分组查询
GET goods/_search
{
  "query": {
    "match": {
      "title": "华为"
    }
  },
  "aggs": {
    "goods_brands": {
      "terms": {
        "field": "brandName",
        "size": 100
      }
    }
  }
}

#高亮查询
GET goods/_search
{
  "query": {
    "match": {
      "title": "华为"
    }
  },
  "highlight": {
    "fields": {
      "title": {
        "pre_tags": "<font color='red'>",
        "post_tags": "</font>"
      }
    }
  }
}

# -------重建索引------------



# 新建student_index_v1 
PUT student_index_v1
{
  "mappings": {
    "properties": {
      "birthday":{
        "type": "date"
      }
    }
  }
}

GET student_index_v1

PUT student_index_v1/_doc/1
{
  "birthday":"1990-10-03"
}

GET student_index_v1/_search

PUT student_index_v1/_doc/2
{
  "birthday":"1990年10月03日"
}

# 业务变更了需要将birthday字段类型变为text

#1.重新创建索引student_index_v2
PUT student_index_v2
{
  "mappings": {
    "properties": {
      "birthday":{
        "type": "text"
      }
    }
  }
}

GET student_index_v2

#2.将student_index_v1的数据拷贝到student_index_v2
POST _reindex
{
  "source": {
    "index": "student_index_v1"
  },
  "dest": {
    "index": "student_index_v2"
  }
}

GET student_index_v2/_search

PUT student_index_v2/_doc/2
{
  "birthday":"1990年10月03日"
}

#索引别名
#0.先删除student_index_v1
DELETE student_index_v1

#1.给student_index_v2起个别名student_index_v1
POST student_index_v2/_alias/student_index_v1

GET student_index_v1/_search
GET student_index_v2/_search
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

嘉&年华

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值