备份代码
# 查看索引
GET /_cat/indices?v
# 创建索引
PUT /products
PUT /orders
{
"settings":{
"number_of_shards":1,
"number_of_replicas":0
}
}
# 删除索引
DELETE /products
# 创建索引
PUT /products
{
"settings":{
"number_of_shards":1,
"number_of_replicas":0
},
"mappings":{
"properties": {
"id":{
"type":"integer"
},
"title":{
"type":"keyword"
},
"create_at":{
"type":"date"
},
"description":{
"type":"text"
}
}
}
}
# 查看 索引映射信息
GET /products/_mapping
# 添加文档操作,手动指定_id
POST /products/_doc/1
{
"id":1,
"title":"冰墩墩",
"price":388,
"create_at":"2022-02-02",
"description":"一墩难求"
}
#添加文档 自动创建文档的_id 55hPJ4ABO8TdR8Be5SCX
POST /products/_doc/
{
"title":"雪融融",
"price":288,
"create_at":"2022-02-02",
"description":"雪融融真可爱"
}
# 文档查询
GET /products/_doc/3
# 删除文档,基于id删除
DELETE /products/_doc/1
# 更新文档,删除原始文档,在重新添加,传入全部字段
PUT /products/_doc/1
{
"id":1,
"title":"雪融融",
"price":288,
"create_at":"2022-02-02",
"description":"雪融融真可爱,抢购一个"
}
# 更新文档,基于指定字段更新
POST /products/_update/1
{
"doc":{
"price":188
}
}
# 文档批量操作
POST /products/_bulk
{"index":{"_id":3}}
{"id":3,"title":"冰墩墩","price":388,"create_at":"2022-02-02", "description":"一墩难求"}
{"index":{"_id":4}}
{"id":4,"title":"雪融融","price":288,"create_at":"2022-02-02", "description":"真可爱"}
POST /products/_bulk
{"index":{"_id":6}}
{"id":6,"title":"cat","price":388,"create_at":"2022-02-02", "description":"lovely cat"}
{"index":{"_id":7}}
{"id":7,"title":"dog","price":288,"create_at":"2022-02-02", "description":"small dog"}
# 文档批量操作,添加、更新、删除
POST /products/_bulk
{"index":{"_id":5}}
{"id":4,"title":"福娃","price":688,"create_at":"2018-02-02", "description":"北京欢迎你"}
{"update":{"_id":3}}
{"doc":{"title":"冰墩墩儿"}}
{"delete":{"_id":4}}
# query DSL 语法
GET /products/_search
{
"query":{
"match_all": {}
}
}
# term 基于关键词查询
# keyword integer double date类型 不分词 全部内容搜索
# text 类型 ES中默认使用标准分词: 中文单字分词,英文单词分词 部分搜索
#
GET /products/_search
{
"query": {
"term": {
"title": {
"value": "雪融融"
}
}
}
}
# 范围查询_range
GET /products/_search
{
"query": {
"range": {
"price": {
"gte": 200,
"lte": 300
}
}
}
}
# 前缀查询
GET /products/_search
{
"query": {
"prefix": {
"title": {
"value": "冰"
}
}
}
}
# 通配符查询[wildcard] ?匹配一个 *匹配多个
GET /products/_search
{
"query": {
"wildcard": {
"description": {
"value": "ca?"
}
}
}
}
#多ids查询 ,查询一组符合条件的id
GET /products/_search
{
"query": {
"ids": {
"values": [1,2,3]
}
}
}
# fuzzy模糊查询 最大模糊错误 0-2之间
# 搜索关键词长度为2不允许存在模糊
# 搜索关键词长度为3-5 允许一次模糊
# 关键词长度大于5 允许最大2模糊
GET /products/_search
{
"query": {
"fuzzy": {
"title": "冰墩儿"
}
}
}
# boolean
GET /products/_search
{
"query": {
"bool": {
"should": [
{
"term": {
"title": {
"value": "冰墩墩"
}
}
}
]
}
}
}
# multi_match
# query 输入关键词 输入一段文本
GET /products/_search
{
"query": {
"multi_match": {
"query": "冰墩墩",
"fields": ["title","description"]
}
}
}
# query_string
GET /products/_search
{
"query": {
"query_string": {
"default_field": "description",
"query": "雪融融"
}
}
}
# 查看 索引映射信息
GET /products/_mapping
# 高亮。高亮的前提时进行分词,type为text
GET /products/_search
{
"query": {
"query_string": {
"default_field": "description",
"query": "雪融融"
}
},
"highlight": {
"fields": {
"*":{}
}
}
}
#自定义高亮
GET /products/_search
{
"query": {
"query_string": {
"default_field": "description",
"query": "雪融融"
}
},
"highlight": {
"pre_tags": ["<span style='color:red;'>"],
"post_tags": ["</span>"],
"require_field_match": "false",
"fields": {
"*":{}
}
}
}
# 指定条数
GET /products/_search
{
"query": {
"query_string": {
"default_field": "description",
"query": "雪融融"
}
},
"highlight": {
"fields": {
"*":{}
}
},
"from": 0,
"size": 10,
"sort": [
{
"price": {
"order": "desc"
}
}
],
"_source": ["id","title","description"]
}
# 索引案例
DELETE /products
PUT /products
{
"mappings" : {
"properties" : {
"description" : {
"type" : "text"
},
"price" : {
"type" : "long"
},
"title" : {
"type" : "keyword"
}
}
}
}
PUT /products/_bulk
{"index":{"_id":1}}
{"title":"蓝月亮洗衣液","price":19.9,"description":"蓝月亮洗衣液很高效"}
{"index":{"_id":2}}
{"title":"iphone13","price":19.9,"description":"很不错的手机"}
{"index":{"_id":3}}
{"title":"小浣熊干脆面","price":1.5,"description":"小浣熊很好吃"}
GET /products/_search
{
"query": {
"match_all": {}
}
}
GET /products/_search
{
"query": {
"term": {
"description": {
"value": "很"
}
}
}
}
POST /_analyze
{
"analyzer": "standard",
"text":"this is a , good Man! 我是一个中国人"
}
POST /_analyze
{
"analyzer": "simple",
"text":"this is a , good Man! 我是 一个中国人"
}
POST /_analyze
{
"analyzer": "whitespace",
"text":"this is a , good Man! 我是 一个中国人"
}
PUT /test
{
"mappings":{
"properties":{
"title":{
"type":"text",
"analyzer":"standard"
}
}
}
}
PUT /test/_doc/1
{
"title":"我是小明,this is good Man"
}
GET /test/_search
{
"query": {
"term": {
"title": {
"value": "good"
}
}
}
}
POST /_analyze
{
"analyzer": "ik_smart",
"text":"中华人民共和国国歌"
}
DELETE /test
PUT /test
{
"mappings":{
"properties":{
"title":{
"type":"text",
"analyzer":"ik_max_word"
}
}
}
}
PUT /test/_doc/1
{
"title":"今天是中华人民共和国成立多少周年,应该放中华人民共和国国歌"
}
GET /test/_search
{
"query": {
"term": {
"title": {
"value": "共和国"
}
}
}
}
POST /_analyze
{
"analyzer": "ik_max_word",
"text":"我是一名矿大学子,矿大教育很棒"
}
# 删除索引
DELETE /products
# 创建索引
PUT /products
{
"settings":{
"number_of_shards":1,
"number_of_replicas":0
},
"mappings":{
"properties": {
"id":{
"type":"integer"
},
"title":{
"type":"keyword"
},
"create_at":{
"type":"date"
},
"description":{
"type":"text",
"analyzer": "ik_max_word"
}
}
}
}
# 文档批量操作
POST /products/_bulk
{"index":{"_id":1}}
{"id":3,"title":"龙猫","price":188,"create_at":"2022-01-02", "description":"龙猫肥宅"}
{"index":{"_id":2}}
{"id":4,"title":"大白","price":88,"create_at":"2022-01-01", "description":"大白真可爱"}
{"index":{"_id":3}}
{"id":3,"title":"冰墩墩","price":388,"create_at":"2022-02-02", "description":"一墩难求"}
{"index":{"_id":4}}
{"id":4,"title":"雪融融","price":288,"create_at":"2022-02-25", "description":"真可爱"}
# filer query 过滤查询
GET /products/_search
{
"query": {
"term": {
"description": {
"value": "可爱"
}
}
}
}
# 过滤查询 term
GET /products/_search
{
"query": {
"bool": {
"must": [
{
"match_all": {}
}
],
"filter": [
{
"term": {
"description": "大白"
}
}
]
}
}
}
# 过滤查询 terms
GET /products/_search
{
"query": {
"bool": {
"must": [
{
"match_all": {}
}
],
"filter": [
{
"terms": {
"description": [
"可爱",
"宅"
]
}
}
]
}
}
}
# 过滤查询 range
GET /products/_search
{
"query": {
"bool": {
"must": [
{
"match_all": {}
}
],
"filter": [
{
"range": {
"price": {
"gte": 100,
"lte": 300
}
}
}
]
}
}
}
# 过滤查询 exist
GET /products/_search
{
"query": {
"bool": {
"must": [
{
"match_all": {}
}
],
"filter": [
{
"exists": {
"field": "description"
}
}
]
}
}
}
# 过滤查询 ids
GET /products/_search
{
"query": {
"bool": {
"must": [
{
"match_all": {}
}
],
"filter": [
{
"ids": {
"values": [
"1",
"2",
"3"
]
}
}
]
}
}
}