基础操作
# 添加数据
# PUT 索引名/类型/文档id
PUT a1/doc/1
{
"name": "阿飞的小姨妈",
"age": 18
}
PUT a1/doc/2
{
"name": "阿飞的二姨妈",
"age": 16
}
PUT a1/doc/3
{
"name": "阿飞的老姨妈",
"age":16
}
# 获取文档中的一条数据
# GET 索引名/类型/文档id
GET a1/doc/1
# 获取文档中的所有数据
GET a1/doc/_search
# 删除一条
# DELETE 索引名/类型/文档id
DELETE a1/doc/3
# 删除所有
DELETE a1/
# 更新一条数据
# POST 索引名/类型/文档id/_update
POST a1/doc/1/_update
{
"doc":{
"age": 20
}
}
# 查询所有的索引
GET _cat/indices?v
# head确认索引是否存在
HEAD a1 # 返回200,不存在返回404
查询的两种方式
- query string
- DSL结构化查询
# 查询年龄为18的
GET a1/doc/_search?q=age:18
GET a1/doc/_search
{
"query":{
"match":{
"age":18
}
}
}
match系列查询
GET a1/doc/_search
{
"query":{
"match":{
"name":"张磊"
}
}
}
# 查询包含白和可爱的,查询的结果为包含一个字和两字的,只要在这三个字里面的任意一个有都会返回
GET a1/doc/_search
{
"query":{
"match":{
"tag":"白 可爱"
}
}
}
match_phrase短语查询
# 查询可爱,这个不会拆分,按整体查询
GET a1/doc/_search
{
"query":{
"match_phrase":{
"tag":"可爱"
}
}
}
# 特殊参数 slop ,表示间隔,比如 中国是世界,我们查询的时候输入中国世界,查询结果没有对应的返回数据。但是我们加上slop:1,表示跳过1个间隔去匹配
GET a1/doc/_search
{
"query":{
"match_phrase":{
title:{
"query":"中国世界",
"slop": 1
}
}
}
}
match_all查询所有
GET a1/doc/_search
{
"query":{
"match_all":{}
}
}
排序查询 sort
# 插入测试数据
PUT s22/doc/1
{
"name":"张磊",
"age":26,
"desc": "大保健,shen不好",
"tag": ["篮球","上午"],
"both": "1990-11-12",
"city":"河北"
}
PUT s22/doc/2
{
"name":"罗新宇",
"age":26,
"desc": "小帅x,shen不好",
"tag": ["针灸","爱推拿"],
"both": "1988-12-12",
"city":"山东"
}
PUT s22/doc/3
{
"name":"苏守丽",
"age":18,
"desc": "大美妞,天生丽质难自弃",
"tag": ["白","fu","美","可爱"],
"both": "1999-08-15",
"city":"山西"
}
PUT s22/doc/4
{
"name":"崔雪飞",
"age":18,
"desc": "大美女,天生丽质难自弃",
"tag": ["白","fu","美","乖巧"],
"both": "1999-08-15",
"city":"辽宁"
}
PUT s22/doc/5
{
"name":"he明明",
"age":25,
"desc": "大美女,天生丽质难自弃",
"tag": ["白","fu","美","乖巧"],
"both": "1999-08-15",
"city":"河北"
}
PUT s22/doc/6
{
"name":"何青青",
"age":28,
"desc": "大美女,天生丽质难自弃",
"tag": ["白","fu","美","乖巧"],
"both": "1999-08-15",
"city":"河北"
}
# 按年龄降序排序 sort排序
GET s22/doc/_search
{
"query": {
"match_all": {}
},
"sort": [
{
"age": {
"order": "desc"
}
}
]
}
分页查询 from和size
# 分页 from和size 从什么开始返回几条数据
GET s22/doc/_search
{
"query": {
"match_all": {}
},
"from": 0,
"size": 2
}
布尔查询
# must(and)
# should(or)
# must_not(not)
# filter
# must
# 查询city是河北的
GET s22/doc/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"city": "河北"
}
}
]
}
}
}
# 查询city是河北的,并且年龄是26的
GET s22/doc/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"city": "河北"
}
},
{
"match": {
"age": 26
}
}
]
}
}
}
# should 或者
# 查询city是河北,或者年龄是26的人
GET s22/doc/_search
{
"query": {
"bool": {
"should": [
{
"match": {
"city": "河北"
}
},
{
"match": {
"age": 26
}
}
]
}
}
}
# must_not
# 查询city不是河北,年龄又不是18的
GET s22/doc/_search
{
"query": {
"bool": {
"must_not": [
{
"match": {
"city": "河北"
}
},
{
"match": {
"age": "18"
}
}
]
}
}
}
# filter
# 查询city是河北,年龄大于25的
GET s22/doc/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"city": "河北"
}
}
],
"filter": {
"range": {
"age": {
"gt": 25
}
}
}
}
}
}
结果过滤 _source
# 查询city是河北,年龄大于25的city字段结果
GET s22/doc/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"city": "河北"
}
}
],
"filter": {
"range": {
"age": {
"gt": 25
}
}
}
}
},
"_source": ["city"]
}
高亮查询
# 高亮显示丽质
# 默认
GET s22/doc/_search
{
"query": {
"match": {
"desc": "丽质"
}
},
"_source": ["desc"],
"highlight": {
"fields": {
"desc": {}
}
}
}
# 自定义高亮显示
GET s22/doc/_search
{
"query": {
"match": {
"desc": "丽质"
}
},
"_source": ["desc"],
"highlight": {
"pre_tags": "<b style='color: green'>", # 前半段
"post_tags": "</b>", # 后半段
"fields": {
"desc": {}
}
}
}
聚合函数
# 聚合函数 max avg min sum
GET s22/doc/_search
{
"query": {
"match_all": {}
},
"aggs": {
"my_avg": {
"avg": {
"field": "age"
}
}
}
}
分组查询
# 分组
GET s22/doc/_search
{
"query": {
"match_all": {}
},
"size": 0,
"aggs": {
"avg_group": {
"range": {
"field": "age",
"ranges": [
{
"from": 15,
"to": 20
},
{
"from":20,
"to":25
},
{
"from":25,
"to":30
}
]
}
}
}
}