Joey Yi Zhao
Joey Yi Zhao

Reputation: 42576

Does elasticsearch ignore dot in match_phrase?

I am using match_phrase to do a search like:

"match_phrase": {
                            "name": "XX .S"
                          }

And it finds the result which has name as "name" : "XX S&#", and "name": "XX S Ltd". It seems that it ignores . in the search. After some investigation, the dot may be striped during indexing when it tokenise the words inside the string. If this is true, how can I make the search treat dot as a regular character? If it is not true, what causes that and how can I fix it?

What I am expecting is that I can get below response by querying XX .S

A XX .S B
XX .S
XX .S11
XX .Sa

The only issue about match_phrase I found so far is that it doesn't escape .. It works perfect for other cases.

Upvotes: 0

Views: 2288

Answers (2)

turong
turong

Reputation: 1622

You can keep the old search logic by introducing/(not replacing) a more keyword field like this.

{
  "foo": {
    "type" "text",
    "fields": {
      "raw": { 
        "type":  "keyword"
      }
    }
  }
}

And you can perform full-text search with foo.raw field.

This is what you need to query to have results

{
    "query": {
        "wildcard": {
            "foo.raw": "*XX .S*"
        }
    }
}

See more details at https://www.elastic.co/guide/en/elasticsearch/reference/7.5/multi-fields.html

Best,

Upvotes: 0

YouXiang-Wang
YouXiang-Wang

Reputation: 1127

You need to define "name" as keyword type while you are creating the index. Default is text type which will be tokenized by Elastic Search engine.

name": {
    "type": "keyword"
}

Use wild card:

{
    "from": 0,
    "size": 200,
    "query": {
        "bool": {
            "filter": [
                {
                    "bool": {
                        "must": [
                            {
                                "wildcard": {
                                    "name": {
                                        "wildcard": "*XX .S*",
                                        "boost": 1
                                    }
                                }
                            }
                        ],
                        "adjust_pure_negative": true,
                        "boost": 1
                    }
                }
            ],
            "adjust_pure_negative": true,
            "boost": 1
        }
    },
    "_source": {
        "includes": [
            "name"
        ],
        "excludes": []
    }
}

Upvotes: 1

Related Questions