Elasticsearch 按文本长度排序
Elasticsearch Sort By Length Of Text
我正在使用 elasticsearch 7.13 和 kibana 上的代码
这是我的映射
{
"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()"
}
}
}
那么为什么排序结果return只有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'] 会将“标题”拆分为数组,您需要将该数组连接到字符串。
试试这个:
"source": "int length = String.join(' ',doc['full_text.title']).length(); return length;"
我正在使用 elasticsearch 7.13 和 kibana 上的代码
这是我的映射
{
"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()"
}
}
}
那么为什么排序结果return只有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'] 会将“标题”拆分为数组,您需要将该数组连接到字符串。 试试这个:
"source": "int length = String.join(' ',doc['full_text.title']).length(); return length;"