Graham
Graham

Reputation: 96

Changing how hyphen is interpreted in elasticsearch

I have a field that contains names with the following structure <name>-<version> (e.g. foo-1.0).

I have the following analyzer config:

"settings": {
  "index": {
    "analysis": {
      "analyzer": {
        "default": {
          "type": "snowball",
          "language": "English"
        }
      }
    }
  }

And the following mapping:

"itemName": {
  "type": "multi_field",
    "fields": {
      "itemName": {
        "type": "string",
        "index": "analyzed"
      },
      "untouched": {
        "type": "string",
        "index": "not_analyzed"
      }
    }
  },

I'd like to be able to search for foo-1.0 without having to quote it. From user feedback, this is what they expect to be able to do, but unfortunately, foo-1.0 is being interpreted as foo NOT 1.0. I understand that the hyphen is equivalent to NOT but I naively thought it would only do this if preceded by a space (e.g. foo -1.0). Is there a way to configure elasticsearch to either stop interpreting the hyphen as NOT, or to stop is splitting on the hyphen when not prefixed by a space? Or is it something I can influence through the analyzer?

I don't know whether this will influence the answer, but I'd also like to be able to search other fields in the same query. E.g. something like foo-1.0 OR bar.

Many thanks for any help.

Graham.

Upvotes: 1

Views: 898

Answers (2)

Kumar Kailash
Kumar Kailash

Reputation: 1395

curl -XPUT http://localhost:9200/index_name -d '{
    "mappings" : {
        "type_name" : {
            "properties" : {
                "field_name" : { "type": "string", "index" : "not_analyzed" }
            }
        }
    }
}'

After which you could use term query or querystring.

Upvotes: 0

kgm
kgm

Reputation: 807

You can escape - with backslash \ (or when using JSON double backslash \\

Search for

"foo\\-1.0"

Upvotes: 1

Related Questions