【发布时间】:2021-02-18 18:12:43
【问题描述】:
我正在阅读 ElasticSearch Definitive Guide,关于在弹性搜索中对嵌套字段进行排序。
我正在使用 Elastic Search 7.10.1
以下是创建索引和准备数据的代码。
- 创建索引并设置映射
PUT /my_index
{
"mappings": {
"properties": {
"body": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"comments": {
"type": "nested",
"properties": {
"age": {
"type": "long"
},
"comment": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"date": {
"type": "date"
},
"name": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"stars": {
"type": "long"
}
}
},
"tags": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"title": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
}
}
- 插入数据:
PUT /my_index/_doc/1
{
"title": "Nest eggs",
"body": "Making your money work...",
"tags": [
"cash",
"shares"
],
"comments": [
{
"name": "John Smith",
"comment": "Great article",
"age": 28,
"stars": 4,
"date": "2014-09-01"
},
{
"name": "Alice White",
"comment": "More like this please",
"age": 31,
"stars": 5,
"date": "2014-10-22"
}
]
}
PUT /my_index/_doc/2
{
"title": "Investment secrets",
"body": "What they don't tell you ...",
"tags": [
"shares",
"equities"
],
"comments": [
{
"name": "Mary Brown",
"comment": "Lies, lies, lies",
"age": 42,
"stars": 1,
"date": "2014-10-18"
},
{
"name": "John Smith",
"comment": "You're making it up!",
"age": 28,
"stars": 2,
"date": "2014-10-16"
}
]
}
- 根据
comments.starts字段查询和排序
GET /my_index/_search
{
"query": {
"nested": {
"path": "comments",
"query": {
"range": {
"comments.date": {
"gte": "2014-10-01",
"lt": "2014-11-01"
}
}
}
}
},
"sort": {
"comments.stars": {
"order": "asc",
"mode": "min",
"nested_filter": {
"range": {
"comments.date": {
"gte": "2014-10-01",
"lt": "2014-11-01"
}
}
}
}
}
}
- 结果如下:
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 2,
"relation" : "eq"
},
"max_score" : null,
"hits" : [
{
"_index" : "my_index",
"_type" : "_doc",
"_id" : "1",
"_score" : null,
"_source" : {
"title" : "Nest eggs",
"body" : "Making your money work...",
"tags" : [
"cash",
"shares"
],
"comments" : [
{
"name" : "John Smith",
"comment" : "Great article",
"age" : 28,
"stars" : 4,
"date" : "2014-09-01"
},
{
"name" : "Alice White",
"comment" : "More like this please",
"age" : 31,
"stars" : 5,
"date" : "2014-10-22"
}
]
},
"sort" : [
9223372036854775807
]
},
{
"_index" : "my_index",
"_type" : "_doc",
"_id" : "2",
"_score" : null,
"_source" : {
"title" : "Investment secrets",
"body" : "What they don't tell you ...",
"tags" : [
"shares",
"equities"
],
"comments" : [
{
"name" : "Mary Brown",
"comment" : "Lies, lies, lies",
"age" : 42,
"stars" : 1,
"date" : "2014-10-18"
},
{
"name" : "John Smith",
"comment" : "You're making it up!",
"age" : 28,
"stars" : 2,
"date" : "2014-10-16"
}
]
},
"sort" : [
9223372036854775807
]
}
]
}
}
我不明白结果,我无法弄清楚starts 是如何排序的。如果指定了"order":"asc" 和"order": "desc",,则没有区别
【问题讨论】:
标签: elasticsearch