ElasticSearch 数据操做
1.插入数据
URL规则:spa
POST /{索引}/{类型}/{ID}rest
ID:能够不传递,有则使用替代系统的_id,没有则系统生成_id。code
1.1 指定ID
数据:blog
POST /haoke/user/1001 { "id":1001, "name":"张三", "age":20, "sex":"男" }
响应:索引
{ "_index": "haoke", "_type": "user", "_id": "1002", "_version": 1,#版本信息 "result": "created", #结果 "_shards": { "total": 1, "successful": 1, "failed": 0 }, "_seq_no": 0, "_primary_term": 1 }
1.2 不指定id插入数据:
POST /haoke/user { "name":"李四", "age":23, "sex":"男" }
响应:ip
{ "_index": "haoke", "_type": "user", "_id": "e3bPeXUBg0sTC8c2CssI", "_version": 1, "result": "created", "_shards": { "total": 1, "successful": 1, "failed": 0 }, "_seq_no": 1, "_primary_term": 1 }
说明:非结构化的索引,不须要事先建立,直接插入数据默认建立索引。ci
2.更新数据
2.1 覆盖更新
在Elasticsearch中,文档数据是不可修改的,可是能够经过覆盖的方式进行更新。文档
PUT /haoke/user/1001 { "id":1001, "name":"张三", "age":21, "sex":"女" }
更新结果以下:it
{ "_index": "haoke", "_type": "user", "_id": "1001", "_version": 2, #版本进行了+1 "result": "updated", "_shards": { "total": 1, "successful": 1, "failed": 0 }, "_seq_no": 2, "_primary_term": 2 }
注意 _version 增长了1io
2.2 局部更新数据
原理
在内部,依然会查询到这个文档数据,而后进行覆盖操做,步骤以下:
1. 从旧文档中检索JSON
2. 修改它
3. 删除旧文档
4. 索引新文档
示例
#注意:这里多了_update标识 POST /haoke/user/1001/_update { "doc":{ "age":23 } }
结果
{ "_index": "haoke", "_type": "user", "_id": "1001", "_version": 3, "result": "updated", "_shards": { "total": 1, "successful": 1, "failed": 0 }, "_seq_no": 3, "_primary_term": 2 }
4.删除数据
只需发起DELETE请求便可
DELETE /haoke/user/e3bPeXUBg0sTC8c2CssI
{ "_index": "haoke", "_type": "user", "_id": "e3bPeXUBg0sTC8c2CssI", "_version": 2, "result": "deleted", "_shards": { "total": 1, "successful": 1, "failed": 0 }, "_seq_no": 4, "_primary_term": 2 }
状态 "result": "deleted",
再次请求
GET /haoke/user/e3bPeXUBg0sTC8c2CssI { "_index": "haoke", "_type": "user", "_id": "e3bPeXUBg0sTC8c2CssI", "found": false }
注意,result表示已经删除,version 也会加1
删除不存在的数据会返回 result: not_found
{ "_index": "haoke", "_type": "user", "_id": "e3bPeXUBg0sTC8c2CssI", "_version": 1, "result": "not_found", "_shards": { "total": 1, "successful": 1, "failed": 0 }, "_seq_no": 5, "_primary_term": 2 }
说明:
删除一个文档也不会当即从磁盘上移除,它只是被标记成已删除。Elasticsearch将会在你以后添加更多索引的
时候才会在后台进行删除内容的清理
5.搜索数据
5.1 根据id搜索数据
GET /haoke/user/1001 #返回结果 { "_index": "haoke", "_type": "user", "_id": "1001", "_version": 3, "_seq_no": 3, "_primary_term": 2, "found": true, "_source": { "id": 1001, "name": "张三", "age": 23, "sex": "女" } }
5.2 查询所有数据
默认返回10条数据
GET /haoke/user/_search #返回结果 { "took": 2, "timed_out": false, "_shards": { "total": 2, "successful": 2, "skipped": 0, "failed": 0 }, "hits": { "total": { "value": 2, "relation": "eq" }, "max_score": 1.0, "hits": [{ "_index": "haoke", "_type": "user", "_id": "1002", "_score": 1.0, "_source": { "id": 1002, "name": "张三", "age": 20, "sex": "男" } }, { "_index": "haoke", "_type": "user", "_id": "1001", "_score": 1.0, "_source": { "id": 1001, "name": "张三", "age": 23, "sex": "女" } }] } }
5.3 根据关键字搜索数据
GET /haoke/user/_search?q=age:20 #返回结果 { "took": 34, "timed_out": false, "_shards": { "total": 2, "successful": 2, "skipped": 0, "failed": 0 }, "hits": { "total": { "value": 1, "relation": "eq" }, "max_score": 1.0, "hits": [{ "_index": "haoke", "_type": "user", "_id": "1002", "_score": 1.0, "_source": { "id": 1002, "name": "张三", "age": 20, "sex": "男" } }] } }
6.DSL搜索
Elasticsearch提供丰富且灵活的查询语言叫作DSL查询(Query DSL),它容许你构建更加复杂、强大的查询。
DSL(Domain Specific Language特定领域语言)以JSON请求体的形式出现。
6.1 示例1:年龄等于20
POST /haoke/user/_search #请求体 { "query" : { "match" : { "age" : 20 } } } #响应 { "took": 3, "timed_out": false, "_shards": { "total": 2, "successful": 2, "skipped": 0, "failed": 0 }, "hits": { "total": { "value": 1, "relation": "eq" }, "max_score": 1.0, "hits": [{ "_index": "haoke", "_type": "user", "_id": "1002", "_score": 1.0, "_source": { "id": 1002, "name": "张三", "age": 20, "sex": "男" } }] } }
6.2. 示例2:查询年龄大于10岁的男性用户
POST /haoke/user/_search #查询结构体 { "query": { "bool": { "filter": { "range": { "age": { "gt": 10 } } }, "must": { "match": { "sex": "男" } } } } } #返回结构 { "took": 3, "timed_out": false, "_shards": { "total": 2, "successful": 2, "skipped": 0, "failed": 0 }, "hits": { "total": { "value": 3, "relation": "eq" }, "max_score": 0.4700036, "hits": [ { "_index": "haoke", "_type": "user", "_id": "s1MIg3UBapAzpGN_aapD", "_score": 0.4700036, "_source": { "id": 1003, "name": "李四", "age": 20, "sex": "男" } }, { "_index": "haoke", "_type": "user", "_id": "uVMLg3UBapAzpGN_EqoA", "_score": 0.4700036, "_source": { "id": 1004, "name": "王五", "age": 20, "sex": "男" } }, { "_index": "haoke", "_type": "user", "_id": "1002", "_score": 0.2876821, "_source": { "id": 1002, "name": "张三", "age": 20, "sex": "男" } } ] } }
filter: 过滤
must:匹配
6.3 全文搜索
POST /haoke/user/_search # 查询体 { "query":{ "match":{ "name":"张三 李四" } } } # 返回 { "took": 7, "timed_out": false, "_shards": { "total": 2, "successful": 2, "skipped": 0, "failed": 0 }, "hits": { "total": { "value": 3, "relation": "eq" }, "max_score": 1.9616582, "hits": [ { "_index": "haoke", "_type": "user", "_id": "1002", "_score": 1.9616582, "_source": { "id": 1002, "name": "张三", "age": 20, "sex": "男" } }, { "_index": "haoke", "_type": "user", "_id": "1001", "_score": 1.9616582, "_source": { "id": 1001, "name": "张三", "age": 23, "sex": "女" } }, { "_index": "haoke", "_type": "user", "_id": "s1MIg3UBapAzpGN_aapD", "_score": 1.9616582, "_source": { "id": 1003, "name": "李四", "age": 20, "sex": "男" } } ] } }
7.高亮显示
在query下面增长 highlight
POST /haoke/user/_search #查询结构 { "query": { "match": { "name": "张三 李四" } }, "highlight": { "fields": { "name": {} } } } #返回 { "took": 55, "timed_out": false, "_shards": { "total": 2, "successful": 2, "skipped": 0, "failed": 0 }, "hits": { "total": { "value": 3, "relation": "eq" }, "max_score": 1.9616582, "hits": [ { "_index": "haoke", "_type": "user", "_id": "1002", "_score": 1.9616582, "_source": { "id": 1002, "name": "张三", "age": 20, "sex": "男" }, "highlight": { "name": [ "<em>张</em><em>三</em>" ] } }, { "_index": "haoke", "_type": "user", "_id": "1001", "_score": 1.9616582, "_source": { "id": 1001, "name": "张三", "age": 23, "sex": "女" }, "highlight": { "name": [ "<em>张</em><em>三</em>" ] } }, { "_index": "haoke", "_type": "user", "_id": "s1MIg3UBapAzpGN_aapD", "_score": 1.9616582, "_source": { "id": 1003, "name": "李四", "age": 20, "sex": "男" }, "highlight": { "name": [ "<em>李</em><em>四</em>" ] } } ] } }
8.聚合
在Elasticsearch中,支持聚合操做,相似SQL中的group by操做
POST /haoke/user/_search #查询 { "aggs": { "all_interests": { "terms": { "field": "age" } } } } #返回 { "took": 19, "timed_out": false, "_shards": { "total": 2, "successful": 2, "skipped": 0, "failed": 0 }, "hits": { "total": { "value": 6, "relation": "eq" }, "max_score": 1.0, "hits": [ { "_index": "haoke", "_type": "user", "_id": "1002", "_score": 1.0, "_source": { "id": 1002, "name": "张三", "age": 20, "sex": "男" } }, { "_index": "haoke", "_type": "user", "_id": "wFMMg3UBapAzpGN_9aoN", "_score": 1.0, "_source": { "id": 1005, "name": "赵六", "age": 32, "sex": "女" } }, { "_index": "haoke", "_type": "user", "_id": "w1MNg3UBapAzpGN_j6qI", "_score": 1.0, "_source": { "id": 1005, "name": "孙七", "age": 33, "sex": "男" } }, { "_index": "haoke", "_type": "user", "_id": "1001", "_score": 1.0, "_source": { "id": 1001, "name": "张三", "age": 23, "sex": "女" } }, { "_index": "haoke", "_type": "user", "_id": "s1MIg3UBapAzpGN_aapD", "_score": 1.0, "_source": { "id": 1003, "name": "李四", "age": 20, "sex": "男" } }, { "_index": "haoke", "_type": "user", "_id": "uVMLg3UBapAzpGN_EqoA", "_score": 1.0, "_source": { "id": 1004, "name": "王五", "age": 20, "sex": "男" } } ] }, "aggregations": { "all_interests": { "doc_count_error_upper_bound": 0, "sum_other_doc_count": 0, "buckets": [ { "key": 20, "doc_count": 3 }, { "key": 23, "doc_count": 1 }, { "key": 32, "doc_count": 1 }, { "key": 33, "doc_count": 1 } ] } } }
年龄 20有3条,23有1条...