ES 安装、search、index、doc

2022-12-28 15:14:01 浏览数 (1)

文章目录
  • 1. 安装
  • 2. search
  • 3. index
  • 4. doc CRUD

1. 安装

https://www.elastic.co/cn/

  • 下载 https://www.elastic.co/cn/downloads/past-releases/elasticsearch-8-5-3 https://www.elastic.co/cn/downloads/past-releases/kibana-8-5-3
  • 解压,点击 D:elasticsearch-8.5.3binelasticsearch.bat 启动后会报错
  • 修改配置 "D:elasticsearch-8.5.3configelasticsearch.yml" 配置文件会多出来一些配置

学习环境下,全部改为false即可

代码语言:javascript复制
# Enable security features
xpack.security.enabled: False

xpack.security.enrollment.enabled: False

# Enable encryption for HTTP API client connections, such as Kibana, Logstash, and Agents
xpack.security.http.ssl:
  enabled: False
  keystore.path: certs/http.p12

# Enable encryption and mutual authentication between cluster nodes
xpack.security.transport.ssl:
  enabled: False
  verification_mode: certificate
  keystore.path: certs/transport.p12
  truststore.path: certs/transport.p12
  • 再重新启动 ES 在浏览器输入 http://localhost:9200/ 看见 json,就算安装好了
  • 点击 "D:kibana-8.5.3binkibana.bat" http://localhost:5601/app/dev_tools#/console
  • 测试

写入 doc

代码语言:javascript复制
put product/_doc/1 
{
  "name": "apple",
  "price": 5.6
}

返回

代码语言:javascript复制
{
  "_index": "product",
  "_id": "1",
  "_version": 3,
  "result": "updated",
  "_shards": {
    "total": 2,
    "successful": 1,
    "failed": 0
  },
  "_seq_no": 2,
  "_primary_term": 1
}

查询 doc

代码语言:javascript复制
get /product/_doc/1

返回

代码语言:javascript复制
{
  "_index": "product",
  "_id": "1",
  "_version": 3,
  "_seq_no": 2,
  "_primary_term": 1,
  "found": true,
  "_source": {
    "name": "apple",
    "price": 5.6
  }
}

2. search

查看所有 index

代码语言:javascript复制
get _cat/indices

从 index 中 from 第几个数据开始,size 个docs

代码语言:javascript复制
GET kibana_sample_data_logs/_search?from=1&size=2

3. index

创建 index

代码语言:javascript复制
PUT test_index
{
  "settings":{
    "number_of_shards": 1,
    "number_of_replicas": 1
  }
}

删除 index

代码语言:javascript复制
DELETE test_index

创建完 index 后,主分片数量、index名、字段类型 不可以再修改

重新创建文档,只会带过来 doc,属性设置不会带过来

代码语言:javascript复制
POST _reindex
{
  "source": {
    "index": "test_index"
  },
  "dest": {
    "index": "new_test_index"
  }
}

检查 index 是否存在

代码语言:javascript复制
head new_test_index

4. doc CRUD

创建发生在 主分片(可读可写) 操作类型:

  • index:更新
  • create:只创建,不更新,如果存在相同doc报错
代码语言:javascript复制
PUT test_index/_doc/1?op_type=create
{
  "name": "test1"
}

id 1 的 doc 已存在,create 报错

代码语言:javascript复制
{
  "error": {
    "root_cause": [
      {
        "type": "version_conflict_engine_exception",
        "reason": "[1]: version conflict, document already exists (current version [1])",
        "index_uuid": "ntM1X5SOTxiz8tRVwdHK6g",
        "shard": "0",
        "index": "test_index"
      }
    ],
    "type": "version_conflict_engine_exception",
    "reason": "[1]: version conflict, document already exists (current version [1])",
    "index_uuid": "ntM1X5SOTxiz8tRVwdHK6g",
    "shard": "0",
    "index": "test_index"
  },
  "status": 409
}

index 操作,替换 doc

代码语言:javascript复制
PUT test_index/_doc/1?op_type=index
{
  "name": "test_new"
}
代码语言:javascript复制
{
  "_index": "test_index",
  "_id": "1",
  "_version": 2,
  "result": "updated",
  "_shards": {
    "total": 2,
    "successful": 1,
    "failed": 0
  },
  "_seq_no": 1,
  "_primary_term": 1
}

POST 可以自动生成 随机 id

代码语言:javascript复制
POST test_index/_doc/
{
  "name": "test_new"
}
代码语言:javascript复制
{
  "_index": "test_index",
  "_id": "YrdpU4UBIo5EnYllVY0M",
  "_version": 1,
  "result": "created",
  "_shards": {
    "total": 2,
    "successful": 1,
    "failed": 0
  },
  "_seq_no": 4,
  "_primary_term": 1
}

0 人点赞