Elasticsearch REST API

#ES REST API

#设置高亮
#过滤器不会计算相关度的得分,所以它们在计算上更快一些
#过滤器可以被缓存到内存中,这使得在重复的搜索查询上,其要比相应的查询快出许多


#查看集群健康
GET /_cluster/health?pretty

#查看集群节点列表
GET /_cat/nodes?v&pretty

#查看分片
GET /_cat/shards?v&pretty

#查看所有索引
GET /_cat/indices?v&pretty

#查看所有类型
GET /_mapping?pretty

#新建index
PUT /test_index

#删除index
DELETE /test_index

#查看分词结果语句
GET /test_index/_analyze
{
  "tokenizer": "whitespace",
  "text": ["You're the 1st runner home!"]
}

#新增记录
PUT /test_index/user/2
{
  "name":"zhang san",
  "title":"大厨",
  "desc":"程序员厨子"
}

#新增记录不指定id
POST /test_index/user
{
  "name":"李四",
  "title":"大厨",
  "desc":"程序员厨子"
}

#查看记录
GET /test_index/user/1?pretty

#删除记录
DELETE /test_index/user/2

#更新记录
PUT /test_index/user/7
{
  "name":"小7啊",
  "title":"大厨",
  "desc":"厨子",
  "age":19
}

#查看所有记录
GET /test_index/user/_search?pretty

#查看所有记录
GET /test_index/_search?q=*&pretty

#查看所有记录
GET /test_index/_search?pretty
{
  "query": {
    "match_all": {}
  }
}

#查看所有记录(默认返回10条记录)
GET /tem_course_dis/_search?pretty

#全文搜索
GET /test_index/user/_search?pretty
{
  "query": {
    "match": {
      "desc": "程序"
    }
  }
}

#全文搜索(size设置条数)
GET /test_index/user/_search?pretty
{
  "query": {
    "match": {
      "desc": "程序"
    }
  },
  "size": 2
}

#全文搜索(from指定位移)
GET /test_index/user/_search?pretty
{
  "query": {
    "match": {
      "desc": "程序"
    }
  },
  "from": 2, 
  "size": 2
}

#全文搜索(多个关键字,默认认为是or关系)
GET /test_index/user/_search?pretty
{
  "query": {
    "match": {
      "desc": "程序员 厨子"
    }
  }
}

#全文搜索(布尔查询:and)
GET /test_index/user/_search?pretty
{
  "query": {
    "bool": {
      "must": [
        {"match": {
          "desc": "程序员"
        }},
        {"match": {
          "desc": "厨子"
        }}
      ]
    }
  }
}
GET /test_index/user/_search?pretty
{
  "query": {
    "bool": {
      "must_not": [
        {"match": {
          "desc": "厨子"
        }}
      ]
    }
  }
}
#全文搜索(布尔查询:or)
GET /test_index/user/_search?pretty
{
  "query": {
    "bool": {
      "should": [
        {"match": {
          "desc": "厨子"
        }}
      ]
    }
  }
}
#全文搜索(范围)
GET /test_index/user/_search?pretty
{
  "query": {
    "bool": {
      "filter": {
        "range": {
          "age": {
            "gte": 10,
            "lte": 20
          }
        }
      }
    }
  }
}

#全文搜索(排序:asc正序;desc倒序)
GET /test_index/user/_search?pretty
{
  "query": {
    "match_all": {}
  },
  "sort": [
    {
      "age": {
        "order": "asc"
      }
    }
  ]
}

#全文搜索(显示有用字段值)
GET /test_index/user/_search?pretty
{
  "query": {
    "match_all": {}
  },
  "_source": "name"
}
GET /test_index/user/_search?pretty
{
  "query": {
    "match_all": {}
  },
  "_source": ["name", "age"]
}

#全文搜索(匹配短语)
GET /test_index/user/_search?pretty
{
  "query": {
    "match_phrase": {
      "desc": "程序员厨子"
    }
  }
}

#全文搜索(复合查询)
GET /test_index/user/_search?pretty
{
  "query": {
    "bool": {
      "must_not": [
        {"match": {
          "desc": "厨子"
        }}
      ], 
      "filter": {
        "range": {
          "age": {
            "gte": 15,
            "lte": 34
          }
        }
      }
    }
  }
}

#全文搜索(聚合)
#size: 查询条数,这里设置为0,因为我们不关心搜索到的数据,只关心聚合结果,提高效率
#aggs:声明这是一个聚合查询,是aggregations的缩写
#popular_colors:给这次聚合起一个名字,任意。
#terms:划分桶的方式,这里是根据词条划分
#field:划分桶的字段
#桶
POST /test_index/_search?pretty
{
  "size": 2, 
  "aggs": {
    "group_by_state": {
      "terms": {
        "field": "age"
      }
    }
  }
}
#桶内度量
POST /test_index/_search?pretty
{
  "size": 0, 
  "aggs": {
    "group_by_state": {
      "terms": {
        "field": "age"
      },
      "aggs": {
        "avg_age": {
          "avg": {
            "field": "age"
          }
        }
      }
    }
  }
}
#桶内嵌套桶

#划分桶的方式
#Date Histogram Aggregation:根据日期阶梯分组,例如给定阶梯为周,会自动每周分为一组
#Histogram Aggregation:根据数值阶梯分组,与日期类似
#Terms Aggregation:根据词条内容分组,词条内容完全匹配的为一组
#Range Aggregation:数值和日期的范围分组,指定开始和结束,然后按段分组

#阶梯分组(参数min_doc_count为1,来约束最少文档数量为1,这样文档数量为0的桶会被过滤)
GET /test_index/_search?pretty
{
  "size": 0,
  "aggs": {
    "age": {
      "histogram": {
        "field": "age",
        "interval": 5,
        "min_doc_count": 1
      }
    }
  }
}

#全文搜索(使用sql查询)
POST /_xpack/sql/translate
{
  "query":"SELECT * FROM test_index"
}

#批处理(新增)
POST /test_index/user/_bulk?pretty
{"index":{"_id":"7"}}
{"name":"小七"}
{"index":{"_id":"9"}}
{"name":"小九"}

#批处理(更新/删除)
POST /test_index/user/_bulk?pretty
{"update":{"_id":"7"}}
{"doc":{"name":"小7啊"}}
{"delete":{"_id":"9"}}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值