幂等(idempotent、idempotence)是一个数学与计算机学概念,常见于抽象代数中。
在编程中一个幂等操作的特点是其任意多次执行所产生的影响均与一次执行的影响相同。幂等函数,或幂等方法,是指可以使用相同参数重复执行,并能获得相同结果的函数。
一般用put做增加索引库,以保证索引库幂等性,post可能给覆盖了索引库
docker镜像引入7.4.2的el和ki,还有中文友好的ik分词器
配置ik扩展词典,禁用词典。。。
自定义扩展分词表 分词器查看ik分词器效果
POST /_analyze
{
"text": "已经有19家分校啦,里面也有高富帅,不知道有没有白富美",
"analyzer": "ik_max_word"
}
GET /_analyze
{
"analyzer": "ik_max_word",
"text": "奥巴马竞选失败"
}
GET /_analyze
{
"analyzer": "ik_max_word",
"text": "特朗普选举失败"
}
POST /heima/_doc
{
"title":"小米手机",
"images":"http://image.leyou.com/12479122.jpg",
"price":2699.00
}
GET /heima/_mapping
给定id增加
POST /heima/_doc/2
{
"title":"小米手机",
"images":"http://image.leyou.com/12479122.jpg",
"price":2699.00
}
按照指定的id查看
GET /heima/_doc/2
GET /heima/_doc/3
按照指定id修改
PUT /heima/_doc/3
{
"title":"锤子手机",
"images":"锤子.jpeg",
"description":"耐用的很",
"price":9998.00
}
给定id删除
DELETE /heima/1
查询所有
GET /heima/_search
{
"query": {
"match_all": {
}
}
}
分词查询 默认or连接
GET /heima/_search
{
"query": {
"match": {
"title": "锤子手机"
}
}
}
分词查询 改成and
GET /heima/_search
{
"query": {
"match": {
"title": {
"operator": "and",
"query": "锤子手机"
}
}
}
}
条件查询
GET /heima/_search
{
"query": {
"term": {
"price": 2699
}
}
}
模糊查询 fuzziness是误差编辑距离
GET /heima/_search
{
"query": {
"fuzzy": {
"title": {
"value": "小米",
"fuzziness": 1
}
}
}
}
范围查询
GET /heima/_search
{
"query": {
"range": {
"price": {
"gte": 2000.0,
"lte": 3000.0
}
}
}
}
布尔查询
GET /heima/_search
{
"query": {
"bool": {
"must": [
{"match": {
"title": "手机"
}
},{"range": {
"price": {
"gte": 3000,
"lte": 10000
}
}}
]
}
}
}
排序
GET /heima/_search
{
"query": {
"match_all": {
}
},
"sort": [
{
"price": {
"order": "desc"
}
}
]
}
POST /hema/_doc
{
"title":{
"type":"keyword",
"value":"hhh"
},
"url":"12345566"
}
GET /hema/_search
{
}
高亮
GET /heima/_search
{
"query": {
"match": {
"title": "手机"
}
},
"highlight": {
"pre_tags": "",
"post_tags": "",
"fields": {
"title": {}
}
}
}
分页
GET /heima/_search
{
"query": {
"match_all": {}
},
"sort": [
{
"price": {
"order": "asc"
}
}
],
"from": 0,
"size": 20
}
筛选字段 # "_source": ["title","price"]
GET /heima/_search
{
"query": {
"match_all": {
}
},
"_source": {
"includes": ["title","images"]
}
}
过滤
GET /heima/_search
{
"query": {
"bool": {
"must": {
"match": {
"title": "手机"
}
},
"filter": [
{
"range": {
"price": {
"gte": 2099,
"lte": 2999
}
}
}
]
}
}
}
批量导入数据 先创建索引库结构
PUT /car
{
"mappings": {
"properties": {
"color": {
"type": "keyword"
},
"make": {
"type": "keyword"
}
}
}
}
开始批量导入
POST /car/_bulk
{ "index": {}}
{ "price" : 10000, "color" : "红", "make" : "本田", "sold" : "2014-10-28" }
{ "index": {}}
{ "price" : 20000, "color" : "红", "make" : "本田", "sold" : "2014-11-05" }
{ "index": {}}
{ "price" : 30000, "color" : "绿", "make" : "福特", "sold" : "2014-05-18" }
{ "index": {}}
{ "price" : 15000, "color" : "蓝", "make" : "丰田", "sold" : "2014-07-02" }
{ "index": {}}
{ "price" : 12000, "color" : "绿", "make" : "丰田", "sold" : "2014-08-19" }
{ "index": {}}
{ "price" : 20000, "color" : "红", "make" : "本田", "sold" : "2014-11-05" }
{ "index": {}}
{ "price" : 80000, "color" : "红", "make" : "宝马", "sold" : "2014-01-01" }
{ "index": {}}
聚合查看价位信息
GET /car/_search
{
"aggs": {
"agg_price": {
"stats": {
"field": "price"
}
}
}
}
以颜色分桶聚合查看车品价位信息
GET /car/_search
{
"size": 0,
"aggs": {
"agg_color": {
"terms": {
"field": "color",
"size": 10
},
"aggs": {
"agg_price": {
"stats": {
"field": "price"
}
}
}
}
}
}
桶内嵌套桶 参看不同颜色车所属哪些制造商
GET /car/_search
{
"size": 0,
"aggs": {
"agg_color": {
"terms": {
"field": "color"
},
"aggs": {
"agg_price": {
"avg": {
"field": "price"
}
},
"aggs":{
"terms":{
"field":"make"
}
}
}
}
}
}