Match string with minus character in elasticsearch

Viewed 12218

So in DB I have this entry:

Mark-Whalberg

When searching with term

Mark-Whalberg

I get not match.

Why? Is minus a special character what I understand? It symbolizes "exclude"?

The query is this:

{"query_string": {"query": 'Mark-Whalberg', "default_operator": "AND"}}

Searching everything else, like:

Mark
Whalberg
hlb
Mark Whalberg

returns a match.

Is this stored as two different pieces? How can I get a match when including the minus sign in the search term?

--------------EDIT--------------

This is the current query:

var fields = [
    "field1",
    "field2",
];

{"query_string":{"query": '*Mark-Whalberg*',"default_operator": "AND","fields": fields}};
2 Answers

I've stuck in same question and the answer from @Mickael was perfect to understand what is going on (I really recommend you to read the linked documentation).

I solve this by defining an operator to the query:

GET http://localhost:9200/creative/_search

{  
  "query": {
    "match": {
      "keyword_id": {
        "query": "fake-keyword-uuid-3",
        "operator": "AND"
       }
    }
  }
}

For better understand the algorithm that this query uses, try to add "explain": true and analyse the results:

GET http://localhost:9200/creative/_search

{  
  "explain": true,
  "query": // ...
}
Related