ElasticSearch | Update By Query

2020-05-28  本文已影响0人  乌鲁木齐001号程序员

重建索引 | 使用场景

ElasticSearch | 重建索引的 API

Update By Query | 举几个栗子

准备数据
DELETE blogs/

PUT blogs/_doc/1
{
  "content":"Hadoop is cool",
  "keyword":"hadoop"
}

GET blogs/_mapping
修改 Mapping | 增加子字段 | 使用英文分词器
PUT blogs/_mapping
{
  "properties" : {
    "content" : {
      "type" : "text",
      "fields" : {
        "english" : {
          "type" : "text",
          "analyzer":"english"
        }
      }
    }
  }
}
写入文档
PUT blogs/_doc/2
{
  "content":"Elasticsearch rocks",
    "keyword":"elasticsearch"
}
查询新写入文档
POST blogs/_search
{
  "query": {
    "match": {
      "content.english": "Elasticsearch"
    }
  }
}
查询修改 Mapping 之前写入的文档
POST blogs/_search
{
  "query": {
    "match": {
      "content.english": "Hadoop"
    }
  }
}
_update_by_query | 更新索引中所有文档
POST blogs/_update_by_query
{

}
再次查询修改 Mapping 之前写入的文档
POST blogs/_search
{
  "query": {
    "match": {
      "content.english": "Hadoop"
    }
  }
}

Reindex API | 使用场景

Reindex API | 注意事项

Reindex API | op_type

Reindex API | 跨集群

Reindex API | 异步操作

Reindex API | 举个栗子 | 更新已有索引的字段类型

ElasticSearch 是不允许更新已有索引的字段类型的,如要更新,需新建一个索引,再用 Reindex API 把数据导进去。

查询已有索引 blogs 的 Mapping
GET blogs/_mapping
尝试更改已有索引 blogs 字段的类型
PUT blogs/_mapping
{
    "properties" : {
    "content" : {
      "type" : "text",
      "fields" : {
        "english" : {
          "type" : "text",
          "analyzer" : "english"
        }
      }
    },
    "keyword" : {
      "type" : "keyword"
    }
  }
}
创建新的索引 | 设置索引的 Mapping
DELETE blogs_fix

PUT blogs_fix/
{
  "mappings": {
      "properties" : {
      "content" : {
        "type" : "text",
        "fields" : {
          "english" : {
            "type" : "text",
            "analyzer" : "english"
          }
        }
      },
      "keyword" : {
        "type" : "keyword"
      }
    }    
  }
}
Reindex API | 把现有索引的数据写进新的索引中
POST  _reindex
{
  "source": {
    "index": "blogs"
  },
  "dest": {
    "index": "blogs_fix"
  }
}
在新索引中查询文档
GET  blogs_fix/_doc/1
上一篇下一篇

猜你喜欢

热点阅读