[Don't use ElasticSearch yet?] ElasicSearch client-side index, mapping, document basic operation command kibana demo

1. Index operation

1.1 Create an index

##  创建索引
put csdn

return result

{
  "acknowledged" : true,
  "shards_acknowledged" : true,
  "index" : "csdn"
}

1.2 Query index information

##  查询索引
get csdn

return result

{
  "csdn" : {
    "aliases" : { },
    "mappings" : { },
    "settings" : {
      "index" : {
        "creation_date" : "1645948029364",
        "number_of_shards" : "1",
        "number_of_replicas" : "1",
        "uuid" : "tl3tRSlZQCqfM_PH8s7KaQ",
        "version" : {
          "created" : "7080099"
        },
        "provided_name" : "csdn"
      }
    }
  }
}

1.3 delete index

DELETE csdn 

return result

{
  "acknowledged" : true
}

1.4 Modify the index status

## 修改索引状态
 ## 关闭 索引  无法进行读写操作
post csdn/_close 
 ## 重新打开索引
post csdn/_open

return result

{
  "acknowledged" : true,
  "shards_acknowledged" : true
}

1.5 Determine whether the index exists

## 判断索引是否存在
head csdn

return result

-----索引存在---

200 - OK


-----索引不存在---

{"statusCode":404,"error":"Not Found","message":"404 - Not Found"}

2. Mapping operations

2.0 Mapping related parameter description

  1. index determines whether the field can be searched by the user

  2. store determines whether the field is stored

  3. The type of the type field Note that there is no String type after version 5.0, use text and keyword instead

  4. The text type requires word segmentation to be set to text, and does not support aggregation

  5. The keyword type does not need word segmentation to be set to keyword, and supports aggregation

2.1 Add the mapping after creating the index

post csdn/_mapping
{
  "properties":{
    "id":{
      "index":true,
      "store":true,
      "type":"integer"
    },
     "name":{
      "index":true,
      "store":false,
      "type":"text",
      "analyzer":"ik_max_word"
    },
    "age":{
      "index":true,
      "store":false,
      "type":"integer"
    }
  }
}

return result

{
  "acknowledged" : true
}

2.2 Create a mapping when creating an index

put csdn
{
  "mappings":{
     "properties":{
    "id":{
      "index":true,
      "store":true,
      "type":"integer"
    },
     "name":{
      "index":true,
      "store":false,
      "type":"text",
      "analyzer":"ik_max_word"
    },
    "age":{
      "index":true,
      "store":false,
      "type":"integer"
    }
  }
  }
}

return result

{
  "acknowledged" : true,
  "shards_acknowledged" : true,
  "index" : "csdn"
}

2.3 Mapping query

## 根据索引查询映射信息
get csdn/_mapping

return result

{
  "csdn" : {
    "mappings" : {
      "properties" : {
        "age" : {
          "type" : "integer"
        },
        "id" : {
          "type" : "integer",
          "store" : true
        },
        "name" : {
          "type" : "text",
          "analyzer" : "ik_max_word"
        }
      }
    }
  }
}

3. Document Operations

3.1 Adding documents

post csdn/_doc
{
  "id":20,
  "name":"赵六",
  "age":25
}

return result

{
  "_index" : "csdn",
  "_type" : "_doc",
  "_id" : "gPc9On8BcPj1STPlFokb",
  "_version" : 1,
  "result" : "created",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 0,
  "_primary_term" : 1
}

3.2 Querying documents

3.2.1 Query documents based on their unique IDs

get csdn/_doc/gPc9On8BcPj1STPlFokb

return result

{
  "_index" : "csdn",
  "_type" : "_doc",
  "_id" : "gPc9On8BcPj1STPlFokb",
  "_version" : 1,
  "_seq_no" : 0,
  "_primary_term" : 1,
  "found" : true,
  "_source" : {
    "id" : 20,
    "name" : "赵六",
    "age" : 25
  }
}

3.2.2 Query all documents

## 查询所有文档

get csdn/_doc/_search

return result

{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "csdn",
        "_type" : "_doc",
        "_id" : "gPc9On8BcPj1STPlFokb",
        "_score" : 1.0,
        "_source" : {
          "id" : 20,
          "name" : "赵六",
          "age" : 25
        }
      },
      {
        "_index" : "csdn",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "id" : 21,
          "name" : "王五",
          "age" : 11
        }
      }
    ]
  }
}

3.3 Modify the document

## 根据文档id修改文档
put csdn/_doc/gPc9On8BcPj1STPlFokb
{
  "id":20,
  "name":"赵牛修改",
  "age":11  
}

return result

{
  "_index" : "csdn",
  "_type" : "_doc",
  "_id" : "gPc9On8BcPj1STPlFokb",
  "_version" : 2,
  "result" : "updated",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 4,
  "_primary_term" : 1
}

Note: Documents in ES cannot be modified after they are created. To modify, just delete the original document, add a new document, and add one to the version number.

​ If the ID exists, it means modifying the operation. If it does not exist, it means adding the operation.

3.3.1 Querying the document version number

get csdn/_doc/_search?version=true

return result

{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 3,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "csdn",
        "_type" : "_doc",
        "_id" : "1",
        "_version" : 1,
        "_score" : 1.0,
        "_source" : {
          "id" : 21,
          "name" : "王五",
          "age" : 11
        }
      },
      {
        "_index" : "csdn",
        "_type" : "_doc",
        "_id" : "MfdEOn8BcPj1STPlTYra",
        "_version" : 1,
        "_score" : 1.0,
        "_source" : {
          "query" : {
            "match_all" : { }
          }
        }
      },
      {
        "_index" : "csdn",
        "_type" : "_doc",
        "_id" : "gPc9On8BcPj1STPlFokb",
        "_version" : 3,
        "_score" : 1.0,
        "_source" : {
          "id" : 20,
          "name" : "赵牛修改",
          "age" : 11
        }
      }
    ]
  }
}

3.4 Deleting documents

delete csdn/_doc/gPc9On8BcPj1STPlFokb

return result

{
  "_index" : "csdn",
  "_type" : "_doc",
  "_id" : "gPc9On8BcPj1STPlFokb",
  "_version" : 4,
  "result" : "deleted",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 5,
  "_primary_term" : 1
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324149255&siteId=291194637