简单操作elasticsearch(es版本7.6)

简单操作elasticsearch(es版本7.6)

es 官方文档 https://www.elastic.co/guide/index.html

简单操作elasticsearch主要是指管理索引,对数据进行增删改查的操作。通常情况下我们使用es head进行这些操作,也可以通过postman或者是其它的http请求工具进行操作。

以下主要是基于es head操作

1、索引管理

a、创建索引(索引名称必须小写!!!)

请求地址:http://localhost:9200/{IndexName}

http方法:PUT

参数示例(创建address索引):

{
    "settings":{
        "number_of_shards":2,    
        "number_of_replicas":1
    },
    "mappings":{
            "properties":{
                "id":{"type":"keyword"},
                "country":{"type":"keyword"},
                "province":{"type":"keyword"},
                "city":{"type":"keyword"},
                "address1":{"type":"text"},
                "remark":{"type":"text"}
            }
        }
}

其中settings是设置索引分片信息:分片数,副本数。

mapping是设置索引的属性信息(属性名称,类型----es数据类型请参照https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-types.html

es head实现示例::

也可以使用Postman或者其他http请求工具实现:

postman实现示例

 b、查看索引

请求地址:http://localhost:9200/{IndexName}/_mappings

http方法:GET

示例(查看address的mapping):

 c、查看索引的配置(settings)

请求地址:http://localhost:9200/{IndexName}/_settings

http方法:GET

示例(查看address的settings):

2、添加数据

注意:默认情况下添加数据时,如果指定的某个字段不存在时,es会自动创建该字段

a、指定id,不存在则创建,存在则更新

请求地址:http://localhost:9200/{IndexName}/_doc/{id}

http方法:PUT

参数示例:

{
  "country": "CN",
  "province": "广东",
  "City": "广州",
  "address1": "天河区猎德村123"
}

示例(更新address中id为1的数据--存在则更新,不存在则新增):

b、不指定id,直接创建,id由es自动生成

路径:http://localhost:9200/IndexName/_doc

http方法:POST

参数示例:

{

  “country”:"CN",

  "province":"广东",

  “City”:“广州”,

  "address1":"白云区xxx"

}

示例(创建一条新的address记录)

3、查询数据

请求地址:http://localhost:9200/{IndexName}/_search

方法:POST

参数示例

{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "province": "广东"
          }
        }
      ]
    }
  }
}

示例(查询province是“广东”的address记录)

4、修改数据

请求地址:http://localhost:9200/{IndexName}/_update_by_query

方法:POST

参数示例

{
  "script": {
    "source": "ctx._source.province='广东省';ctx._source.address1=''"
  },
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "province": "广东"
          }
        }
      ]
    }
  }
}

示例(更新province是“广东”的记录:修改province为“广东省”、address1为空)

5、删除数据

请求地址:http://localhost:9200/{IndexName}/_delete_by_query

方法:POST

参数示例:

{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "province": "广东省"
          }
        }
      ]
    }
  }
}

  

示例(删除province是“广东省”的记录)

----------------------------

猜你喜欢

转载自www.cnblogs.com/johnyong/p/12873386.html