返回介绍

PART Ⅰ : 容器云OPENSHIFT

PART Ⅱ:容器云 KUBERNETES

PART Ⅲ:持续集成与持续部署

PART Ⅴ:日志/监控/告警

PART Ⅵ:基础

PART Ⅶ:数据存储、处理

PART VIII:CODE

PART X:HACKINTOSH

PART XI:安全

index

发布于 2024-06-08 21:16:46 字数 8069 浏览 0 评论 0 收藏 0

数据定义语言:Data Definition Language

1. 创建索引

PUT /index_name?pretty
========================================================
curl -sk -u username:userpassword -XPUT "http://localhost:9200/index_name?pretty"

创建索引时设置参数

curl -sk -u username:userpassword -XPUT "http://localhost:9200/index_name?pretty" \
-H 'Content-Type: application/json' -d'
{
 "settings" : {
   "number_of_replicas" : 0
  }
}'

2. 删除Index

DELET /index_name
========================================================
curl -sk -u username:userpassword -XDELETE "http://127.0.0.1:9200/index_name"

数据控制语言:Data Control Language

1. 查看索引的设置

GET /index_name/_settings
========================================================
curl -sk -u username:userpassword "http://127.0.0.1:9200/index_name/_settings"

2. 查看索引的Mapping

GET /index_name/_mapping
========================================================
curl -sk -u username:userpassword "http://127.0.0.1:9200/index_name/_mapping"

3. 设置索引Mapping

PUT /index_name
{
  "mappings": {
    "index_name": {
      "dynamic":"false",
      "properties": {
        "id": {
            "type": "long"
        },
        "prd_id": {
           "type": "long"
        },
        "mer_id": {
          "type": "long"
        },
        "data_status": {
          "type": "text"
        },
        "datachange_createtime": {
          "type": "date",
          "format": "strict_date_optional_time||epoch_millis"
        },
        "datachange_lasttime": {
          "type": "date",
          "format": "strict_date_optional_time||epoch_millis"
        }
      }
    }
  }
}
========================================================
curl -sk -u username:userpassword -XPUT "http://127.0.0.1:9200/index_name" -H 'Content-Type: application/json' -d'
{
  "mappings": {
    "index_name": {
      "dynamic":"false",
      "properties": {
        "id": {
            "type": "long"
        },
        "prd_id": {
           "type": "long"
        },
        "mer_id": {
          "type": "long"
        },
        "data_status": {
          "type": "text"
        },
        "datachange_createtime": {
          "type": "date",
          "format": "strict_date_optional_time||epoch_millis"
        },
        "datachange_lasttime": {
          "type": "date",
          "format": "strict_date_optional_time||epoch_millis"
        }
      }
    }
  }
}'

数据操作语言:Data Manipulation Language

1. 向索引中插入一个文档

向索引中插入一个ID为1的文档

PUT /index_name/_doc/1 
{
  "name": "test"
} 
========================================================
curl -sk -u username:userpassword \
-XPUT "http://localhost:9200/index_name/_doc/1" \
-H 'Content-Type: application/json' \
-d'{  "name": "test" }'

2. 向索引中批量插入文档

详见Elasticsearch索引文档批量操作

3. 更新指定文档

PUT /index_name/_doc/1?pretty
{
  "doc": {"name": "test1"}
}
========================================================
curl -sk -u username:userpassword \
-XPUT "http://localhost:9200/index_name/_doc/1" \
-H 'Content-Type: application/json' \
-d '
{
  "doc": {"name": "test1"}
}
'

4. 指定文档新增字段

PUT /index_name/_doc/1?pretty
{
  "doc": {"name": "test1","new_field": "testN"}
}
========================================================
curl -sk -u username:userpassword \
-XPUT "http://localhost:9200/index_name/_doc/1" \
-H 'Content-Type: application/json' \
-d '
{
  "doc": {"name": "test1","new_field": "testN"}
}
'

5. 删除文档

curl -sk -u username:userpassword \
-XPOST "http://localhost:9200/index_name/_delete_by_query" \
-H 'Content-Type: application/json' \
-d '
{
  "query":{
    "term":{
        "name" : "test1"
    }
  }
}'

数据查询语言:Data Query Language

1. 查询索引中的所有文档

只显示前十条

GET /index_name/_search?pretty 
========================================================
curl -sk -u username:userpassword "http://localhost:9200/index_name/_search?pretty"

2. 查询_id为1的文档

GET /index_name/_doc/1?pretty
========================================================
curl -sk -u username:userpassword "http://localhost:9200/index_name/_doc/1?pretty"

3. 查询_id为1的文档的元数据

GET index_name/_doc/1/_source
========================================================
curl -sk -u username:userpassword "http://localhost:9200/index_name/_doc/1/_source?pretty"

4. 查询符合指定条件的文档

GET /index_name/_search?q=name:test1
========================================================
curl -sk -u username:userpassword "http://localhost:9200/index_name/_search?q=name:test1"

5. 复杂查询

GET /employee/_doc/_search   
{
    "query" : {
        "bool": {
            "must": {
                "match" : {
                    "last_name" : "smith"
                }
            },
            "filter": {
                "range" : {
                    "age" : { "gt" : 30 }
                }
            }
        }
    }
}
#这条语句翻译成sql:select * from employee where last_name='smith' and age > 40

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文