gpt4 book ai didi

elasticsearch - 如何使用 Elasticsearch 对文本字段进行排序

转载 作者:行者123 更新时间:2023-12-02 18:53:44 25 4
gpt4 key购买 nike

{
"parent" : "some_id",
"type" : "support",
"metadata" : {
"account_type" : "Regular",
"subject" : "Test Subject",
"user_name" : "John Doe",
"origin" : "Origin",
"description" : "TEST",
"media" : [ ],
"ticket_number" : "XXXX",
"status" : "completed",
},
"create_time" : "2021-02-24T15:08:57.750Z",
"entity_name" : "comment"
}

这是我的演示数据。当我尝试按 metadata.sort 排序时,例如->

GET comments-*/_search
{
"query": {
"bool": {
"must": [{
"match": {
"type": "support"
}
}]
}
},
"from": 0,
"size": 50,
"sort": [{
"metadata.status": {
"order": "desc"
}
}]
}

它说 -> Fielddata 默认在文本字段上被禁用。在 [metadata.status] 上设置 fielddata=true 以通过反转倒排索引将字段数据加载到内存中。请注意,这可能会占用大量内存。或者改用关键字字段。

我不确定如何实现相同的目标,因为我是 ESS 的新手。任何帮助将不胜感激

最佳答案

您只能在字符串字段上按“关键字”类型的字段排序。

如果您在发送文档之前不设置映射,Elasticsearch 动态映射将创建 2 个字段。

在本例中为“status”和“status.keyword”。

所以尝试使用“metadata.status.keyword”。

长话短说

对于不进行全文搜索的字段(如状态标志),最好只存储字段的关键字版本。

为此,您必须在索引任何文档之前设置映射。

有一个技巧:

  1. 摄取数据
POST test_predipa/_doc
{
"parent" : "some_id",
"type" : "support",
"metadata" : {
"account_type" : "Regular",
"subject" : "Test Subject",
"user_name" : "John Doe",
"origin" : "Origin",
"description" : "TEST",
"media" : [ ],
"ticket_number" : "XXXX",
"status" : "completed"
},
"create_time" : "2021-02-24T15:08:57.750Z",
"entity_name" : "comment"
}
  1. 获取自动生成的映射
GET test_predipa/_mapping
  1. 使用相同的映射创建一个新的空索引并根据需要进行修改(在这种情况下,从 metadata.status 中删除文本类型字段,只保留关键字 one。
PUT test_predipa_new
{
"mappings": {
"properties": {
"create_time": {
"type": "date"
},
"entity_name": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"metadata": {
"properties": {
"account_type": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"description": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"origin": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"status": {
"type": "keyword"
},
"subject": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"ticket_number": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"user_name": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
},
"parent": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"type": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
}
}
  1. 将数据从旧索引移动到新的空索引
POST _reindex
{
"source": {
"index": "test_predipa"
},
"dest": {
"index": "test_predipa_new"
}
}
  1. 运行排序查询
GET test_predipa_new/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"type": "support"
}
}
]
}
},
"from": 0,
"size": 50,
"sort": [
{
"metadata.status": {
"order": "desc"
}
}
]
}

关于elasticsearch - 如何使用 Elasticsearch 对文本字段进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66427690/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com