elasticsearch 文档操作

single API

PUT /spider/ # 创建索引
POST /spider/ # 并不能用于创建索引,报错
PUT /spider/movies/1/ # 插入document,索引为1,若数据存在,则为覆盖更新
POST /spider/movies/ # 插入文档,随机生成索引
HEAD /spider/ # 用于验证 索引或document是否存在,若存在则返回200状态码,不存在则返回404.
HEAD /spider/movies/1/
DELETE /spider/ # 删除索引
DELETE /spider/movies/1/ # 删除文档

1.添加数据

PUT /test03/user/1
{
  "name":"huyande",
  "age":26,
  "desc":"我爱北京天安门",
  "tags":["技术宅","温暖","友善"]
}

image-20200717104802278

2.更新数据

PUT
PUT /test03/user/1
{
  "name":"张三",
  "age":26,
  "desc":"我爱北京天安门",
  "tags":["技术宅","温暖","友善"]
}

image-20200717111702229

POST --此方式更零活,可指定修改指定字段
POST /test03/user/1/_update
{
  "doc":{
    "name":"李四"
  }
}

image-20200717112122297

2.查询数据

简单查询–根据id查询

GET /test03/user/1

image-20200717105302042

根据查询条件查询 --query string search

GET /test03/user/_search?q=name:huyande
GET /test_index/test_type/_search?q=+test_field:test #+是必须包含
GET /test_index/test_type/_search?q=-test_field:test #-是不包含

image-20200717110722333

根据查询条件查询–query DSL

DSL:Domain Specified Language,特定领域的语言

http request body:请求体,可以用json的格式来构建查询语法,比较方便,可以构建各种复杂的语法

image-20200717113920510

1.term/terms 过滤

term主要用于精确匹配那些值,比如数字,日期,布尔值或者 not_analyzed的字符串(未经分析的文本数据)

terms 跟 term 有点类似,但 terms 允许指定多个匹配条件。 如果某个字段指定了多个值,那么文档需要一起去做匹配

查询是直接通过倒排索引指定的词条进程精确查找

GET /test03/user/_search
{
 "query": {
    "term":{
    "age":26
  }
 }
}

image-20200717113006981

GET /test03/user/_search
{
 "query": {
    "terms":{
      "age":[26,286]
  }
 }
}

image-20200717113704523

2.range过滤

range过滤允许我们按照指定范围查找一批数据

  1. gt 大于
  2. gte 大于等于
  3. lt 小于
  4. lte 小于等于
GET /test03/user/_search
{
 "query": {
    "range": {
        "age": {
            "gte":  20,
            "lt":   30
        }
    }
 }
}

image-20200717113905494

3.exists 和 missing 过滤

exists 和 missing 过滤可以用于查找文档中是否包含指定字段或没有某个字段,类似于SQL语句中的IS_NULL条件

# 将有name 字段的输出出来
GET /test03/user/_search
{
 "query": {
    "exists": {
        "field": "name"
    }
 }
}

image-20200717141228680

4.bool过滤
  1. must :: 多个查询条件的完全匹配,相当于 and。
  2. must_not :: 多个查询条件的相反匹配,相当于 not。
  3. should :: 至少有一个查询条件匹配, 相当于 or。
# 查询年龄是26岁 name字段不是王的数据
GET /test03/user/_search
{
 "query": {
    "bool": {
      "must": [
        {
          "term": {
            "age": {
              "value": 26
            }
          }
        }
      ],
      "must_not": [
        {
          "term": {
            "name": {
              "value": "王"
            }
          }
        }
      ]
      
    }
 }
}

image-20200717155440532

5.bool查询

bool 查询与 bool 过滤相似,用于合并多个查询子句。不同的是,bool 过滤可以直接给出是否匹配成功, 而bool 查询要计算每一个查询子句的 _score

GET /test03/user/_search
{
 "query": {
    "bool": {
      "must": [
        {
          "match": {
            "name": "王麻子"
          }
        }
      ]      
    }
 }
}


image-20200717163028690

image-20200717163049792

6.bool嵌套查询
#查询 年龄是26岁的 或者 名字叫张三的
GET /test03/user/_search
{
 "query": {
    "bool": {
      "should": [
        {
          "term": {
            "age": {
              "value": "26"
            }
          }
        },
        {
          "bool":{
            "must": [
              {
                "match": {
                  "name": "张三"
                }
              }
            ]
          }
        }
      ]      
    }
 }
}


image-20200717164740086

7.match_all 查询

