ElasticSearch 数据操作
时间:2017-12-11 20:12:17 参考:
环境:
- 系统版本:Win10 64位
- Elasticsearch版本:6.0.0
Elasticsearch 数据操作#
设置字段类型(如果索引已经存在需要删除索引)#
-
设置productID类型为keyword,使之可以进行精确查询。
插入数据#
-
批量插入数据
POST /my_store/products/_bulk { "index": { "_id": 1 }} { "price" : 10, "productID" : "XHDK-A-1293-#fJ3" } { "index": { "_id": 2 }} { "price" : 20, "productID" : "KDKE-B-9947-#kL5" } { "index": { "_id": 3 }} { "price" : 30, "productID" : "JODL-X-1937-#pV7" } { "index": { "_id": 4 }} { "price" : 30, "productID" : "QQPX-R-3956-#aD8"}
更新数据#
-
根据id更新文档部分内容
POST /my_store/products/1/_update {"doc":{ "tags" : ["a","b"], "tagCount" : 2 }} POST /my_store/products/2/_update {"doc":{ "tags" : ["a"], "tagCount" : 1}} POST /my_store/products/3/_update {"doc":{ "tags" : ["b"], "tagCount" : 1}} POST /my_store/products/4/_update {"doc": { "tags" : ["c"], "tagCount" : 1}}
-
增加日期字段
-
增加名字(文本字段)
-
范围查询:range支持 ge(大于)、gte(大于等于)、lt(小于)和 lte(小于等于)。
值查询#
-
根据Id进行过滤查询(过滤查询会缓存,效率高)
-
查询多个值
terms
-
组合过滤条件,多个条件同时满足,包含must(都匹配)、must_not(都不匹配)和should(至少有一个匹配)
-
查询价格等于20,或者产品ID等于
XHDK-A-1293-#fJ3
,同时价格不等于30的数据 -
嵌套boolean,价格等于20或 (价格等于30且productID等于JODL-X-1937-#pV7)的数据
-
-
term实现精确相等,通过指定数量,精确查询数组的值。
范围查询#
-
查询价格在一定范围的数据
GET my_store/products/_search { "query": { "constant_score": { "filter": { "range": { "price": { "gte": 10, "lte": 20 } } } } } }
等价的查询
-
日期范围
-
日期范围同时加计算
字符串范围#
- 字符串范围查询
GET my_store/products/_search
{
"query": {
"constant_score": {
"filter": {"range": {
"name": {
"gte": "a",
"lt": "b"
}
}}
}
}
}
存在查询和缺失查询#
在es中 null
, []
和 [null]
是等价的,都是空,如果字段里面存的是 null
,那么该字段就是一个空字段,文档上不会有该字段。
-
数据初始化
POST /test_exists/posts/_bulk { "index": { "_id": "1" }} { "tags" : ["search"] } { "index": { "_id": "2" }} { "tags" : ["search", "open_source"] } { "index": { "_id": "3" }} { "other_field" : "some data" } { "index": { "_id": "4" }} { "tags" : null } { "index": { "_id": "5" }} { "tags" : ["search", null] }
-
存在查询
-
缺失查询
分析数据#
-
使用 productID的分析器分析
XHDK-A-1293-#fJ3
分析结果:
删除数据#
-
删除索引