# Wildcard search case insensitive

**URL:** <https://discuss.elastic.co/t/wildcard-search-case-insensitive/122337>\
**Category:** Elasticsearch\
**Created:** [March 2, 2018, 10:56pm UTC](https://discuss.elastic.co/t/wildcard-search-case-insensitive/122337 "2018-03-02T22:56:18Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![stam585](https://avatars.discourse-cdn.com/v4/letter/s/f1d935/32.png) [@stam585](https://discuss.elastic.co/u/stam585)\
**Post date:** [March 2, 2018, 10:56pm UTC](https://discuss.elastic.co/t/wildcard-search-case-insensitive/122337/1 "2018-03-02T22:56:18Z")

</div>

Going over the wildcard search documentation I am missing a few issues.

> **[Wildcard query | Elasticsearch Guide \[8.11\] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-wildcard-query.html)**

First, I have the following mapping and entities:

```
{
    "ms_search_v1": {
        "mappings": {
            "entries": {
                "properties": {
                    "index": {
                        "type": "long"
                    },
                    "metaSite": {
                        "properties": {
                            "name1": {
                                "type": "keyword"
                            },
                            "name2": {
                                "type": "text"
                            }
                        }
                    }
                }
            }
        }
    }
}

```

entities

```
[
  {
    "metaSite": {
      "name1": "Aaa Bbb",
      "name2": "Aaa Bbb"
    },
    "index": 0
  },
  {
    "metaSite": {
      "name1": "Bbb Ccc",
      "name2": "Bbb Ccc"
    },
    "index": 1
  },
  {
    "metaSite": {
      "name1": "Aaa Ccc",
      "name2": "Aaa Ccc"
    },
    "index": 2
  }
]

```

1. On which data type does wild card search works? Keyword or text?  
according to the doc

> Keyword fields are only searchable by their exact value.  
> But it does seem to work when I search it with case sensitive wildcard

```
 {
     "query": {
         "wildcard" : { "metaSite.name1" : "Aaa*" }
     }
 }

```

1. When I query the text I get nothing.  
{  
"query": {  
"wildcard" : { "metaSite.name2" : "Aaa\*" }  
}  
}

How come?

1. How do I query wildcard with case insensitive?  
and on what data type should I store the data?

I tried adding the following

```
...
"name3": {
    "type": "keyword",
    "normalizer": "lowercase_normalizer"
}
....

```

where the normalizer is defined with lowercase analyzer and it worked.  
But is it the right way?  
Thanks.

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [March 3, 2018, 12:17am UTC](https://discuss.elastic.co/t/wildcard-search-case-insensitive/122337/2 "2018-03-03T00:17:44Z")

</div>

So in short. When you index a field with a text type and default analyzer, it is indexed in lowercase and broken into tokens.

`Aaa Bbb` became `aaa`, `bbb`.

When you search with a wildcard query (which is a bad idea), you must use the same case as was indexed. So `aa*` should work where `Aa*` shouldn't.

For keywords, they are indexed as `Aaa Bbb`, so searching for `Aa*` will match. Note that `Bb*` won't match.

With a lowercase normalizer, `aa*` will match. `Aa*`, `Bb*` or `bb*` won't match.

---

<div class="post-metadata">

**Author:** ![stam585](https://avatars.discourse-cdn.com/v4/letter/s/f1d935/32.png) [@stam585](https://discuss.elastic.co/u/stam585)\
**Post date:** [March 3, 2018, 2:10pm UTC](https://discuss.elastic.co/t/wildcard-search-case-insensitive/122337/3 "2018-03-03T14:10:48Z")

</div>

Thanks for your reply.  
Ran a test on name 3 with the lowercase\_normalizer.

It did return values for Aa\*

```
"query": {
    "wildcard" : { "metaSite.name3" : "Aa*" }
}

```

So What my actual question is

1. is this the best practice? (keyword + lowercase\_normalizer)
2. My field now is keyword. But according to ES doc I think it would better be text (since it is a name and is a free text).

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [March 3, 2018, 3:11pm UTC](https://discuss.elastic.co/t/wildcard-search-case-insensitive/122337/4 "2018-03-03T15:11:00Z")

</div>

Hmmm. That's interesting. I was not expecting that.  
I can indeed reproduce it. I'll check internally on that and will update.

Anyway, yes using keyword for fulltext search is not a good idea IMO.  
And again using wildcard query is generally a bad idea.

---

<div class="post-metadata">

**Author:** ![stam585](https://avatars.discourse-cdn.com/v4/letter/s/f1d935/32.png) [@stam585](https://discuss.elastic.co/u/stam585)\
**Post date:** [March 3, 2018, 3:15pm UTC](https://discuss.elastic.co/t/wildcard-search-case-insensitive/122337/5 "2018-03-03T15:15:55Z")

</div>

Thanks.  
This is an old system.

1. What is your recommendation?
2. Should I turn it to text data type (it is a name field)  
If the user requirement is to search text with prefix and I want to maintain case insensitivity search.
3. if the main usage if prefix then I could use prefix instead of wildcard. Will it improve the query (since wildcard uses the string as a prefix to \* as in Aa\*).

in general, best practice or guid lines to wildcard case insensitive search in ES is hard to find.

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [March 3, 2018, 3:41pm UTC](https://discuss.elastic.co/t/wildcard-search-case-insensitive/122337/6 "2018-03-03T15:41:51Z")

</div>

Using edge ngram based analyzer is preferable.  
You pay the price at index time but not at search time.

See [https://www.elastic.co/guide/en/elasticsearch/reference/6.2/analysis-edgengram-tokenizer.html](https://www.elastic.co/guide/en/elasticsearch/reference/6.2/analysis-edgengram-tokenizer.html)

---

<div class="post-metadata">

**Author:** ![stam585](https://avatars.discourse-cdn.com/v4/letter/s/f1d935/32.png) [@stam585](https://discuss.elastic.co/u/stam585)\
**Post date:** [March 3, 2018, 5:26pm UTC](https://discuss.elastic.co/t/wildcard-search-case-insensitive/122337/7 "2018-03-03T17:26:08Z")

</div>

In this example I would need to write down the max gram parameter.  
I really don't know what it would be since it is a name filed filled by the user.

I thought a better option would be using multi field.  
[https://www.elastic.co/guide/en/elasticsearch/reference/current/multi-fields.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/multi-fields.html)  
What do you think?

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [March 3, 2018, 10:37pm UTC](https://discuss.elastic.co/t/wildcard-search-case-insensitive/122337/8 "2018-03-03T22:37:18Z")

</div>

Multifield is generally a good solution when you want to have multiple ways of searching the same thing or have multiple use cases.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [March 31, 2018, 10:37pm UTC](https://discuss.elastic.co/t/wildcard-search-case-insensitive/122337/9 "2018-03-31T22:37:26Z")

</div>

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