索引twitter中put _id 为1的如果存在则更新_id为1的,不存在则添加_id为1的文档
PUT twitter/_doc/1
{
"user": "GB",
"uid": 1,
"city": "Beijing",
"province": "Beijing",
"country": "China"
}
POST twitter/_doc/1
{
"user": "GB",
"uid": 1,
"city": "Beijing",
"province": "Beijing",
"country": "China"
}
如果已经文档已经存在,则抛出异常,不存在则添加
PUT twitter/_create/1
{
"user": "GB",
"uid": 1,
"city": "Beijing",
"province": "Beijing",
"country": "China"
}
上面的命令与当前命令的效果是一样的,op_type的值必须是index,create,其他值会抛出异常
PUT twitter/_doc/1?op_type=create
{
"user": "GA",
"uid": 1,
"city": "Beijing",
"province": "Beijing",
"country": "China"
}
插入数据时不定义Id,则es自动会生成一个id .必须使用post,使用put会报错
POST my-index/_doc
{
"user": "GA",
"uid": 1,
"city": "Beijing",
"province": "Beijing",
"country": "China"
}
#此命令会报错
PUT my-index/_doc
{
"user": "GA",
"uid": 1,
"city": "Beijing",
"province": "Beijing",
"country": "China"
}
查看twitter索引下的id为1的文档
GET twitter/_doc/1
#参看twitter索引下_id为1的_source部分
GET twitter/_doc/1/_source
#在es7之后,在tyoe最终要被废弃的情况下,使用如下命令获取_source
GET twitter/_source/1
PUT twitter/_doc/2
{
"user" : "双榆树-张三",
"message" : "今儿天气不错啊,出去转转去",
"uid" : 2,
"age" : 20,
"city" : "北京",
"province" : "北京",
"country" : "中国",
"address" : "中国北京市海淀区",
"location" : {
"lat" : "39.970718",
"lon" : "116.325747"
}
}
#只查询_id为2的_source字段中的city,age,province
GET twitter/_source/2?_source=city,age,province
一次性请求多个文档,docs为数组格式,_source可以指定获取_source的部分字段
GET _mget
{
"docs": [
{
"_index": "twitter",
"_id": 1,
"_source": [
"user",
"city"
]
},
{
"_index": "twitter",
"_id": 2,
"_source": [
"user",
"city"
]
}
]
}
#批量id查询文档,是上面的简化版
GET twitter/_mget
{
"ids":[1,2]
}
使用这种方式来更新文档的话会将数据全部覆盖,如果我们只修改其中某几个字段的话,这种方式就会显得不太方便
PUT twitter/_doc/1
{
"user": "GB",
"uid": 1,
"city": "Beijing",
"province": "Beijing",
"country": "China"
}
#使用post _update 更新少量字段
POST twitter/_update/1
{
"doc": {
"user": "AB",
"uid": 1
}
}
GET twitter/_doc/1
#如果我们不知道文档的id,应该如何修改呢.就像关系型数据库那样.根据条件进行修改,es也提供了这样的接口
# query 构建查询条件,script确定要修改的字段和修改的值
POST twitter/_update_by_query
{
"query":{
"match":{
"user":"AB"
}
},
"script":{
"source":"ctx._source.city = params.city;ctx._source.uid=params.uid;",
"long":"painless",
"params":{
"city":"22",
"uid":3
}
}
}
如果字段名为中文,修改的话需要一个中括号并 escape 引号的方式来操作。
PUT my_china_index/_doc/1
{
"签到状态":"已签到",
"地点":"上海"
}
GET my_china_index/_doc/1
POST my_china_index/_update_by_query
{
"query":{
"match":{
"地点":"上海"
}
},
"script":{
"source":"ctx._source[\"签到状态\"]=params[\"签到状态\"]",
"long":"painless",
"params":{
"签到状态":"未签到"
}
}
}
使用_update接口使用ctx['_op'] 来达到删除一个文档的目的
GET twitter/_doc/1
POST twitter/_update/1
{
"script": {
"source": """
if(ctx._source.uid==3){
ctx.op='delete'
}else{
ctx.op='none'
}
""",
"lang" : "painless"
}
}
doc_as_upsert 开启存在则更新,不存在新增,upsert则是不存在新增时所添加的字段
GET twitter/_doc/4
POST twitter/_update/4
{
"doc": {
"user" : "GB",
"uid" : 2,
"city" : "Beijing",
"province" : "Beijing",
"country" : "China"
},
"upsert":{
"user":"ab"
}
}
#检查文档的存在
HEAD twitter/_doc/1
#删除文档
DELETE twitter/_doc/1
根据条件删除
GET twitter/_mget
{
"ids": [
"1",
"2",
"3",
"4"
]
}
POST twitter/_delete_by_query
{
"query": {
"match": {
"user":"GB"
}
}
}
批量处理数据(index可以更新和创建,update只更新,create只创建。delete删除)
POST _bulk
{"index":{"_index":"twitter","_id":1}}
{"user":"双榆树-张三","message":"今儿天气不错啊,出去转转去","uid":2,"age":20,"city":"北京","province":"北京","country":"中国","address":"中国北京市海淀区","location":{"lat":"39.970718","lon":"116.325747"}}
{"index":{"_index":"twitter","_id":2}}
{"user":"东城区-老刘","message":"出发,下一站云南!","uid":3,"age":30,"city":"北京","province":"北京","country":"中国","address":"中国北京市东城区台基厂三条3号","location":{"lat":"39.904313","lon":"116.412754"}}
{"index":{"_index":"twitter","_id":3}}
{"user":"东城区-李四","message":"happy birthday!","uid":4,"age":30,"city":"北京","province":"北京","country":"中国","address":"中国北京市东城区","location":{"lat":"39.893801","lon":"116.408986"}}
{"index":{"_index":"twitter","_id":4}}
{"user":"朝阳区-老贾","message":"123,gogogo","uid":5,"age":35,"city":"北京","province":"北京","country":"中国","address":"中国北京市朝阳区建国门","location":{"lat":"39.718256","lon":"116.367910"}}
{"index":{"_index":"twitter","_id":5}}
{"user":"朝阳区-老王","message":"Happy BirthDay My Friend!","uid":6,"age":50,"city":"北京","province":"北京","country":"中国","address":"中国北京市朝阳区国贸","location":{"lat":"39.918256","lon":"116.467910"}}
{"index":{"_index":"twitter","_id":6}}
{"user":"虹桥-老吴","message":"好友来了都今天我生日,好友来了,什么 birthday happy 就成!","uid":7,"age":90,"city":"上海","province":"上海","country":"中国","address":"中国上海市闵行区","location":{"lat":"31.175927","lon":"121.383328"}}
#查询全部文档
POST twitter/_search
#查询数量
GET twitter/_count
GET twitter/_search?size=2&from=2
GET twitter/_search
{
"query": {
"match": {
"user": "刘吴"
}
},
"aggregations":{
"top_authors":{
"terms": {
"field": "author"
}
}
}
}
搜索全部文档,但只返回前1
GET /_search?size=1
#搜索义twit开头的索引
POST /twit*/_search
通过filter_path 来筛选要展示的字段
GET twitter/_search?filter_path=hits.hits._index
GET twitter/_search?filter_path=hits.hits
{
"_source":["user","city"],
"query": {
"match_all": {
}
}
}
GET twitter/_search
{
"query": {
"match": {
"user": "张"
}
}
}
GET twitter/_search
{
"_source":{
"includes": ["use*"],
"excludes": ["u*"]
} ,
"query": {
"match_all": {}
}
}
GET twitter/_search
{
"script_fields": {
"years_to_100": {
"script": {
"lang": "painless",
"source": "100-params._source.age"
}
}
}
}
GET twitter/_search
{
"script_fields": {
"years_to_100": {
"script": "100-params._source.age"
}
}
}
GET twitter/_count
{
"query": {
"match": {
"city": "北京"
}
}
}
修改settings
GET my_index_shard/_settings
PUT my_index_shard
{
"settings": {
"number_of_replicas": 5,
"number_of_shards": 2
}
}
修改mapping
DELETE twitter
PUT twitter
{
"settings": {
"number_of_shards": 1,
"number_of_replicas": 1
}
}
GET twitter/_mapping
GET twitter/_search
{
"query": {
"match": {
"city": "北京"
}
}
}
GET twitter/_search
{
"query": {
"script": {
"script":{
"source": "doc['city.keyword'].contains(params.name)",
"lang": "painless",
"params": {
"name":"北京"
}
}
}
}
}
将_score过滤 city=北京 代表模糊搜索,city.keyword=北京代表精确搜索
GET twitter/_search
{
"query": {
"bool": {
"filter":{
"term": {
"city.keyword": "北京"
}
}
}
}
}
minimum_should_match 标识最少匹配3个字及以上才可以匹配,operator默认为or
GET twitter/_search
{
"query": {
"match": {
"user": {
"query": "朝阳区-老贾",
"operator": "or",
"minimum_should_match": 3
}
}
}
}
"operator": "and" 代表 query条件要全部匹配
GET twitter/_search
{
"query": {
"match": {
"user": {
"query": "朝阳区-老贾",
"operator": "and"
}
}
}
}
在多个字段中匹配‘朝阳’, address^3 将address 含有“朝阳”的权重*3倍,权重在前的先显示 _score
POST twitter/_search
{
"query": {
"multi_match": {
"query": "朝阳",
"fields": [
"user",
"address^3",
"message"
],
"type": "best_fields"
}
}
}
查询user 前缀为朝的文档
POST twitter/_search
{
"query": {
"prefix": {
"user": {
"value": "朝"
}
}
}
}
使用term查询text类型存储的中文,会因为默认分词器的原因导致中文查询不到
POST twitter/_search
{
"query": {
"term": {
"user.keyword": {
"value": "朝阳区-老贾"
}
}
}
}
#使用terms精确匹配
POST twitter/_search
{
"query": {
"terms": {
"user.keyword": [
"朝阳区-老贾",
"东城区-老刘"
]
}
}
}
PUT /job-candidates
{
"mappings": {
"properties": {
"name": {
"type": "keyword"
},
"programming_languages": {
"type": "keyword"
},
"required_matches": {
"type": "long"
}
}
}
}
PUT /job-candidates/_doc/1?refresh
{
"name": "Jane Smith",
"programming_languages": [ "c++" ],
"required_matches": 2
}
PUT /job-candidates/_doc/2?refresh
{
"name": "Jason Response",
"programming_languages": [ "java", "php" ],
"required_matches": 2
}
定义匹配的数量
GET /job-candidates/_search
{
"query": {
"terms_set":{
"programming_languages":{
"terms":[ "c++", "java", "php" ],
"minimum_should_match_field":"required_matches"
}
}
}
}
GET /job-candidates/_search
{
"query": {
"terms_set": {
"programming_languages": {
"terms": [
"c++",
"java",
"php"
],
"minimum_should_match_script": {
"source": "2"
}
}
}
}
}
复合查询
POST /_search
{
"query": {
"bool": {
"must": [
{
"match": {
"city":"北京"
}
},
{
"match": {
"age": "30"
}
}
]
}
},
"explain" : true
}
POST twitter/_search
{
"query": {
"bool": {
"must_not": [
{
"match": {
"city":"北京"
}
}
]
}
}
}
must的条件必须满足,should可以满足也可以不满足 条件满足排名在前
POST /_search
{
"query": {
"bool": {
"must": [
{
"match": {
"age": "30"
}
}
],
"should": [
{
"match_phrase": {
"message": "birthday"
}
}
]
}
}
}
进行位置查询 geo_distance 排序
POST twitter/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"address": "北京"
}
}
]
}
},
"post_filter": {
"geo_distance": {
"distance": "5km",
"location": {
"lat": 39.920086,
"lon": 116.454182
}
}
},
"sort": [
{
"_geo_distance": {
"location": {
"lat": 39.920086,
"lon": 116.454182
},
"order": "asc",
"unit": "km"
}
}
]
}
范围查询排序
GET twitter/_search
{
"query": {
"range": {
"age": {
"gte": 30,
"lte": 40
}
}
},
"sort": [
{
"age": {
"order": "desc"
}
}
]
}
将存在city字段不为空的文档查询出来
GET twitter/_search
{
"query": {
"exists": {
"field": "city"
}
}
}
#与上面相反,将city字段为空的查询出来
GET twitter/_search
{
"query": {
"bool": {
"must_not": [
{
"exists": {
"field": "city"
}
}
]
}
}
}
匹配短语允许短语之间可以有1个token "slop": 1
GET twitter/_search
{
"query": {
"match_phrase": {
"message":{
"query": "Happy birthday",
"slop": 1
}
}
}
}