java

elasticsearch7.10.2-文档api

1.添加文档

POST my-index-000001/_create/1
{
    "name": "Mary",
    "age":23
}

2.查询文档

GET my-index-000001/_doc/0

3.删除文档

DELETE /my-index-000001/_doc/1

4.条件删除

POST /my-index-000001/_delete_by_query
{
  "query": {
    "match": {
      "name": "Perter"
    }
  }
}

5.更新

POST /<index>/_update/<_id>
示例(更新age字段的值):

POST my-index-000001/_update/1
{
  "script": {
    "source": "ctx._source.age=params.age",
    "lang": "painless",
    "params": {
      "age": 34
    }
  }
}

示例:
POST my-index-000001/_update/1
{
  "doc":{
      "name":"Perter",
      "age":18,
      "job":"stduent"
  }
}

6.批量操作

格式:
POST _bulk
{ "index" : { "_index" : "test", "_id" : "1" } }
{ "field1" : "value1" }
{ "delete" : { "_index" : "test", "_id" : "2" } }
{ "create" : { "_index" : "test", "_id" : "3" } }
{ "field1" : "value3" }
{ "update" : {"_id" : "1", "_index" : "test"} }
{ "doc" : {"field2" : "value2"} }

解释:
create: 如果指定的文档不存在,则对其进行索引。下一行必须包含要建立索引的源数据
delete: 从索引中删除指定的文档
index:  索引指定的文档。如果文档存在,则替换文档并增加版本。下一行必须包含要建立索引的源数据。
udpate: 执行部分文档更新。下一行必须包含部分文档和更新选项。

示例:
POST /my-index-000001/_bulk
{ "index" : { "_index" : "my-index-000001", "_id" : "2" } }
{ "name" : "李四","age":18}
{ "index" : { "_index" : "my-index-000001", "_id" : "3" } }
{ "name" : "王五","age":28}
{ "update" : {"_id" : "1", "_index" : "my-index-000001"} }
{ "doc" : {"name" : "陈六"} }

7.关键词搜索

//首先超级羽绒服关键字先会被分词为超级、羽绒服然后再去es中查询与这两个分词相匹配的文档,依据相关度即分值得到以下结果
GET idx_pro/_search
{
  "query": {
    "match": {
      "name": {
        "query": "超级羽绒服",
        "analyzer": "ik_smart"
      }
    }
  }
}

//不使用配置的话可以采用如下简写方式
{
    "query": {
      "match": {"name": "超级羽绒服"}
    }
}

关于作者

程序员,软件工程师,java, golang, rust, c, python,vue, Springboot, mybatis, mysql,elasticsearch, docker, maven, gcc, linux, ubuntu, centos, axum,llm, paddlepaddle, onlyoffice,minio,银河麒麟,中科方德,rpm