Elasticsearch 8.1 CURD

//新建索引
put index01
// 获取索引
get index01


新建文档不指明ID,文档ID随机生成
POST test1/_doc
{
  "name": "test",
  "age": "30"
}
POST test4/_doc/
{
  "@timestamp": "2099-11-15T13:12:00",
  "message": "GET /search HTTP/1.1 200 1070000",
  "user": {
    "id": "kimchy"
  }
}

_doc 代表文档,新建文档指明ID

POST test1/_doc/1
{
  "name": "jack",
  "age": "30"
}
获取文档 指定ID

GET test1/_doc/1

查找所有文档

GET test1/_search

查找byname
GET test1/_search?q=name:jack

/* 全局更新
PUT test1/_doc/1
{
  "name":"rose",
  "age": "34"
}

//局部更新 
POST test1/_update/1
{
  "doc": {
    "name": "new_name2"
  }

}

新建别名
PUT my-index-02/_alias/my-alias


新建mapping
PUT /test
{
  "settings": {
    "number_of_shards": 1
  },
  "mappings": {
    "properties": {
      "field1": { "type": "text" },
      "name": { "type": "text" }
       }
    }
  }
}

新建index 指明分片 备份
PUT /my-index-02
{
  "settings": {
    "index": {
      "number_of_shards": 3,  
      "number_of_replicas": 2 
    }
  }
}
删除
DELETE customer


批量插入数据
POST _bulk
{ "index" : { "_index" : "test", "_id" : "1" } }
{ "name" : "小明" }


复杂搜索

POST test2/_bulk
{"index":{}}
{"name":"aaron","age":23,"hair":"brown","tags":["apple","orange","banana"]}
{"index":{}}
{"name":"sue","age":19,"hair":"red","tags":["apple","lemon","grape"]}
{"index":{}}
{"name":"sally","age":19,"hair":"blonde","tags":["apple","orange","grape"]}
{"index":{}}
{"name":"george","age":19,"hair":"blonde","tags":["apple","pineapple","grape"]}
{"index":{}}
{"name":"fred","age":69,"hair":"blonde","tags":["apple","banana ","grape"]}



条件查询
GET test2/_search
{
  "query": {
    "match": {
      "hair": "blonde"
    }
  },

}

条件查询 返回固定列
GET test2/_search
{
  "query": {
    "match": {
      "hair": "blonde"
    }
  },
  "_source": ["name","age"]
  
}

条件查询 排序
GET test2/_search
{
  "query": {
    "match": {
      "hair": "blonde"
    }
  },
"sort": [
  {
    "age": {
      "order": "desc"
    }
  }
]
}

分页
GET test2/_search
{
  "query": {
    "match": {
      "hair": "blonde"
    }
  },
"sort": [
  {
    "age": {
      "order": "desc"
    }
  }
],
"from": 0,
"size": 20
}

多条件查询 boolean  (and)
GET test2/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "name": "sally"
          }
        },
        {
          "match": {
            "age": "19"
          }
        }
        
      ]
    }
  }
}
 
 多条件查询 boolean  (or)
GET test2/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "match": {
            "name": "sally"
          }
        },
        {
          "match": {
            "age": "69"
          }
        }
        
      ]
    }
  }
} 


多条件查询 boolean  (not)
GET test2/_search
{
  "query": {
    "bool": {
      "must_not": [
        {
          "match": {
            "name": "sally"
          }
        },
        {
          "match": {
            "age": "19"
          }
        }
        
      ]
    }
  }
}

过滤
获取年龄大于10岁小于20
GET test2/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "name": "sally"
          }
        }
      ],
      "filter": 
        {
          "range": {
            "age": {
              "gt": 10,
              "lt": 20
            }
          }
        }
    }
  }
}


匹配多个条件

多个条件 用空格分开 只要能满足一个条件就能被查到
GET test2/_search
{
  "query": {
    "match": {
      "tags": "apple banana"
    }
  }
}

精确查询



PUT /test
{
  "settings": {
    "number_of_shards": 1
  },
  "mappings": {
    "properties": {
      "name": { "type": "text" },
      "desc": { "type": "keyword" }
       }
    }
  }
PUT test/_doc/1
{
  "name":"张三",
  "desc": "法外狂徒"
}


PUT test/_doc/2
{
  "name":"李四",
  "desc": "守法分子"
}

// keyword 代表是关键字 不会被拆分
GET _analyze
{
  "analyzer": "keyword",
  "text":"法外狂徒"
}

//  split("")分开
GET _analyze
{
  "analyzer": "standard",
  "text":"法外狂徒"
}

keyword 字段不会被分词器解析
GET /test/_search
{
 "query": {
   "term": {
     "desc": {
       "value": "法外狂徒"
     }
   }
 }
}

text 字段分词器解析 含有的都会被查到。
GET /test/_search
{
 "query": {
   "term": {
     "name": {
       "value": "三"
     }
   }
 }
}

//  精确查询
GET /test/_search
{
 "query": {
   "bool": {
     "must": [
       {
         "term": {
           "name": {
             "value": "三"
           }
         }
       },
       {
         "term": {
           "desc": {
             "value": "法外狂徒"
           }
         }
       }
     ]
   }
 }
}


高亮查询

默认标签

GET /test/_search
{
 "query": {
   "match": {
     "name": "张三"
   }
 },
 "highlight": {
   "fields": {
     "name": {}
   }
 }
}

自定义标签
GET /test/_search
{
 "query": {
   "match": {
     "name": "张三"
   }
 },
 "highlight": {
   "pre_tags":"<p class='key' style='red'>", 
   "post_tags": "</p>", 
   "fields": {
     "name": {}
   }
 }
}















  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值