Elasticsearch基础操作

71 阅读1分钟

索引操作

1.创建索引

put /${index_name}
{
    "settings":{
        ...
        }
     "mappings":{
         ...
      }
      
PUT /hotel 
{ "settings" : 
    { "number_of_shards" : 15, //指定主分片个数
        "number_of_replicas" : 2 //指定副分片个数 },
        "mappings":{ "properties":{ … } } }

2.删除索引

delete /hotel

3.关闭索引

post /hotel/_close

4.打开索引

post /hotel/_open

5.索引别名

POST /_aliases { 
    "actions": [ 
        { "add": { //为索引january_log建立别名last_three_month 
            "index": "january_log", 
            "alias": "last_three_month"
            } 
           },
           { "add": { //为索引february_log建立别名last_three_month
           "index": "february_log",
           "alias": "last_three_month" 
           }
           },
           { "add": { //为索引march_log建立别名last_three_month 
           "index": "march_log",
           "alias": "last_three_month" 
           } }
           ] }

映射操作

1.查看映射

GET /hotel/_mapping

2.扩展映射

post /hotel/_mapping{
    "properties":{
        "tag":{  //索引中新增字段tag,类型为keyword
            "type":"keyword"
            }
           }
          }

3.基本数据类型

  • keyword类型:不进行切分的字符串类型,在索引时,对keyword类型的数据不进行切分,直接构建倒排索引,在搜索时,对该类型的查询字符串不进行切分后的部分匹配,keyword类型数据一般用于对文档的过滤,排序和聚合
  • text类型 时可进行切分的字符串类型,在索引时,可按照相应的切词算法对文本内容进行切分,然后构建倒排索引。
  • 数值类型:long,integer,short,byte,double,float,half_float,scaled_float和unsigned_long等
  • 布尔类型 :可以直接使用true或false,或者使用字符串形式的true或false
  • 日期类型: date,日期标准UTC

4.复杂的数据类型

  • 数组类型
  • 对象类型
  • 地理类型
  1. 动态映射

    • 当字段没有定义时,ES可以根据写入的数据自动定义该字段的类型。
  2. 多字段

    • 针对同一个字段,有时候需要不同的数据类型,这通常表现在为了不同的目的以不同的方式索引相同的字段,例如,在订单搜索系统中,既希望能够按照用户姓名进行搜索,又希望按照姓氏进行排列,可以在mapping定义中将姓名字段先后定义为text类型和keyword类型,其中,keyword类型的字段叫作子字段,这样ES在建立索引时会将姓名字段建立两份索引,即text类型的索引和keyword类型的索引。
PUT /hotel_order {
    "mappings": { 
        "properties": { 
            "order_id": { //定义order_id字段类型为keyword 
                "type": "keyword" 
                }, 
             "user_id": { //定义user_id字段类型为keyword 
                 "type": "keyword" 
                 },
              "user_name": { //定义user_name字段类型为text 
                  "type": "text", 
                  "fields": { //定义user_name多字段 //定义user_name字段的子字段user_name_keyword,并定义其类型为keyword 
                  "user_name_keyword": 
                      { "type": "keyword" 
                      } } }, 
                "hotel_id": { //定义hotel_id字段类型为keyword 
                    "type": "keyword" } } } }
                    
                    
                    
GET /hotel_order/_search { 
    "query": { 
        "match": { //match搜索使用text类型的字段 
            "user_name": "Jordan" } }, 
         "sort": { //排序使用子字段 
             "user_name.user_name_keyword": "asc" 
             } }

文档操作

  1. 单条写入文档
    • 在ES中写入文档请求的类型post,其请求形式如下:
    post /hotel/_doc/001
    {//写入的文档数据在url中指定_id,因此在数据中需要指定_id
        "tittle":"好再来酒店",
        "city":"青岛",
        "price": 578.1
     }
    
  2. 批量写入文档
post /_bulk
{"index":{"_index":"hotel","_id":"001"}} //索引
{"tittle":"文雅酒店","city":"北京","price":556.00} //数据
{"index":{"_index":"hotel","_id":"002"}} //索引
{"tittle":"文雅酒店1","city":"北京1","price":556.00} //数据
  1. 更新单条文档
post /hotel/_update/001
  {//需要更新的数据
     "doc":{
         "title":"好再来酒店",
         "city":"北京",
         "price":657.99
}}
  1. 批量更新文档
POST /_bulk //批量请求 
//指定批量更新的索引和文档_id 
{"update":{
    "_index":"${index_name}",
    ”_id”:”${_id}”
    }} 
  {“doc”:{ …}#JSON数据} 
  //设定更新的文档内容 
  //指定批量更新的索引和文档_id 
  {"update":{
  "_index":"${index_name}",
  ”_id”:”${_id}”}} {“doc”:{ …}#JSON数据
  } //设定更新的文档内容
  1. 根据条件更新文档
POST /${index_name}/_update_by_query { 
"query": { //条件更新的查询条件 … }, 
"script": { //条件更新的具体更新脚本代码 … } 
}



POST /hotel/_update_by_query { 
    "query": { //更新文档的查询条件:城市为北京的文档 
        "term": { 
            "city": { "value": "北京" } 
            } 
      }, 
      "script": { //条件更新的更新脚本,将城市改为“上海” 
          "source": "ctx._source['city']='上海'", 
          "lang": "painless" 
        } 
}

6.删除单条文档

DELETE /hotel/_doc/001 7. 批量删除文档

post /_bulk
{"delete":{"_index":"hotel","_id":"0001"}}
{"delete":{"_index":"hotel","_id":"0003"}}
  1. 根据条件删除文档 和条件更新操作类似,有些场景需要根据某些条件同时删除多条数据
post /hotel/_delete_by_query{
    "query":{
        "term":{
            "city":{
                "value":"北京"
                }
           }
        }
    }