elasticsearch 数据 添加,更新,删除,查询

匿名 (未验证) 提交于 2019-12-03 00:26:01

elasticsearch 数据 添加,更新,删除,查询


上篇文章说了,elasticsearch mapping字段的增,删,更新。如果把mapping的修改理解成对数据结构的修改,那这篇文章就可以理解成对数据的修改。

1,添加数据

  1. "http://127.0.0.1:9200/ik_v2/chinese/3?pretty"'
  2. {
  3. }'

2,更新数据

2.1,部分数据更新

  1. 'localhost:9200/ik_v2/chinese/3/_update?pretty''{
  2. }'
  3. "http://127.0.0.1:9200/ik_v2/chinese/3?pretty"
  4. "_index""ik_v2"
  5. "_type""chinese"
  6. "_id""3"
  7. "_version"
  8. "found"
  9. "_source"
  10. "id"
  11. "username""testtest"//部分更新了
  12. "description""测试测试"

2.2,全部更新

  1. "http://127.0.0.1:9200/ik_v2/chinese/3?pretty"'
  2. {
  3. }'
  4. //id为3的数据全部更新了
  5. "http://127.0.0.1:9200/ik_v2/chinese/3?pretty"
  6. "_index""ik_v2"
  7. "_type""chinese"
  8. "_id""3"
  9. "_version"
  10. "found"
  11. "_source"
  12. "id"
  13. "username""111111111"
  14. "description""222222222"

2.3,拼接更新

  1. 'localhost:9200/ik_v2/chinese/3/_update?pretty''{
  2. }'
  3. 'localhost:9200/ik_v2/chinese/3/_update?pretty''{
  4. }'
  5. "http://127.0.0.1:9200/ik_v2/chinese/3?pretty"
  6. "_index""ik_v2"
  7. "_type""chinese"
  8. "_id""3"
  9. "_version"
  10. "found"
  11. "_source"
  12. "id"//加了2
  13. "username""111111111"
  14. "description""222222222333"//拼了333

2.4,添加字段,并更新数据

  1. //添加一个字段为sex值为1
  2. 'localhost:9200/ik_v2/chinese/3/_update?pretty''{
  3. }'
  4. //删除sex这个字段
  5. 'localhost:9200/ik_v2/chinese/3/_update?pretty''{
  6. }'

在这里要注意,用这个方法,mapping结构会改变。

注:以上的更新操作都是单条数据更新

2.5,多条数据更新

  1. 'localhost:9200/ik_v2/test/_update_by_query?pretty''{
  2. "took"
  3. "timed_out"
  4. "total"
  5. "updated"//更新了二条
  6. "deleted"
  7. "batches"
  8. "version_conflicts"
  9. "noops"
  10. "retries"
  11. "bulk"
  12. "search"
  13. "throttled_millis"
  14. "requests_per_second"
  15. "throttled_until_millis"
  16. "failures"

注意,这个条件字段,最好不要用分词字段,因为不可控。上面我只是为了测试用。

3,删除数据

3.1,单条删除

  1. "http://127.0.0.1:9200/ik_v2/chinese/3?pretty"

3.2,多条数据删除

  1. 'http://127.0.0.1:9200/ik_v2/_delete_by_query?pretty''{
  2. "took"
  3. "timed_out"
  4. "total"
  5. "deleted"//删除了二条
  6. "batches"
  7. "version_conflicts"
  8. "noops"
  9. "retries"
  10. "bulk"
  11. "search"
  12. "throttled_millis"
  13. "requests_per_second"
  14. "throttled_until_millis"
  15. "failures"

注意,这个条件字段,最好不要用分词字段,因为不可控。

4,查询

  1. "http://127.0.0.1:9200/ik,ik_v2/chinese/_search?pretty"'
  2. {
  3. }
  4. '

查询的操作,非常多,后面会单独的详细说。查询总的来说,支持多索引多字段查询。新版es不支持一个索引多个mapping,老版还支持多mapping查询。

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!