简体   繁体   English

如何在 ElasticSearch 中对价格字符串进行排序?

[英]How can I sort price string in ElasticSearch?

In my example i tried to sort but i have no success.在我的示例中,我尝试排序,但没有成功。 My problem is because my price is string and the price is like that => 1.300,00.我的问题是因为我的价格是字符串,价格就是这样 => 1.300,00。 When I sort string price i have that for exemplo.当我对字符串价格进行排序时,我以它为例。 0,00 | 0,00 | 1,00 | 1,00 | 1.000,00 | 1.000,00 | 2,00. 2,00。 I wanna format format in double for sort or like similar that.我想以 double 格式进行排序或类似的格式。 How can i do that ?我怎样才能做到这一点 ? 输出

It is not a good idea to keep Price as a keyword in Elastic search best approach would be to map price as scaled float in elastic search like this:在弹性搜索中将Price作为关键字保留不是一个好主意,最好的方法是将price映射为弹性搜索中的scaled float ,如下所示:

New Mapping:新映射:

PUT [index_name]/_mapping
{
  "properties": {
    "price2": {
      "type": "scaled_float",
        "scaling_factor": 100
    }
  }
}

To solve your problem you can add new mapping and convert your value from string to numeric value:要解决您的问题,您可以添加新映射并将您的值从string转换为numeric

Update by query:按查询更新:

POST [index_name]/_update_by_query
{
    "query": {
        "match_all": {}
    }, 
    "script": {
       "source": "ctx._source['price2'] = ctx._source['price'].replace(',','')"
    }
}

This query will convert your keyword value to string and map it in another field named price2 , then you will need to have an ingest pipeline to do the process to new entries:此查询会将您的keyword值转换为string并将其映射到名为price2另一个字段中,然后您需要有一个ingest pipeline来执行对新条目的处理:

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"
      }
    }
  ]
}

You need to remove _simulate and add this ingest pipeline to your index.您需要删除_simulate并将此ingest pipeline添加到您的索引中。

我的代码

How can I get this price value ?我怎样才能得到这个价格值?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM