ES not search with hyphen

Hello :slight_smile:
I can't figure out the problem at all. I have a field with a title. The name consists of a hyphen. This is: "usd-coin". I'm trying to search with this query:

{
    "sort": [
        {
            "time": "desc"
        }
    ],
    "query": {
        "bool": {
            "must": [
                {"term": {"currency": "usd-coin"}}
            ]
        }
    }
}

In the end, it doesn't look for anything. But as soon as I leave "usd" - everything will be found. I need search accuracy. Can you please tell me how I can search specifically for the query "usd-coin"?
As far as I understand, the hyphen is reserved by the system. But how can this be bypassed?

I will be grateful for the answers.

Use a keyword datatype instead.

Can you please tell me how to specify this?

See Keyword type family | Elasticsearch Guide [8.1] | Elastic

As I understand it, you need to recreate the partition and specify the mappings. But I already have a table. How can I search in my case?

Yes. You need to reindex the data of you want to use the right mapping for your use case.

Here, as a workaround, you could maybe use a phrase query. Although this might work, be aware that it's a workaround and that you'd better use the right mapping.

You can try to create a runtime field with the keyword map for that specific field.

It would be something like this:

PUT your-index-name/
{
  "mappings": {
    "runtime": {
      "currency": {
        "type": "keyword"
      }
    }
  }
}

Be aware that runtime fields can impact the search performance in some cases, the best solution would indeed be a reindex, but maybe the runtime field can solve your problem quickly.

You can read more about runtime fields in the documentation.

4 Likes

OMG! I forgot about this one. Thanks @leandrojmp !

2 Likes

Thanks a lot :slight_smile:

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.