[关闭]
@1234567890 2018-03-05T09:56:03.000000Z 字数 4012 阅读 1072

elasticsearch

ES


基本操作CRUD

列出所有索引

  1. curl 'localhost:9200/_cat/indices?v'

创建一个索引

  1. curl -XPUT 'localhost:9200/customer?pretty'

上传数据C

  1. curl -XPUT 'localhost:9200/customer/external/1?pretty' -d '{"name":"wht"}'
  2. curl -XPOST 'localhost:9200/customer/external?pretty' -d '{"name":"Jane Doe"}'
  3. 注意,在上面的情形中,由于我们没有指定一个ID,我们使用的是POST而不是PUT

数据查询R

  1. curl -XGET 'localhost:9200/customer/external/1?pretty'
  2. curl -XGET 'localhost:9200/customer/external/_search?pretty' -d '{"query":{"match":{"name":"wht"}}}'
  3. curl -XGET 'localhost:9200/customer/external/_search?pretty' -d '{"query":{"match_all":{}}}'
  4. curl 'localhost:9200/customer/_search?q=*&pretty'

更新文档U

  1. curl -XPOST 'localhost:9200/customer/external/1/_update?pretty' -d '{"doc": { "name":"Jane Doe", "age": 20 }}'
  2. curl -XPOST 'localhost:9200/customer/external/1/_update?pretty' -d '{"script" :"ctx._source.age += 5"}'
  3. 在上面的例子中,ctx._source指向当前要被更新的文档。

删除文档D

  1. curl -XDELETE 'localhost:9200/customer/external/2?pretty'
  2. curl -XDELETE 'localhost:9200/customer/external/_query?pretty' -d '{"query": { "match":{ "name": "John" } }}'
  1. {
  2. "took" : 9,
  3. "timed_out" : false,
  4. "_shards" : {
  5. "total" : 5,
  6. "successful" : 5,
  7. "failed" : 0
  8. },
  9. "hits" : {
  10. "total" : 4,
  11. "max_score" : 1.0,
  12. "hits" : [
  13. {
  14. "_index" : "customer",
  15. "_type" : "external",
  16. "_id" : "1",
  17. "_score" : 1.0,
  18. "_source" : {
  19. "name" : "wht",
  20. "age" : 20
  21. }
  22. },
  23. {
  24. "_index" : "customer",
  25. "_type" : "external",
  26. "_id" : "AVi5SsOlPAt5b1643npc",
  27. "_score" : 1.0,
  28. "_source" : {
  29. "name" : "hhhh3"
  30. }
  31. }
  32. ]
  33. }
  34. }
  35. 对于这个响应,我们看到了以下的部分:
  36. - took —— Elasticsearch执行这个搜索的耗时,以毫秒为单位
  37. - timed_out —— 指明这个搜索是否超时
  38. - _shards —— 指出多少个分片被搜索了,同时也指出了成功/失败的被搜索的shards的数量
  39. - hits —— 搜索结果
  40. - hits.total —— 能够匹配我们查询标准的文档的总数目
  41. - hits.hits —— 真正的搜索结果数据(默认只显示前10个文档)
  42. - _scoremax_score —— 现在先忽略这些字段

细讲查询

只返回第一个文档

  1. curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
  2. {
  3. "query": { "match_all": {} },
  4. "size": 1
  5. }'

返回第11到第20个文档

  1. curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
  2. {
  3. "query": { "match_all": {} },
  4. "from": 10,
  5. "size": 10
  6. }'

账户余额降序排序

  1. curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
  2. {
  3. "query": { "match_all": {} },
  4. "sort": { "balance": { "order": "desc" } }
  5. }'

指定返回的几个字段

  1. curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
  2. {
  3. "query": { "match_all": {} },
  4. "_source": ["account_number", "balance"]
  5. }'

match与match_phrase对比

  1. 返回地址中包含“mill”或者包含“lane”的账户
  2. curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
  3. {
  4. "query": { "match": { "address": "mill lane" } }
  5. }'
  6. match的变体(match_phrase),它会去匹配短语“mill lane
  7. curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
  8. {
  9. "query": { "match_phrase": { "address": "mill lane" } }
  10. }'

布尔查询

  1. 组合查询返回包含“mill”和“lane”的所有的账户
  2. curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
  3. {
  4. "query": {
  5. "bool": {
  6. "must": [
  7. { "match": { "address": "mill" } },
  8. { "match": { "address": "lane" } }
  9. ]
  10. }
  11. }
  12. }'
  13. 返回的是地址中包含“mill”或者“lane”的所有的账户
  14. curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
  15. {
  16. "query": {
  17. "bool": {
  18. "should": [
  19. { "match": { "address": "mill" } },
  20. { "match": { "address": "lane" } }
  21. ]
  22. }
  23. }
  24. }'
  25. 它返回地址中既不包含“mill”,同时也不包含“lane”的所有的账户信息
  26. curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
  27. {
  28. "query": {
  29. "bool": {
  30. "must_not": [
  31. { "match": { "address": "mill" } },
  32. { "match": { "address": "lane" } }
  33. ]
  34. }
  35. }
  36. }'
  37. 40岁以上并且不生活在IDdaho)的人的账户
  38. curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
  39. {
  40. "query": {
  41. "bool": {
  42. "must": [
  43. { "match": { "age": "40" } }
  44. ],
  45. "must_not": [
  46. { "match": { "state": "ID" } }
  47. ]
  48. }
  49. }
  50. }'

_analyze关键字查看分析的过程

  1. curl 'localhost:9200/test/_analyze?analyzer=standard&pretty' -d "xiao ming"

创建mapping

  1. curl -XPUT 'localhost:9200/test?pretty' -d '
  2. {
  3. "mappings": {
  4. "employee": {
  5. "properties": {
  6. "username": {
  7. "type": "text"
  8. },
  9. "password": {
  10. "type": "keyword"
  11. },
  12. "describtion": {
  13. "type": "text"
  14. },
  15. "birthday": {
  16. "type": "date",
  17. "format": "yyyy-MM-dd"
  18. },
  19. "email": {
  20. "type": "text"
  21. },
  22. "ip_addr": {
  23. "type": "ip"
  24. }
  25. }
  26. }
  27. }
  28. }'

数据手动导入

  1. curl -XPOST 'localhost:9200/test/employee/_bulk?pretty' --data-binary @test.db
  2. test.db
  3. {"index":{"_index":"test","_type":"employee","_id":1}}
  4. {"username":"xiao ming","password":"password-111","describtion":"i'm xiaoming,call mingming","birthday":"2010-01-01","email":"123@qq.com","ip_addr":"192.168.0.1"}
  5. {"index":{"_index":"test","_type":"employee","_id":2}}
  6. {"username":"xiao hong","password":"password-222","describtion":"i'm xiao hong,call mingming","birthday":"2010-02-02","email":"222@qq.com","ip_addr":"192.168.0.2"}
  7. {"index":{"_index":"test","_type":"employee","_id":3}}
  8. {"username":"小红","password":"password-333","describtion":"i'm 小红,call 红红","birthday":"2010-03-03","email":"333@163.com","ip_addr":"192.168.0.3"}
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注