教程列表
ElasticSearch入门教程【一】- 简介
ElasticSearch入门教程【二】- 安装
ElasticSearch入门教程【三】- Head插件
ElasticSearch入门教程【四】- 基本用法
ElasticSearch入门教程【五】- TransportClient客户端
ElasticSearch入门教程【六】- spring-boot-starter-data-elasticsearch
Elasticsearch
为开发者提供了丰富的基于Http协议的
RESTful API
,通过简单的Rest请求,就可以实现非常强大的功能。
RESTful API
基本格式如下,以http
方法决定动作:GET/PUT/POST/DELETE
http://<ip>:<port>/<索引>/<类型>/<文档id>
这里我们使用Postman
向Elasticsearch
发送RESTful API
请求。
创建一个名为people
的索引,其中包含一个名为student
的类型,该类型有四个属性,姓名name、地址address、年龄age、生日birthday
请求信息:
URL: http://192.168.255.150:9200/people
METHOD: PUT
Content-Type: application/json
REQUEST:
{
"settings": {
"number_of_shards": "5",
"number_of_replicas": "1"
},
"mappings": {
"student": {
"properties": {
"name": {
"type": "text"
},
"address": {
"type": "keyword"
},
"age": {
"type": "integer"
},
"birthday": {
"type": "date",
"format": "yyyy-MM-dd HH:mm:ss||epoch_millis"
}
}
}
}
}
id需在在url中手动指定,例如指定id为1,插入文档到people
索引,student
类型
请求信息:
URL: http://192.168.255.150:9200/people/student/1
METHOD: PUT
Content-Type: application/json
REQUEST:
{
"name": "rkyao",
"address": "杭州",
"age": 26,
"date": "1994-01-01 12:00:00"
}
URL: http://192.168.255.150:9200/people/student
METHOD: POST
Content-Type: application/json
REQUEST:
{
"name": "rkyao2",
"address": "杭州",
"age": 26,
"date": "1994-01-01 12:00:00"
}
更新操作需在url中加上关键词_update
直接更新索引为people
,类型为student
,id为1的文档,修改name
字段
请求信息:
URL: http://192.168.255.150:9200/people/student/1/_update
METHOD: POST
Content-Type: application/json
REQUEST:
{
"doc": {
"name":"Q"
}
}
(1) 脚本更新索引为people
,类型为student
,id为1的文档,将age
字段加1
请求信息:
URL: http://192.168.255.150:9200/people/student/1/_update
METHOD: POST
Content-Type: application/json
REQUEST:
{
"script": {
"lang": "painless",
"inline": "ctx._source.age += 1"
}
}
(2) 脚本更新索引为people
,类型为student
,id为1的文档,将age
字段设置为25
请求信息:
URL: http://192.168.255.150:9200/people/student/1/_update
METHOD: POST
Content-Type: application/json
REQUEST:
{
"script": {
"lang": "painless",
"inline": "ctx._source.age = params.age",
"params": {
"age" : 25
}
}
}
删除索引为people
,类型为student
,id为1的文档
请求信息:
URL: http://192.168.255.150:9200/people/student/1
METHOD: DELETE
URL: http://192.168.255.150:9200/people
METHOD: DELETE
查询条件放在url中,例如查询people
索引student
类型中id为1的数据
请求信息:
URL: http://192.168.255.150:9200/people/student/1
METHOD: GET
url中需添加关键字_search
URL: http://192.168.255.150:9200/people/student/_search
METHOD: POST
Content-Type: application/json
REQUEST:
{
"query": {
"match_all": {}
}
}
URL: http://192.168.255.150:9200/people/student/_search
METHOD: POST
Content-Type: application/json
REQUEST:
{
"query": {
"match_all": {}
},
"from": 1,
"size": 2
}
(3) match模糊查询,查询字段为name
,查询值为rkyao hello
,查询时会先将rkyao hello
分词,返回包含rkyao
或hello
的数据,结果可能为
URL: http://192.168.255.150:9200/people/student/_search
METHOD: POST
Content-Type: application/json
REQUEST:
{
"query": {
"match": {
"name": "rkyao hello"
}
}
}
(4) match_phrase模糊查询,查询字段为name
,查询值为rkyao hello
,查询时会将rkyao hello
作为一个整体去模糊匹配,结果可能为
URL: http://192.168.255.150:9200/people/student/_search
METHOD: POST
Content-Type: application/json
REQUEST:
{
"query": {
"match_phrase": {
"name": "rkyao hello"
}
}
}
(5) 多字段模糊查询,返回name
或address
字段中包含rkyao
的数据
URL: http://192.168.255.150:9200/people/student/_search
METHOD: POST
Content-Type: application/json
REQUEST:
{
"query": {
"multi_match": {
"query": "rkyao",
"fields": ["name", "address"]
}
}
}
(6) query_string语法查询,语法可以包含在查询值里,例如使用OR
逻辑,查询name
或address
字段中包含rkyao
或hello
的数据
URL: http://192.168.255.150:9200/people/student/_search
METHOD: POST
Content-Type: application/json
REQUEST:
{
"query": {
"query_string": {
"query": "rkyao OR hello",
"fields": ["name", "address"]
}
}
}
(6) term精确查询,查询address
为杭州
的数据
URL: http://192.168.255.150:9200/people/student/_search
METHOD: POST
Content-Type: application/json
REQUEST:
{
"query": {
"term": {
"address": "杭州"
}
}
}
(7) 范围查询,查询age
在20到30之间的数据
gte
大于等于,lte
小于等于,gt
大于,`lte小于
URL: http://192.168.255.150:9200/people/student/_search
METHOD: POST
Content-Type: application/json
REQUEST:
{
"query": {
"range": {
"age": {
"gte": 20,
"lte": 30
}
}
}
}
(8) 子条件查询,查询时只判断文档是否满足条件,不计算匹配度
URL: http://192.168.255.150:9200/people/student/_search
METHOD: POST
Content-Type: application/json
REQUEST:
{
"query": {
"bool": {
"filter": {
"term": {
"name": "rkyao"
}
}
}
}
}
(9) 指定排序,例如日期birthday
降序返回
请求信息:
URL: http://192.168.255.150:9200/people/student/_search
METHOD: POST
Content-Type: application/json
REQUEST:
{
"query": {
"match": {
"name": "rkyao"
}
},
"sort": [
{
"birthday": {"order": "desc"}
}
]
}
(9) 查询函数,可使用一些函数进行较复杂的查询操作,如terms、stats、max、min等,下面示范根据字段age
进行聚合查询
请求信息:
URL: http://192.168.255.150:9200/people/student/_search
METHOD: POST
Content-Type: application/json
REQUEST:
{
"aggs": {
"group_by_age": {
"terms": {
"field": "age"
}
}
}
}
(1) 固定分数查询,可以通过boost固定分数,不支持 match 只支持 filter
请求信息:
URL: http://192.168.255.150:9200/people/student/_search
METHOD: POST
Content-Type: application/json
REQUEST:
{
"query": {
"constant_score": {
"filter": {
"match": {
"name": "rkyao"
}
},
"boost": 2
}
}
}
(2) 布尔查询
should关键字,查询name
包含rkyao
或者address
包含杭州
的数据
请求信息:
URL: http://192.168.255.150:9200/people/student/_search
METHOD: POST
Content-Type: application/json
REQUEST:
{
"query": {
"bool": {
"should": [
{
"match": {
"name": "rkyao"
}
},
{
"match": {
"address": "杭州"
}
}
]
}
}
}
must关键字,查询name
包含rkyao
并且address
包含杭州
的数据
请求信息:
URL: http://192.168.255.150:9200/people/student/_search
METHOD: POST
Content-Type: application/json
REQUEST:
{
"query": {
"bool": {
"must": [
{
"match": {
"name": "rkyao"
}
},
{
"match": {
"address": "杭州"
}
}
]
}
}
}
must_not关键字,查询name
不包含rkyao
并且address
不包含杭州
的数据
请求信息:
URL: http://192.168.255.150:9200/people/student/_search
METHOD: POST
Content-Type: application/json
REQUEST:
{
"query": {
"bool": {
"must_not": [
{
"match": {
"name": "rkyao"
}
}
]
}
}
}