sorting - Elasticsearch 按文本长度排序
问题描述
我在 kibana 上使用 elasticsearch 7.13 和代码
这是我的映射
{
"full_text" : {
"properties" : {
"title" : {
"type" : "text",
"fielddata" : true
},
}
}
}
这是我的数据
"full_text" : [
{
"title" : "Pkd chuyên cho thuê kingdom 101 1pn đến 3pn giá rẻ nhất thị trường chỉ 11 triệu/căn. lh 0919504***"
}
]
这是我按 full_text.title 长度排序的代码
"sort": {
"_script": {
"type": "number",
"order": "desc",
"script": {
"lang": "painless",
"source": "doc['full_text.title'].value.length()"
}
}
}
那么为什么排序结果只返回 7 呢?
"_source" : {
"full_text" : [
{
"title" : "Pkd chuyên cho thuê kingdom 101 1pn đến 3pn giá rẻ nhất thị trường chỉ 11 triệu/căn. lh 0919504***"
}
]
},
"sort": [
7.0
]
解决方案
因为 doc['full_text.title'] 会将“title”拆分为数组,因此您需要将该数组加入字符串。试试这个:
"source": "int length = String.join(' ',doc['full_text.title']).length(); return length;"
推荐阅读
- .htaccess - 重定向 domainA.com/dir/file -> domainB.com/dir/file 保持 URL 结构
- javascript - 如何让 addEventListener 点击事件在 IOS 上使用 vanilla javascript
- node.js - 如何定义我的 Convict Schema 一次并将其重用于我的配置中的多个对象
- javascript - 邮递员没有响应,我正在发送补丁请求但在邮递员控制台中没有收到响应?
- date - Power BI 中的层次结构日期
- reactjs - 在类型 '[] | 上找不到具有类型参数的索引签名'字符串' iCommits'.ts(7053)
- c++ - 为什么我的 .icns 图标不能在 OSX 上与 QT/C++ 一起使用?
- python-3.x - 无法从 selenium 中的 iframe 标记中获取属性('src')
- html - 提交表单后将文件从 HTTP 服务器推送到客户端
- http - 在后台处理时响应 HTTP 请求