elasticsearch 命令简介

环境介绍

以下命令都是基于7.2.0版本,不同版本之间可能稍有区别

创建索引

PUT /index_member
{
   "settings" : {
   	  # 分片的数量
      "number_of_shards" : 3,
      # 副本的数量
      "number_of_replicas" : 3
   }
}

删除索引

DELETE /index_member

创建索引的mapping信息

Elasticsearch数据类型及其属性

PUT /index_member/_mapping
{
  "properties": {
    "memberId": {
      "type": "keyword",
      "index": true
    },
    "idNumber": {
      "type": "keyword",
      "index": true
    },
    "birthday": {
      "type": "long"
    },
    "height": {
      "type": "short"
    },
    "weight": {
      "type": "scaled_float",
      "scaling_factor": 100
    }
  }
}

获取索引的mapping信息

GET /index_member/_mapping

使用SQL语句查询

format后面有多重选择,如csv、yaml、cbor、smile等,但我们常用的就jsontxttxt格式方便我们平时直观的观察数据,而json则方便程序的获取。

POST /_sql?format=txt
{
  "query": """SELECT memberId, username, phone, familyId FROM index_member order by phone"""
}

使用DSL语句查询

# 只有query,没有filter
GET /website/article/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "title": "elasticsearch"
          }
        },
        {
          "range": {
            "id": {
              "gte": 18
            }
          }
        }
      ],
      "should": [
        {
          "match": {
            "content": "elasticsearch"
          }
        }
      ],
      "must_not": [
        {
          "match": {
            "author_id": 111
          }
        }
      ]
    }
  }
}
# 带有filter语句
GET /company/employee/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "join_date": "2016-01-01"
          }
        }
      ],
      "filter": {
        "range": {
          "age": {
            "gte": 30
          }
        }
      }
    }
  }
}

修改索引数据类型

原索引

{
  "index_original" : {
    "mappings" : {
      "properties" : {
        "memberId" : {
          "type" : "keyword"
        },
        "username" : {
          "type" : "text"
        }
      }
    }
  }
}

创建一个新索引

PUT /index_temp
{
   "settings" : {
      "number_of_shards" : 3,
      "number_of_replicas" : 3
   },
   "mappings": {
     "properties": {
      "memberId": {
        "type": "long"
      },
      "username": {
        "type": "text"
      }
    }
   }
}

将原索引的数据同步到新索引

POST _reindex                   
{
  "source": {
    "index": "index_original"
  },
  "dest": {
    "index": "index_temp"
  }
}

删除原索引

DELETE index_original

为新索引设置别名

POST /_aliases
{
     "actions": [
         {"add": {"index": "index_temp", "alias": "index_original"}}
     ]
}

也可以再次创建原索引,然后再将数据同步回去。

删除索引别名

POST /_aliases
{
     "actions": [
         {"remove": {"index": "index_temp", "alias": "index_original"}}
     ]
}
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 书香水墨 设计师: CSDN官方博客
应支付0元
点击重新获取
扫码支付

支付成功即可阅读