zl程序教程

您现在的位置是:首页 >  Java

当前栏目

ES 安装、search、index、doc

2023-02-18 16:46:00 时间

文章目录

1. 安装

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

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

# 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

写入 doc

put product/_doc/1 
{
  "name": "apple",
  "price": 5.6
}

返回

{
  "_index": "product",
  "_id": "1",
  "_version": 3,
  "result": "updated",
  "_shards": {
    "total": 2,
    "successful": 1,
    "failed": 0
  },
  "_seq_no": 2,
  "_primary_term": 1
}

查询 doc

get /product/_doc/1

返回

{
  "_index": "product",
  "_id": "1",
  "_version": 3,
  "_seq_no": 2,
  "_primary_term": 1,
  "found": true,
  "_source": {
    "name": "apple",
    "price": 5.6
  }
}

查看所有 index

get _cat/indices

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

GET kibana_sample_data_logs/_search?from=1&size=2

3. index

创建 index

PUT test_index
{
  "settings":{
    "number_of_shards": 1,
    "number_of_replicas": 1
  }
}

删除 index

DELETE test_index

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

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

POST _reindex
{
  "source": {
    "index": "test_index"
  },
  "dest": {
    "index": "new_test_index"
  }
}

检查 index 是否存在

head new_test_index

4. doc CRUD

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

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

id 1 的 doc 已存在,create 报错

{
  "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

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

POST 可以自动生成 随机 id

POST test_index/_doc/
{
  "name": "test_new"
}
{
  "_index": "test_index",
  "_id": "YrdpU4UBIo5EnYllVY0M",
  "_version": 1,
  "result": "created",
  "_shards": {
    "total": 2,
    "successful": 1,
    "failed": 0
  },
  "_seq_no": 4,
  "_primary_term": 1
}