ES 中简要对比关系
ES 分词器
standard:单字切分
ik_smart:最少拆分
ik_max_word:最细切分
POST _analyze
{
"analyzer": "standard",
"text": "我爱你JAVA!"
}
GET _analyze
{
"analyzer": "ik_smart",
"text": "张三老铁学习java!"
}
GET _analyze
{
"analyzer": "ik_max_word",
"text": "张三老铁学习java!"
}
查看索引
GET _cat/indices?v&s=index:asc
创建映射
如果 index是false。不支持查询,默认是true
PUT product
{
"mappings": {
"properties": {
"product": {
"mappings": {
"properties": {
"brandId": {
"type": "keyword"
},
"brandName": {
"type": "keyword"
},
"id": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
"index": false
}
}
},
"keyword": {
"type": "text",
"analyzer": "ik_max_word"
},
"origin": {
"type": "text",
"analyzer": "ik_max_word"
},
"price": {
"type": "float"
},
"title": {
"type": "text",
"analyzer": "ik_max_word"
}
}
}
}
}
}
}
查询映射
GET product/_mapping
POST 、PUT写入文档
1. PUT、GET、DELETE是幂等的,由于同一条这样的指令,执行多次结果都一样。POST非幂等
2.POST不用加具体的id,它是作用在一个集合资源之上的(/uri),而PUT操作是作用在一个具体资源之上的(/uri/xxx)
POST /product/_doc
{
"id" : "145454",
"title" : "999",
"price" : 0.90,
"origin" : "陕西省",
"brandId" : "2",
"brandName" : "锤子",
"keyword" : "锤子手机"
}
PUT /product/_doc/MM7k1YAB3fJ6uCuW5sKW
{
"id" : "16666",
"title" : "999",
"price" : 0.90,
"origin" : "陕西省",
"brandId" : "2",
"brandName" : "锤子",
"keyword" : "锤子手机"
}
查询当前索引下的数据
ES7.X type 的类型默认都是 "_doc",以下是无条件查询
GET /product/product/_search
{
"query": {
"match_all": {}
}
}
GET /product/_search
{
"query": {
"match_all": {}
}
}
_id 文档的id
基本查询
GET /product/_search
GET /product/_search?q=id:"16666"
GET /product/_doc/MM7k1YAB3fJ6uCuW5sKW
GET /product/_search?q=price:"2799.99"
GET /product/_search?q=price:>"2799.99"
GET /product/_search?q=price:"2799.99"&q=id:3
GET /product/_search?q=price:"2799.99"&q=id:3&q=brandName:"小米"
GET /product/_search?q=price["10" TO "2799.99"]
GET /product/_search?q=price["10" TO "2799.99"]&sort=price:desc
GET /product/_search?q=price["10" TO "2799.99"]&from=0&size=1
GET /product/_search?q=price["10" TO "2799.99"]&from=0&size=1&_source=title,price,origin
_bulk 批量操作文档
POST _bulk
{"create":{"_index":"product","_type":"product","_id":"30"}}
{"id":"30","title":"小米note 4g手机","price":2799.99,"origin":"广东省深圳市","brandId":"3","brandName":"小米","keyword":"小米note手机"}
{"create":{"_index":"product","_type":"product","_id":"50"}}
{"id":"50","title":"小米note 4g手机","price":2799.99,"origin":"广东省深圳市","brandId":"3","brandName":"小米","keyword":"小米note手机"}
POST _bulk
{"update":{"_index":"product","_type":"product","_id":"30"}}
{"id":"30","title":"小米note 4g手机","price":2799.99,"origin":"广东省深圳市","brandId":"3","brandName":"小米","keyword":"小米note手机"}
{"create":{"_index":"product","_type":"product","_id":"50"}}
{"id":"50","title":"小米note 4g手机","price":2799.99,"origin":"广东省深圳市","brandId":"3","brandName":"小米","keyword":"小米note手机"}
POST _bulk
{"delete":{"_index":"product","_type":"product","_id":"30"}}
{"id":"30","title":"小米note 4g手机","price":2799.99,"origin":"广东省深圳市","brandId":"3","brandName":"小米","keyword":"小米note手机"}
{"create":{"_index":"product","_type":"product","_id":"50"}}
{"id":"50","title":"小米note 4g手机","price":2799.99,"origin":"广东省深圳市","brandId":"3","brandName":"小米","keyword":"小米note手机"}
_mget 批量获取文档
GET _mget
{
"docs": [{
"_index": "product",
"_type": "_doc",
"_id": 1
}, {
"_index": "article",
"_type": "_doc",
"_id": 999
}]
}
GET /product/_mget
{
"docs": [{
"_type": "_doc",
"_id": 1
}, {
"_type": "_doc",
"_id": 2
}]
}
GET /product/_doc/_mget
{
"docs": [{
"_id": 1
}, {
"_id": 2
}]
}
term 精确匹配
GET /product/_search
{
"query": {
"term": {
"id": "1"
}
}
}
match模糊匹配
GET /product/_search
{
"from": 0,
"size": 3,
"query": {
"match": {
"keyword": "手机"
}
}
}
multi_match 多条件模糊匹配
GET /product/_search
{
"from": 0,
"size": 3,
"query": {
"multi_match": {
"query": "手机",
"fields": ["title","keyword"]
}
}
}
query_string 含AND与OR条件
GET /product/_search
{
"from": 0,
"size": 3,
"query": {
"multi_match": {
"query": "广州 OR 长沙",
"fields": ["title","keyword"]
}
}
}
范围值
gte 、lte 范围值
from、size 分页参数
_source 只查询的字段
sort 排序
POST /product/_search
{
"query": {
"range": {
"price": {
"gte": 1,
"lte": 50000
}
}
},
"from": 0,
"size": 2,
"_source": ["id", "price", "title", "origin"],
"sort": {
"price": "desc"
}
}
fiter
过滤器方式查询,它的查询不会计算相关性分值,也不会对结果进行排序,因此效率会高一点,查询的结果可以被缓存
GET /product/_search
{
"query": {
"bool": {
"filter": [
{
"wildcard": {
"id": "*1*"
}
}
]
}
}
}
GET _search
{
"query": {
"bool": {
"filter": [
{
"wildcard": {
"id": "*1*"
}
}
]
}
}
}
对已存在的mapping映射进行修改
具体方法
1)如果要推倒现有的映射, 你得重新建立一个静态索引
2)然后把之前索引里的数据导入到新的索引里
3)删除原创建的索引
4)为新索引起个别名, 为原索引名
- 重新建索引
PUT product_update
{
"mappings": {
"properties": {
"product": {
"mappings": {
"properties": {
"brandId": {
"type": "keyword"
},
"brandName": {
"type": "keyword"
},
"id": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
"index": false
}
}
},
"keyword": {
"type": "text",
"analyzer": "ik_max_word"
},
"origin": {
"type": "text",
"analyzer": "ik_max_word"
},
"price": {
"type": "float"
},
"title": {
"type": "text",
"analyzer": "ik_max_word"
"index": false
}
}
}
}
}
}
}
- 将数据导入新的索引
POST _reindex {
"source": {
"index": "product"
},
"dest": {
"index": "product_update"
}
}
- 删除原来的索引
DELETE /product
- 取别名
PUT /product_update/_alias/product_test