开启掘金成长之旅!这是我参与「掘金日新计划 · 2 月更文挑战」的第 17 天,点击查看活动详情
大家好,我是半夏之沫 😁😁 一名金融科技领域的JAVA系统研发😊😊
我希望将自己工作和学习中的经验以最朴实,最严谨的方式分享给大家,共同进步👉💓👈
👉👉👉👉👉👉👉👉💓写作不易,期待大家的关注和点赞💓👈👈👈👈👈👈👈👈
👉👉👉👉👉👉👉👉💓关注微信公众号【技术探界】 💓👈👈👈👈👈👈👈👈
前言
Elasticsearch提供了一种Json风格的查询语言,叫做DSL(Domain-Specific language)。本篇文章将对DSL中涉及到的索引,Match查询,Term查询,布尔查询和排序查询进行详细总结。
Elasticsearch版本:7.2.1
正文
一. 索引相关
1. 查看所有索引
语句如下。
GET /_cat/indices
结果如下。
green open .kibana_task_manager jC4Nwj6WTRugzbaycZ-AdA 1 0 2 0 12.7kb 12.7kb
green open .kibana_1 j9T0aHAMSR6u_C4BoZzmOw 1 0 4 0 15.4kb 15.4kb
2. 创建索引
如下是创建一个叫做testindex的索引,并同时指定mapping,且为name字段添加一个类型为keyword的sortkey字段,以让name字段能够支持排序,桶聚合等。语句如下。
PUT /testindex
{
"mappings": {
"properties": {
"name": {
"type": "text",
"fields": {
"sortkey": {
"type": "keyword"
}
},
"fielddata": true
},
"description": {
"type": "text"
},
"constellation": {
"type": "keyword"
},
"age": {
"type": "integer"
},
"sid": {
"type": "keyword"
},
"sex": {
"type": "text"
},
"status": {
"type": "boolean"
},
"birthday": {
"type": "date"
}
}
}
}
结果如下。
{
"acknowledged" : true,
"shards_acknowledged" : true,
"index" : "testindex"
}
4. 删除索引
语句如下。
DELETE /testindex
5. 查看索引信息
语句如下。
GET /testindex
二. 批量插入文档
如下批量向testindex索引插入文档。语句如下。
PUT /testindex/_bulk
{"index":{"_index":"testindex","_id":"0000"}}
{"name":"Bill","description":"This Is Demo One Bill","constellation":"Sagittarius","age":20,"sid":"0000","sex":"男性","status":true,"birthday":"1995-08-11"}
{"index":{"_index":"testindex","_id":"0001"}}
{"name":"Tom","description":"This Is Demo Two Tom","constellation":"Libra","age":31,"sid":"0001","sex":"男性","status":true,"birthday":"1990-04-23"}
{"index":{"_index":"testindex","_id":"0002"}}
{"name":"Jack","description":"This Is Demo Three Jack","constellation":"Aquarius","age":25,"sid":"0002","sex":"男性","status":true,"birthday":"1994-12-08"}
{"index":{"_index":"testindex","_id":"0003"}}
{"name":"Mary","description":"This Is Demo Four Mary","constellation":"Gemini","age":33,"sid":"0003","sex":"女性","status":false,"birthday":"1988-01-23"}
{"index":{"_index":"testindex","_id":"0004"}}
{"name":"Wendy","description":"This Is Demo Five Wendy","constellation":"Virgo","age":19,"sid":"0004","sex":"女性","status":true,"birthday":"2001-05-05"}
三. Match查询
1. match_all
匹配所有文档,默认只会返回10条数据,如果要返回大于10条数据,可以参考第2小节分页的做法。示例语句如下所示。
GET /testindex/_search
{
"query": {
"match_all": {}
}
}
结果如下。
{
"took" : 2,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 5,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "testindex",
"_type" : "_doc",
"_id" : "0000",
"_score" : 1.0,
"_source" : {
"name" : "Bill",
"description" : "This Is Demo One Bill",
"constellation" : "Sagittarius",
"age" : 20,
"sid" : "0000",
"sex" : "男性",
"status" : true,
"birthday" : "1995-08-11"
}
},
{
"_index" : "testindex",
"_type" : "_doc",
"_id" : "0001",
"_score" : 1.0,
"_source" : {
"name" : "Tom",
"description" : "This Is Demo Two Tom",
"constellation" : "Libra",
"age" : 31,
"sid" : "0001",
"sex" : "男性",
"status" : true,
"birthday" : "1990-04-23"
}
},
{
"_index" : "testindex",
"_type" : "_doc",
"_id" : "0002",
"_score" : 1.0,
"_source" : {
"name" : "Jack",
"description" : "This Is Demo Three Jack",
"constellation" : "Aquarius",
"age" : 25,
"sid" : "0002",
"sex" : "男性",
"status" : true,
"birthday" : "1994-12-08"
}
},
{
"_index" : "testindex",
"_type" : "_doc",
"_id" : "0003",
"_score" : 1.0,
"_source" : {
"name" : "Mary",
"description" : "This Is Demo Four Mary",
"constellation" : "Gemini",
"age" : 33,
"sid" : "0003",
"sex" : "女性",
"status" : false,
"birthday" : "1988-01-23"
}
},
{
"_index" : "testindex",
"_type" : "_doc",
"_id" : "0004",
"_score" : 1.0,
"_source" : {
"name" : "Wendy",
"description" : "This Is Demo Five Wendy",
"constellation" : "Virgo",
"age" : 19,
"sid" : "0004",
"sex" : "女性",
"status" : true,
"birthday" : "2001-05-05"
}
}
]
}
}
2. 分页
分页语句如下。
GET /testindex/_search
{
"query": {
"match_all": {}
},
"from": 2,
"size": 2
}
也就是从第2条数据开始(首条数据是第0条),查询两条数据回来。结果如下。
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 5,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "testindex",
"_type" : "_doc",
"_id" : "0002",
"_score" : 1.0,
"_source" : {
"name" : "Jack",
"description" : "This Is Demo Three Jack",
"constellation" : "Aquarius",
"age" : 25,
"sid" : "0002",
"sex" : "男性",
"status" : true,
"birthday" : "1994-12-08"
}
},
{
"_index" : "testindex",
"_type" : "_doc",
"_id" : "0003",
"_score" : 1.0,
"_source" : {
"name" : "Mary",
"description" : "This Is Demo Four Mary",
"constellation" : "Gemini",
"age" : 33,
"sid" : "0003",
"sex" : "女性",
"status" : false,
"birthday" : "1988-01-23"
}
}
]
}
}
3. 指定返回某些字段
查询数据时,并不是所有字段都是需要的,为了减少查询出来的数据量,可以在查询时指定返回某些字段。语句如下。
GET /testindex/_search
{
"query": {
"match_all": {}
},
"_source": ["name", "sid"]
}
结果如下。
{
"took" : 2,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 5,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "testindex",
"_type" : "_doc",
"_id" : "0000",
"_score" : 1.0,
"_source" : {
"name" : "Bill",
"sid" : "0000"
}
},
{
"_index" : "testindex",
"_type" : "_doc",
"_id" : "0001",
"_score" : 1.0,
"_source" : {
"name" : "Tom",
"sid" : "0001"
}
},
{
"_index" : "testindex",
"_type" : "_doc",
"_id" : "0002",
"_score" : 1.0,
"_source" : {
"name" : "Jack",
"sid" : "0002"
}
},
{
"_index" : "testindex",
"_type" : "_doc",
"_id" : "0003",
"_score" : 1.0,
"_source" : {
"name" : "Mary",
"sid" : "0003"
}
},
{
"_index" : "testindex",
"_type" : "_doc",
"_id" : "0004",
"_score" : 1.0,
"_source" : {
"name" : "Wendy",
"sid" : "0004"
}
}
]
}
}
4. 脚本自定义字段
可以自定义字段,并通过脚本来控制自定义字段的表现形式。语句如下。
GET /testindex/_search
{
"query": {
"match_all": {}
},
"script_fields": {
"hello_field": {
"script": {
"lang": "painless",
"source": "doc['name'].value+'_hello'"
}
},
"word_field": {
"script": {
"lang": "painless",
"source": "doc['name'].value+'_world'"
}
}
}
}
上述的hello_field和word_field是自定义的字段,并且这两个字段由script.source配置的脚本来指定拼装规则。结果如下。
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 5,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "testindex",
"_type" : "_doc",
"_id" : "0000",
"_score" : 1.0,
"fields" : {
"word_field" : [
"bill_world"
],
"hello_field" : [
"bill_hello"
]
}
},
{
"_index" : "testindex",
"_type" : "_doc",
"_id" : "0001",
"_score" : 1.0,
"fields" : {
"word_field" : [
"tom_world"
],
"hello_field" : [
"tom_hello"
]
}
},
{
"_index" : "testindex",
"_type" : "_doc",
"_id" : "0002",
"_score" : 1.0,
"fields" : {
"word_field" : [
"jack_world"
],
"hello_field" : [
"jack_hello"
]
}
},
{
"_index" : "testindex",
"_type" : "_doc",
"_id" : "0003",
"_score" : 1.0,
"fields" : {
"word_field" : [
"mary_world"
],
"hello_field" : [
"mary_hello"
]
}
},
{
"_index" : "testindex",
"_type" : "_doc",
"_id" : "0004",
"_score" : 1.0,
"fields" : {
"word_field" : [
"wendy_world"
],
"hello_field" : [
"wendy_hello"
]
}
}
]
}
}
5. match
根据查询条件来匹配文档,注意:查询条件会分词。语句如下。
GET /testindex/_search
{
"query": {
"match": {
"description": "One"
}
}
}
结果如下。
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 1.3862944,
"hits" : [
{
"_index" : "testindex",
"_type" : "_doc",
"_id" : "0000",
"_score" : 1.3862944,
"_source" : {
"name" : "Bill",
"description" : "This Is Demo One Bill",
"constellation" : "Sagittarius",
"age" : 20,
"sid" : "0000",
"sex" : "男性",
"status" : true,
"birthday" : "1995-08-11"
}
}
]
}
}
6. match_phrase
按短语去匹配文档,注意:查询条件会分词。
普通英文短语示例如下。
GET /testindex/_search
{
"query": {
"match_phrase": {
"description": "Demo One"
}
}
}
结果如下
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 1.4733057,
"hits" : [
{
"_index" : "testindex",
"_type" : "_doc",
"_id" : "0000",
"_score" : 1.4733057,
"_source" : {
"name" : "Bill",
"description" : "This Is Demo One Bill",
"constellation" : "Sagittarius",
"age" : 20,
"sid" : "0000",
"sex" : "男性",
"status" : true,
"birthday" : "1995-08-11"
}
}
]
}
}
match_phrase特别适用于中文短语的匹配。假如使用match去查询sex字段为“女性”的文档,则“女性”会被分词为“女”和“性”,从而导致sex字段为“男性”的文档也被匹配出来,但使用match_phrase就不会,示例语句如下。
GET /testindex/_search
{
"query": {
"match_phrase": {
"sex": "女性"
}
}
}
结果如下。
{
"took" : 2,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 2,
"relation" : "eq"
},
"max_score" : 0.9624802,
"hits" : [
{
"_index" : "testindex",
"_type" : "_doc",
"_id" : "0003",
"_score" : 0.9624802,
"_source" : {
"name" : "Mary",
"description" : "This Is Demo Four Mary",
"constellation" : "Gemini",
"age" : 33,
"sid" : "0003",
"sex" : "女性",
"status" : false,
"birthday" : "1988-01-23"
}
},
{
"_index" : "testindex",
"_type" : "_doc",
"_id" : "0004",
"_score" : 0.9624802,
"_source" : {
"name" : "Wendy",
"description" : "This Is Demo Five Wendy",
"constellation" : "Virgo",
"age" : 19,
"sid" : "0004",
"sex" : "女性",
"status" : true,
"birthday" : "2001-05-05"
}
}
]
}
}
只会把sex字段是女性的文档匹配到。
7. slop指定间隔
match_phrase可以搭配slop来指定间隔,示例如下。
GET /testindex/_search
{
"query": {
"match_phrase": {
"description": {
"query": "Is Bill",
"slop": 2
}
}
}
}
表示is和bill之间如果间隔两个词就算作匹配。结果如下。
{
"took" : 2,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 0.7046245,
"hits" : [
{
"_index" : "testindex",
"_type" : "_doc",
"_id" : "0000",
"_score" : 0.7046245,
"_source" : {
"name" : "Bill",
"description" : "This Is Demo One Bill",
"constellation" : "Sagittarius",
"age" : 20,
"sid" : "0000",
"sex" : "男性",
"status" : true,
"birthday" : "1995-08-11"
}
}
]
}
}
8. match_phrase_prefix
如下的示例中,查询条件Is Demo On会被分词为is,demo和on这三个Token,那么所谓的match_phrase_prefix查询,也就是会将最后一个Token之前的Token(is,demo)去进行一个match_phrase查询,然后最后一个Token(on)进行前缀匹配。
语句如下。
GET /testindex/_search
{
"query": {
"match_phrase_prefix": {
"description": "Is Demo On"
}
}
}
结果如下。
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 1.5603172,
"hits" : [
{
"_index" : "testindex",
"_type" : "_doc",
"_id" : "0000",
"_score" : 1.5603172,
"_source" : {
"name" : "Bill",
"description" : "This Is Demo One Bill",
"constellation" : "Sagittarius",
"age" : 20,
"sid" : "0000",
"sex" : "男性",
"status" : true,
"birthday" : "1995-08-11"
}
}
]
}
}
9. multi_match
需要多个字段匹配上查询条件,注意:查询条件会分词。语句如下。
GET /testindex/_search
{
"query": {
"multi_match": {
"query": "Bill Good",
"fields": ["name", "description"]
}
}
}
结果如下。
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 1.3862944,
"hits" : [
{
"_index" : "testindex",
"_type" : "_doc",
"_id" : "0000",
"_score" : 1.3862944,
"_source" : {
"name" : "Bill",
"description" : "This Is Demo One Bill",
"constellation" : "Sagittarius",
"age" : 20,
"sid" : "0000",
"sex" : "男性",
"status" : true,
"birthday" : "1995-08-11"
}
}
]
}
}
四. Term查询
1. term
根据查询条件匹配文档,注意:查询条件不会分词。语句如下。
GET /testindex/_search
{
"query": {
"term": {
"description": {
"value": "one"
}
}
}
}
结果如下。
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 1.3862944,
"hits" : [
{
"_index" : "testindex",
"_type" : "_doc",
"_id" : "0000",
"_score" : 1.3862944,
"_source" : {
"name" : "Bill",
"description" : "This Is Demo One Bill",
"constellation" : "Sagittarius",
"age" : 20,
"sid" : "0000",
"sex" : "男性",
"status" : true,
"birthday" : "1995-08-11"
}
}
]
}
}
2. terms
根据查询条件集合匹配文档,文档匹配任意查询条件就返回,注意:查询条件不会分词。语句如下所示。
GET /testindex/_search
{
"query": {
"terms": {
"description": [
"one",
"two"
]
}
}
}
结果如下。
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 2,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "testindex",
"_type" : "_doc",
"_id" : "0000",
"_score" : 1.0,
"_source" : {
"name" : "Bill",
"description" : "This Is Demo One Bill",
"constellation" : "Sagittarius",
"age" : 20,
"sid" : "0000",
"sex" : "男性",
"status" : true,
"birthday" : "1995-08-11"
}
},
{
"_index" : "testindex",
"_type" : "_doc",
"_id" : "0001",
"_score" : 1.0,
"_source" : {
"name" : "Tom",
"description" : "This Is Demo Two Tom",
"constellation" : "Libra",
"age" : 31,
"sid" : "0001",
"sex" : "男性",
"status" : true,
"birthday" : "1990-04-23"
}
}
]
}
}
五. 布尔查询
1. must
文档满足所有条件时才会被返回。语句如下。
GET /testindex/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"name": "Mary"
}
},
{
"match_phrase": {
"sex": "女性"
}
}
]
}
}
}
结果如下。
{
"took" : 4,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 2.3487744,
"hits" : [
{
"_index" : "testindex",
"_type" : "_doc",
"_id" : "0003",
"_score" : 2.3487744,
"_source" : {
"name" : "Mary",
"description" : "This Is Demo Four Mary",
"constellation" : "Gemini",
"age" : 33,
"sid" : "0003",
"sex" : "女性",
"status" : false,
"birthday" : "1988-01-23"
}
}
]
}
}
2. should
文档满足任意条件就返回。语句如下。
GET /testindex/_search
{
"query": {
"bool": {
"should": [
{
"match": {
"name": "Bill"
}
},
{
"match": {
"name": "Mary"
}
}
]
}
}
}
结果如下。
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 2,
"relation" : "eq"
},
"max_score" : 1.3862944,
"hits" : [
{
"_index" : "testindex",
"_type" : "_doc",
"_id" : "0000",
"_score" : 1.3862944,
"_source" : {
"name" : "Bill",
"description" : "This Is Demo One Bill",
"constellation" : "Sagittarius",
"age" : 20,
"sid" : "0000",
"sex" : "男性",
"status" : true,
"birthday" : "1995-08-11"
}
},
{
"_index" : "testindex",
"_type" : "_doc",
"_id" : "0003",
"_score" : 1.3862944,
"_source" : {
"name" : "Mary",
"description" : "This Is Demo Four Mary",
"constellation" : "Gemini",
"age" : 33,
"sid" : "0003",
"sex" : "女性",
"status" : false,
"birthday" : "1988-01-23"
}
}
]
}
}
3. must_not
文档不满足所有条件时才返回。语句如下。
GET /testindex/_search
{
"query": {
"bool": {
"must_not": [
{
"match_phrase": {
"sex": "男性"
}
},
{
"range": {
"age": {
"gte": 25
}
}
}
]
}
}
}
结果如下。
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 0.0,
"hits" : [
{
"_index" : "testindex",
"_type" : "_doc",
"_id" : "0004",
"_score" : 0.0,
"_source" : {
"name" : "Wendy",
"description" : "This Is Demo Five Wendy",
"constellation" : "Virgo",
"age" : 19,
"sid" : "0004",
"sex" : "女性",
"status" : true,
"birthday" : "2001-05-05"
}
}
]
}
}
4. filter
filter与must功能是一样的,只是filter不参与计分,而must参与计分。语句如下。
GET /testindex/_search
{
"query": {
"bool": {
"filter": [
{
"match_phrase": {
"sex": "男性"
}
},
{
"range": {
"age": {
"gte": 25
}
}
}
]
}
}
}
结果如下。
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 2,
"relation" : "eq"
},
"max_score" : 0.0,
"hits" : [
{
"_index" : "testindex",
"_type" : "_doc",
"_id" : "0001",
"_score" : 0.0,
"_source" : {
"name" : "Tom",
"description" : "This Is Demo Two Tom",
"constellation" : "Libra",
"age" : 31,
"sid" : "0001",
"sex" : "男性",
"status" : true,
"birthday" : "1990-04-23"
}
},
{
"_index" : "testindex",
"_type" : "_doc",
"_id" : "0002",
"_score" : 0.0,
"_source" : {
"name" : "Jack",
"description" : "This Is Demo Three Jack",
"constellation" : "Aquarius",
"age" : 25,
"sid" : "0002",
"sex" : "男性",
"status" : true,
"birthday" : "1994-12-08"
}
}
]
}
}
5. 布尔查询嵌套
语句如下。
GET /testindex/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"description": "This People"
}
},
{
"terms": {
"age": [
30,
33
]
}
},
{
"bool": {
"should": [
{
"match": {
"status": true
}
},
{
"range": {
"birthday": {
"gte": "1980-01-01"
}
}
}
]
}
}
]
}
}
}
结果如下。
{
"took" : 6,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 2.0870113,
"hits" : [
{
"_index" : "testindex",
"_type" : "_doc",
"_id" : "0003",
"_score" : 2.0870113,
"_source" : {
"name" : "Mary",
"description" : "This Is Demo Four Mary",
"constellation" : "Gemini",
"age" : 33,
"sid" : "0003",
"sex" : "女性",
"status" : false,
"birthday" : "1988-01-23"
}
}
]
}
}
六. 排序查询
1. 简单排序
如下是对日期进行升序排序。语句如下。
GET /testindex/_search
{
"query": {
"match_all": {}
},
"sort": [
{
"birthday": {
"order": "asc"
}
}
]
}
结果如下。
{
"took" : 2,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 5,
"relation" : "eq"
},
"max_score" : null,
"hits" : [
{
"_index" : "testindex",
"_type" : "_doc",
"_id" : "0003",
"_score" : null,
"_source" : {
"name" : "Mary",
"description" : "This Is Demo Four Mary",
"constellation" : "Gemini",
"age" : 33,
"sid" : "0003",
"sex" : "女性",
"status" : false,
"birthday" : "1988-01-23"
},
"sort" : [
569894400000
]
},
{
"_index" : "testindex",
"_type" : "_doc",
"_id" : "0001",
"_score" : null,
"_source" : {
"name" : "Tom",
"description" : "This Is Demo Two Tom",
"constellation" : "Libra",
"age" : 31,
"sid" : "0001",
"sex" : "男性",
"status" : true,
"birthday" : "1990-04-23"
},
"sort" : [
640828800000
]
},
{
"_index" : "testindex",
"_type" : "_doc",
"_id" : "0002",
"_score" : null,
"_source" : {
"name" : "Jack",
"description" : "This Is Demo Three Jack",
"constellation" : "Aquarius",
"age" : 25,
"sid" : "0002",
"sex" : "男性",
"status" : true,
"birthday" : "1994-12-08"
},
"sort" : [
786844800000
]
},
{
"_index" : "testindex",
"_type" : "_doc",
"_id" : "0000",
"_score" : null,
"_source" : {
"name" : "Bill",
"description" : "This Is Demo One Bill",
"constellation" : "Sagittarius",
"age" : 20,
"sid" : "0000",
"sex" : "男性",
"status" : true,
"birthday" : "1995-08-11"
},
"sort" : [
808099200000
]
},
{
"_index" : "testindex",
"_type" : "_doc",
"_id" : "0004",
"_score" : null,
"_source" : {
"name" : "Wendy",
"description" : "This Is Demo Five Wendy",
"constellation" : "Virgo",
"age" : 19,
"sid" : "0004",
"sex" : "女性",
"status" : true,
"birthday" : "2001-05-05"
},
"sort" : [
989020800000
]
}
]
}
}
2. 多级排序
如果是演示当birthday相等时,再根据age来排序。语句如下。
GET /testindex/_search
{
"query": {
"match_all": {}
},
"sort": [
{
"birthday": {
"order": "asc"
}
},
{
"age": {
"order": "desc"
}
}
]
}
结果如下。
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 5,
"relation" : "eq"
},
"max_score" : null,
"hits" : [
{
"_index" : "testindex",
"_type" : "_doc",
"_id" : "0003",
"_score" : null,
"_source" : {
"name" : "Mary",
"description" : "This Is Demo Four Mary",
"constellation" : "Gemini",
"age" : 33,
"sid" : "0003",
"sex" : "女性",
"status" : false,
"birthday" : "1988-01-23"
},
"sort" : [
569894400000,
33
]
},
{
"_index" : "testindex",
"_type" : "_doc",
"_id" : "0001",
"_score" : null,
"_source" : {
"name" : "Tom",
"description" : "This Is Demo Two Tom",
"constellation" : "Libra",
"age" : 31,
"sid" : "0001",
"sex" : "男性",
"status" : true,
"birthday" : "1990-04-23"
},
"sort" : [
640828800000,
31
]
},
{
"_index" : "testindex",
"_type" : "_doc",
"_id" : "0002",
"_score" : null,
"_source" : {
"name" : "Jack",
"description" : "This Is Demo Three Jack",
"constellation" : "Aquarius",
"age" : 25,
"sid" : "0002",
"sex" : "男性",
"status" : true,
"birthday" : "1994-12-08"
},
"sort" : [
786844800000,
25
]
},
{
"_index" : "testindex",
"_type" : "_doc",
"_id" : "0000",
"_score" : null,
"_source" : {
"name" : "Bill",
"description" : "This Is Demo One Bill",
"constellation" : "Sagittarius",
"age" : 20,
"sid" : "0000",
"sex" : "男性",
"status" : true,
"birthday" : "1995-08-11"
},
"sort" : [
808099200000,
20
]
},
{
"_index" : "testindex",
"_type" : "_doc",
"_id" : "0004",
"_score" : null,
"_source" : {
"name" : "Wendy",
"description" : "This Is Demo Five Wendy",
"constellation" : "Virgo",
"age" : 19,
"sid" : "0004",
"sex" : "女性",
"status" : true,
"birthday" : "2001-05-05"
},
"sort" : [
989020800000,
19
]
}
]
}
}
3. 对分词字段排序
正常是不能对会分词的字段进行排序以及桶聚合的,但是可以为分词的字段添加一个类型为keyword的字段,就可以排序了,具体的添加可以查看第一节第2小节。
示例语句如下。
GET /testindex/_search
{
"query": {
"match_all": {}
},
"sort": {
"name": {
"order": "asc"
}
}
}
结果如下。
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 5,
"relation" : "eq"
},
"max_score" : null,
"hits" : [
{
"_index" : "testindex",
"_type" : "_doc",
"_id" : "0000",
"_score" : null,
"_source" : {
"name" : "Bill",
"description" : "This Is Demo One Bill",
"constellation" : "Sagittarius",
"age" : 20,
"sid" : "0000",
"sex" : "男性",
"status" : true,
"birthday" : "1995-08-11"
},
"sort" : [
"bill"
]
},
{
"_index" : "testindex",
"_type" : "_doc",
"_id" : "0002",
"_score" : null,
"_source" : {
"name" : "Jack",
"description" : "This Is Demo Three Jack",
"constellation" : "Aquarius",
"age" : 25,
"sid" : "0002",
"sex" : "男性",
"status" : true,
"birthday" : "1994-12-08"
},
"sort" : [
"jack"
]
},
{
"_index" : "testindex",
"_type" : "_doc",
"_id" : "0003",
"_score" : null,
"_source" : {
"name" : "Mary",
"description" : "This Is Demo Four Mary",
"constellation" : "Gemini",
"age" : 33,
"sid" : "0003",
"sex" : "女性",
"status" : false,
"birthday" : "1988-01-23"
},
"sort" : [
"mary"
]
},
{
"_index" : "testindex",
"_type" : "_doc",
"_id" : "0001",
"_score" : null,
"_source" : {
"name" : "Tom",
"description" : "This Is Demo Two Tom",
"constellation" : "Libra",
"age" : 31,
"sid" : "0001",
"sex" : "男性",
"status" : true,
"birthday" : "1990-04-23"
},
"sort" : [
"tom"
]
},
{
"_index" : "testindex",
"_type" : "_doc",
"_id" : "0004",
"_score" : null,
"_source" : {
"name" : "Wendy",
"description" : "This Is Demo Five Wendy",
"constellation" : "Virgo",
"age" : 19,
"sid" : "0004",
"sex" : "女性",
"status" : true,
"birthday" : "2001-05-05"
},
"sort" : [
"wendy"
]
}
]
}
}
总结
本文主要总结了索引相关操作,match查询,term查询,布尔查询和排序查询这几个常用的查询操作。后续还将对模糊查询和聚合查询进行总结。
大家好,我是半夏之沫 😁😁 一名金融科技领域的JAVA系统研发😊😊
我希望将自己工作和学习中的经验以最朴实,最严谨的方式分享给大家,共同进步👉💓👈
👉👉👉👉👉👉👉👉💓写作不易,期待大家的关注和点赞💓👈👈👈👈👈👈👈👈
👉👉👉👉👉👉👉👉💓关注微信公众号【技术探界】 💓👈👈👈👈👈👈👈👈
开启掘金成长之旅!这是我参与「掘金日新计划 · 2 月更文挑战」的第 17 天,点击查看活动详情