使用match_all 可以查询所有的文档 ,是没有条件下的默认查询语句

GET /test03/user/_search
{
 "query": {
    "match_all": {}
 }
}

image-20200717165746123

8.match 查询

match查询是一个标准查询,不管你需要全文本查询还是精确查询基本上都要用到它。
如果你使用 match 查询一个全文本字段,它会在真正查询之前用分析器先分析match一下查询字符

GET /test03/user/_search
{
 "query": {
    "match": {
      "desc":"北京天安门"
    }
 }
}


9.multi_match 查询

multi_match查询允许你做match查询的基础上同时搜索多个字段

#在name 和desc字段中搜索 ‘张三在北京’
GET /test03/user/_search
{
  "query": {
	  "multi_match": {		
			"query": "张三在北京",
			"fields": ["name", "desc"],
			"type":    "best_fields"
		}
    }
}
GET /test03/user/_search
{
  "query": {
	  "multi_match": {		
			"query": "张四爱北京",
			"fields": ["name", "desc"],
			"type":    "best_fields",
			"tie_breaker": 0.3
		}
    }
}
#tie_breaker是将其它匹配的查询子句考虑进来也是可能的。通过指定tie_breaker参数将其它每个匹配的子句的分值乘以tie_breaker,以达到取得最佳匹配查询子句的_score
#计算公式
#score=best_field.scoreboost+other_fieldsboost.score*tie_breaker

image-20200717171252429

  • best_fields :最佳字段 返回某一个字段匹配到最多关键字的文档
  • most_fields:多数字段 多数字段匹配成功的得分之和,字段匹配越多,得分越高
  • cross_fields :跨字段 指的是一个唯一标识,跨域了多个字段,比如人的标识是名字,一个建筑的标识是地址,姓名跨域散落在多个field中,比如first_name和last_name,一个地址也可以散落在多个字段中,比如country,province,city中
GET /area/_search
{
	"query": {
		"multi_match": {
			"query": "河南省郑州市",
			"fields": ["province", "city"],
			"type": "cross_fields",
			"operator": "and"
		}
	}
}
#operator使用and,是要返回两者同时匹配成功的结果。
10.match_phrase 查询

短语查询,full text search 是一个词组,意味着三个词的位置是连续且有顺序

GET /test03/user/_search
{
  "query": {
	  "match_phrase": {		
		  "desc":"安门,北京"
		}
  }
}

image-20200717173406267

GET /test03/user/_search
{
    "match_phrase": {
        "title": {
            "query": "full text search",
            "slop":1
        }
    }
}
# slop 参数告诉match_phrase查询词条能够相隔多远时仍然将文档视为匹配。相隔多远的意思是,你需要移动一个词条多少次来让查询和文档匹配

11.phrase_prefix 查询

与词组中最后一个词条进行前缀匹配

类似于百度 中的搜索提示

GET /test03/user/_search
{
  "query": {
	  "match_phrase_prefix": {
      "desc": {
        "query": "北京 哇哦"
      }
    }
  }
}

image-20200717174145282

1、北京 就是去进行match,搜索对应的doc
2、哇哦,会作为前缀,去扫描整个倒排索引,找到所有哇哦开头的doc
3、然后找到所有doc中,即包含北京 ,又包含哇哦开头的字符的doc
4、根据你的slop去计算,看在slop范围内,能不能让北京 哇哦,正好跟doc中的北京和哇哦开头的单词的position相匹配

image-20200717174405753

12.regexp 查询
{
    "query": {
        "regexp": {
            "title": "W[0-9].+" 
        }
    }
}
13.高亮查询
GET /test03/user/_search
{
  "query": {
	  "match": {
	    "name": "王麻子"
	  }
  },
  "highlight": {
    "fields": {
      "name":{}
    }
  }
}

image-20200717174831890

GET /test03/user/_search
{
  "query": {
	  "match": {
	    "name": "王麻子"
	  }
  },
  "highlight": {
    "pre_tags": "<p style='color:red'>",
    "post_tags": "</p>", 
    "fields": {
      "name":{}
    }
  }
}


image-20200717175119674

14.分页查
GET /test03/user/_search
{
  "query": {
	  "match": {
	    "desc": "北京"
	  }
  },
 "from": 0,
  "size": 2
}

image-20200717175319842

15 返回指定字段
GET /test03/user/_search
{
  "_source":["name"],
  "query": {
	  "match": {
	    "desc": "北京"
	  }
  },
 "from": 0,
  "size": 2
}

image-20200717175742588

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

古月_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值