简体   繁体   中英

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. When I sort string price i have that for exemplo. 0,00 | 1,00 | 1.000,00 | 2,00. I wanna format format in double for sort or like similar that. 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:

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:

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:

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.

我的代码

How can I get this price value ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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