node.js - 如何在 ElasticSearch 中对价格字符串进行排序?
问题描述
在我的示例中,我尝试进行排序,但没有成功。我的问题是因为我的价格是字符串,价格是这样的 => 1.300,00。当我对字符串价格进行排序时,我以它为例。0,00 | 1,00 | 1.000,00 | 2,00。我想以双精度格式进行排序或类似的格式。我怎样才能做到这一点 ?
解决方案
在弹性搜索中保留关键字不是一个好主意,Price
最好的方法是在弹性搜索中进行映射price
,如下scaled float
所示:
新映射:
PUT [index_name]/_mapping
{
"properties": {
"price2": {
"type": "scaled_float",
"scaling_factor": 100
}
}
}
要解决您的问题,您可以添加新的映射并将您的值从值转换string
为numeric
:
通过查询更新:
POST [index_name]/_update_by_query
{
"query": {
"match_all": {}
},
"script": {
"source": "ctx._source['price2'] = ctx._source['price'].replace(',','')"
}
}
此查询会将您的keyword
值转换为string
并将其映射到另一个名为 的字段中price2
,然后您将需要对ingest pipeline
新条目执行该过程:
摄取管道:
POST _ingest/pipeline/_simulate
{
"pipeline": {
"processors": [
{
"script": {
"description": "Extract 'tags' from 'env' field",
"lang": "painless",
"source": "ctx['price2'] = ctx['price'].replace(',','')"
}
}
]
},
"docs": [
{
"_source": {
"price": "5,000.00"
}
}
]
}
您需要将其删除_simulate
并将其添加ingest pipeline
到您的索引中。
推荐阅读
- c++ - 如何将main Dlg的指针从线程传递到MFC内部的函数
- typescript - aws-cdk 忽略 tsconfig 路径和类型根
- c - c - 无法使用 scanf() 获取多个字符串
- python - 使用 HttpCommunicator 进行 Django 频道测试
- javascript - 如何根据表单提交使用 jQuery 更改 CSS 样式?
- python - 使用 yahoo Finance 在 python 中测试股息率
- javascript - discord.js 分片就绪超时 V12
- python - Quantlib:Vol 表面,不同期限的不同罢工水平
- php - 如果第一个查询在 mysql 和 php 中为空,如何执行第二个查询?
- javascript - 如果 Jquery 中的值为 null 或为空,则分配 0