Elasticsearch的Index API

一、DDL 数据定义(索引的创建与删除)

数据定义语言:Data Definition Language

1. 创建索引

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

2. 删除Index

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

二、DCL 数据控制(索引的配置)

数据控制语言: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"
        }
      }
    }
  }
}'

三、DML 数据操作(文档的增、删、改)

数据操作语言: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"}
}
'

三、DQL 数据查询 (文档的查询)

数据查询语言: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
Copyright Curiouser all right reserved,powered by Gitbook该文件最后修改时间: 2020-06-16 21:35:29

results matching ""

    No results matching ""