首页   快速返回

elasticsearch5.0文档查询更新API
文章分类 elasticsearch
发布时间 2019-01-23 修改时间 2019-01-23
根据原文翻译整理
https://www.elastic.co/guide/en/elasticsearch/reference/5.0/docs-update-by-query.html


The update-by-query API is new and should still be considered experimental. 
The API may change in ways that are not backwards compatible

按查询更新API是新的,还是实验性的。API以不向后兼容的方式更改


POST twitter/_update_by_query
{
  "script": {
    "inline": "ctx._source.likes++",
    "lang": "painless"
  },
  "query": {
    "term": {
      "user": "kimchy"
    }
  }
}

ctx.op   noop  delete

Note that we stopped specifying conflicts=proceed. 
In this case we want a version conflict to abort the process so we can handle the failure.

不指定conflicts=proceed。
版本冲突时中止流程,以便处理失败。




Pick up a new property
Say you created an index without dynamic mapping, filled it with data, 
and then added a mapping value to pick up more fields from the data:

禁用动态映射,写入数据 ,然后增加一个字段

PUT test

{
  "mappings": {
    "test": {
      "dynamic": false,   
      "properties": {
        "text": {"type": "text"}
      }
    }
  }
}


"dynamic": false 禁用动态映射

查看mapping 
http://127.0.0.1:9200/test

{
    "test": {
        "aliases": {},
        "mappings": {
            "test": {
                "dynamic": "false",
                "properties": {
                    "text": {
                        "type": "text"
                    }
                }
            }
        },
        "settings": {
            "index": {
                "creation_date": "1548227359508",
                "number_of_shards": "5",
                "number_of_replicas": "1",
                "uuid": "YGsusI2yRd6TO2kmWxWlJQ",
                "version": {
                    "created": "5000299"
                },
                "provided_name": "test"
            }
        }
    }
}

索引数据


POST test/test?refresh
{
  "text": "words words",
  "flag": "bar"
}
POST test/test?refresh
{
  "text": "words words",
  "flag": "foo"
}

查看所有数据
http://127.0.0.1:9200/test/test/_search

2条记录


根据条件查询
POST http://127.0.0.1:9200/test/test/_search
{
  "query": {
    "match": {
      "flag": "foo"
    }
  }
}

没有符合条件的记录

修改映射 增加字段 flag

PUT test/_mapping/test
{
  "properties": {
    "text": {"type": "text"},
    "flag": {"type": "keyword"}
  }
}

查看新的 mapping 

http://127.0.0.1:9200/test

{
    "aliases": {},
    "mappings": {
        "test": {
            "dynamic": "false",
            "properties": {
                "flag": {
                    "type": "keyword"
                },
                "text": {
                    "type": "text"
                }
            }
        }
    },
    "settings": {
        "index": {
            "creation_date": "1548227359508",
            "number_of_shards": "5",
            "number_of_replicas": "1",
            "uuid": "YGsusI2yRd6TO2kmWxWlJQ",
            "version": {
                "created": "5000299"
            },
            "provided_name": "test"
        }
    }
}


POST test/_update_by_query?refresh&conflicts=proceed
{
  "query": {
    "match": {
      "flag": "foo"
    }
  }
}

这个无法重新索引 

{
  "query": {
    "match_all": {
      
    }
  }
}


再次根据条件查询
POST http://127.0.0.1:9200/test/test/_search
{
  "query": {
    "match": {
      "flag": "foo"
    }
  }
}
可查到一条记录

上一篇     下一篇
kafka这些年

elasticsearch5.0文档查询删除API

elasticsearch5.0文档更新API

elasticsearch5.0批量读取API

elasticsearch5.0批量更新API

elasticsearch5.0重建索引API