目 录CONTENT

文章目录

Elasticsearch 的Index 使用

小张的探险日记
2021-09-15 / 0 评论 / 0 点赞 / 481 阅读 / 4,536 字 / 正在检测是否收录...
温馨提示:
本文最后更新于 2021-09-15,若内容或图片失效,请留言反馈。部分素材来自网络,若不小心影响到您的利益,请联系我们删除。

Elasticsearch 的Index 使用

1.新建索引-基础用法

put student
响应:
{
  "acknowledged" : true,
  "shards_acknowledged" : true,
  "index" : "student"
}

2.新建索引时,指定mapping 和 settings

put teacher
{
  "mappings" : {
    "properties" : {
      "uname":{
        "type" : "keyword"
      },
      "age" : {
        "type" : "integer"
      } 
    }
  },
  "settings" : {
    "index": {
      "number_of_shards" : 2,  //指定分片数量
      "number_of_replicas": 3  //指定副本数量
    }
  }
}

响应
{
  "acknowledged" : true,
  "shards_acknowledged" : true,
  "index" : "teacher"
}

2.1先建索引 再自定义mapping

PUT student/_mapping

{
    "properties" : {
      "name" : {
        "type" : "keyword"
      }
    }
}
响应:
{
  "acknowledged" : true
}

2.2 先建索引 再修改副本数量

​ 先建索引 再修改分片数量

注意:当副本完成创建后才会响应(可能耗时较长),不用等待,过一段时间再去查询即可

分片数量不可修改,必须在创建 索引时指定,创建后无法修改

put student/_settings
{
  "index":{
    "number_of_replicas" : 10
  }
}
响应
{
  "acknowledged" : true
}

3.保存数据

​ 如果保存数据时没有对应的 _mapping 那么,会自动添加

post student/_doc
{
  "name":"test1",
  "age":18
}
响应:
{
  "_index" : "student",
  "_type" : "_doc",
  "_id" : "G3XyhHoBJAYO3TLVdZYH",  //自动产生
  "_version" : 1,
  "result" : "created",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 0,
  "_primary_term" : 1
}

4.手动指定 _id

_id
post student/_doc/1
{
  "name":"test3",
  "age":18
}
响应
{
  "_index" : "student",
  "_type" : "_doc",
  "_id" : "1",  //手动指定
  "_version" : 1,
  "result" : "created",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 1,
  "_primary_term" : 1
}

5.支持保存不一样的数据结构 在 同一个index

post student/_doc
{
  "name":"test11",
  "age":18,
  "bank": 2000
}
响应:
{
  "_index" : "student",
  "_type" : "_doc",
  "_id" : "HHXzhHoBJAYO3TLV1pb2",
  "_version" : 1,
  "result" : "created",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 2,
  "_primary_term" : 1
}

6.搜索所有

get student/_search
响应:
{
  "took" : 143,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 3,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "student",
        "_type" : "_doc",
        "_id" : "G3XyhHoBJAYO3TLVdZYH",
        "_score" : 1.0,
        "_source" : {
          "name" : "test1",
          "age" : 18
        }
      },
      {
        "_index" : "student",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "name" : "test3",
          "age" : 18
        }
      },
      {
        "_index" : "student",
        "_type" : "_doc",
        "_id" : "HHXzhHoBJAYO3TLV1pb2",
        "_score" : 1.0,
        "_source" : {
          "name" : "test11",
          "age" : 18,
          "bank" : 2000
        }
      }
    ]
  }
}

7.按条件搜索-精确搜索

get student/_search
{
  "query":{
    "match": {
      "name": "test1"
    }
  }
}
响应:
{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 0.9808291,
    "hits" : [
      {
        "_index" : "student",
        "_type" : "_doc",
        "_id" : "G3XyhHoBJAYO3TLVdZYH",
        "_score" : 0.9808291,
        "_source" : {
          "name" : "test1",
          "age" : 18
        }
      }
    ]
  }
}

8.查询指定索引的 mapping信息

get student
响应:
{
  "student" : {
    "aliases" : { },
    "mappings" : { },
    "settings" : {
      "index" : {
        "routing" : {
          "allocation" : {
            "include" : {
              "_tier_preference" : "data_content"
            }
          }
        },
        "number_of_shards" : "1",
        "provided_name" : "student",
        "creation_date" : "1625727655112",
        "number_of_replicas" : "1",
        "uuid" : "djai_laJT2m5fGaE5vIoOg",
        "version" : {
          "created" : "7120199"
        }
      }
    }
  }
}

9.删除索引

delete student
响应:
{
  "acknowledged" : true
}

10.查看所有索引

GET _cat/indices?v

11,索引模糊搜索

GET _cat/indices/s*?v
0

评论区