# Wildcard queries and case (in)sensitivity

**URL:** <https://discuss.elastic.co/t/wildcard-queries-and-case-in-sensitivity/9014>\
**Category:** Elasticsearch\
**Created:** [September 13, 2012, 3:36pm UTC](https://discuss.elastic.co/t/wildcard-queries-and-case-in-sensitivity/9014 "2012-09-13T15:36:27Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![LittleGrimmy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/littlegrimmy/32/2714_2.png) [@LittleGrimmy](https://discuss.elastic.co/u/LittleGrimmy)\
**Post date:** [September 13, 2012, 3:36pm UTC](https://discuss.elastic.co/t/wildcard-queries-and-case-in-sensitivity/9014/1 "2012-09-13T15:36:27Z")

</div>

Hi!

I'm in the process of indexing a couple of millions of documents, and the  
requirements for the search are:

- Be able to search for documents in a case-insensitive way
- Be able to search for documents in a case-sensitive way
- Be able to search using wildcards (also in phrases)

As far as I understand, to achieve case-insensitivity, I should have two  
indexes; one lower-cased, and one where everything is stored as it is.

So far, I've tried the following code:

#!/usr/bin/env perl

# 

use strict;  
use warnings;  
use Data::Dumper;

use utf8;  
use ElasticSearch;

my $ES = ElasticSearch-\>new(  
servers =\> ['[foobar.com:9200](http://foobar.com:9200)' ],  
transport =\> 'httplite',  
);

$ES-\>delete\_index(  
index =\> 'foobar',  
ignore\_missing =\> 1,  
);

$ES-\>create\_index(  
index =\> 'foobar',  
settings =\> {  
number\_of\_shards =\> 1,  
number\_of\_replicas =\> 0,  
analysis =\> {  
analyzer =\> {  
complex =\> {  
type =\> 'custom',  
tokenizer =\> 'keyword',  
filter =\> ['trim'],  
char\_filter =\> ['html\_strip'],  
stopwords =\> [],  
},  
},  
},  
},  
mappings =\> {  
article =\> {  
properties =\> {  
id =\> { type =\> 'integer' },  
body =\> { type =\> 'string', analyzer =\> 'complex' },  
},  
},  
},  
);

$ES-\>index(  
index =\> 'foobar',  
type =\> 'document',  
refresh =\> 1,  
id =\> 1,  
data =\> {  
id =\> 1,  
body =\> 'Mexico's military has notched an important success for  
President Felipe Calderon with the arrest of Jorge Eduardo Costilla  
Sanchez, boss of the powerful Gulf drug cartel.',  
},  
);

my $res = $ES-\>searchqs(  
index =\> 'foobar',  
type =\> 'document',  
fields =\> ['body'],  
q =\> '"Felipe Calder\*"',  
analyze\_wildcard =\> 1,  
explain =\> 1,  
analyzer =\> 'complex',  
);

print Dumper( $res );

...but this doesn't work. Any idea how I should proceed?

Thanks in advance!

--

---

<div class="post-metadata">

**Author:** ![Ivan](https://avatars.discourse-cdn.com/v4/letter/i/df788c/32.png) [@Ivan](https://discuss.elastic.co/u/Ivan)\
**Post date:** [September 14, 2012, 2:23pm UTC](https://discuss.elastic.co/t/wildcard-queries-and-case-in-sensitivity/9014/2 "2012-09-14T14:23:35Z")

</div>

I do not see how you are handling both case sensitive and in-sensitive queries.

In your case-sensitive example, you are use a keyword tokenizer on  
your string field, but means the entire field will be one token. I  
have never used a wildcard query on a non-tokenized field, but I would  
assume it would not work for non-prefix queries. Can you try changing  
the tokenizer?

--  
Ivan

On Thu, Sep 13, 2012 at 8:36 AM, LittleGrimmy [toreau@gmail.com](mailto:toreau@gmail.com) wrote:

> Hi!
> 
> I'm in the process of indexing a couple of millions of documents, and the  
> requirements for the search are:
> 
> - Be able to search for documents in a case-insensitive way
> - Be able to search for documents in a case-sensitive way
> - Be able to search using wildcards (also in phrases)
> 
> As far as I understand, to achieve case-insensitivity, I should have two  
> indexes; one lower-cased, and one where everything is stored as it is.
> 
> So far, I've tried the following code:
> 
> #!/usr/bin/env perl
> 
> # 
> 
> use strict;  
> use warnings;  
> use Data::Dumper;
> 
> use utf8;  
> use Elasticsearch;
> 
> my $ES = Elasticsearch-\>new(  
> servers =\> ['[foobar.com:9200](http://foobar.com:9200)' ],  
> transport =\> 'httplite',  
> );
> 
> $ES-\>delete\_index(  
> index =\> 'foobar',  
> ignore\_missing =\> 1,  
> );
> 
> $ES-\>create\_index(  
> index =\> 'foobar',  
> settings =\> {  
> number\_of\_shards =\> 1,  
> number\_of\_replicas =\> 0,  
> analysis =\> {  
> analyzer =\> {  
> complex =\> {  
> type =\> 'custom',  
> tokenizer =\> 'keyword',  
> filter =\> ['trim'],  
> char\_filter =\> ['html\_strip'],  
> stopwords =\> ,  
> },  
> },  
> },  
> },  
> mappings =\> {  
> article =\> {  
> properties =\> {  
> id =\> { type =\> 'integer' },  
> body =\> { type =\> 'string', analyzer =\> 'complex' },  
> },  
> },  
> },  
> );
> 
> $ES-\>index(  
> index =\> 'foobar',  
> type =\> 'document',  
> refresh =\> 1,  
> id =\> 1,  
> data =\> {  
> id =\> 1,  
> body =\> 'Mexico's military has notched an important success for  
> President Felipe Calderon with the arrest of Jorge Eduardo Costilla Sanchez,  
> boss of the powerful Gulf drug cartel.',  
> },  
> );
> 
> my $res = $ES-\>searchqs(  
> index =\> 'foobar',  
> type =\> 'document',  
> fields =\> ['body'],  
> q =\> '"Felipe Calder\*"',  
> analyze\_wildcard =\> 1,  
> explain =\> 1,  
> analyzer =\> 'complex',  
> );
> 
> print Dumper( $res );
> 
> ...but this doesn't work. Any idea how I should proceed?
> 
> Thanks in advance!
> 
> --

--

---

<div class="post-metadata">

**Author:** ![LittleGrimmy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/littlegrimmy/32/2714_2.png) [@LittleGrimmy](https://discuss.elastic.co/u/LittleGrimmy)\
**Post date:** [September 14, 2012, 3:05pm UTC](https://discuss.elastic.co/t/wildcard-queries-and-case-in-sensitivity/9014/3 "2012-09-14T15:05:29Z")

</div>

Actually, I left out the case-insensitive thing in my example, because  
that's the easy part. 🙂

The problem is, given the mentioned string/text, I'm unable to search for  
phrases and/or wildcard-ed queries no matter which tokenizer I'm using. So  
I'm looking for an example on how to achieve that.

On 14 September 2012 16:23, Ivan Brusic [ivan@brusic.com](mailto:ivan@brusic.com) wrote:

> I do not see how you are handling both case sensitive and in-sensitive  
> queries.
> 
> In your case-sensitive example, you are use a keyword tokenizer on  
> your string field, but means the entire field will be one token. I  
> have never used a wildcard query on a non-tokenized field, but I would  
> assume it would not work for non-prefix queries. Can you try changing  
> the tokenizer?
> 
> --  
> Ivan
> 
> On Thu, Sep 13, 2012 at 8:36 AM, LittleGrimmy [toreau@gmail.com](mailto:toreau@gmail.com) wrote:
> 
> > Hi!
> > 
> > I'm in the process of indexing a couple of millions of documents, and the  
> > requirements for the search are:
> > 
> > - Be able to search for documents in a case-insensitive way
> > - Be able to search for documents in a case-sensitive way
> > - Be able to search using wildcards (also in phrases)
> > 
> > As far as I understand, to achieve case-insensitivity, I should have two  
> > indexes; one lower-cased, and one where everything is stored as it is.
> > 
> > So far, I've tried the following code:
> > 
> > #!/usr/bin/env perl
> > 
> > # 
> > 
> > use strict;  
> > use warnings;  
> > use Data::Dumper;
> > 
> > use utf8;  
> > use Elasticsearch;
> > 
> > my $ES = Elasticsearch-\>new(  
> > servers =\> ['[foobar.com:9200](http://foobar.com:9200)' ],  
> > transport =\> 'httplite',  
> > );
> > 
> > $ES-\>delete\_index(  
> > index =\> 'foobar',  
> > ignore\_missing =\> 1,  
> > );
> > 
> > $ES-\>create\_index(  
> > index =\> 'foobar',  
> > settings =\> {  
> > number\_of\_shards =\> 1,  
> > number\_of\_replicas =\> 0,  
> > analysis =\> {  
> > analyzer =\> {  
> > complex =\> {  
> > type =\> 'custom',  
> > tokenizer =\> 'keyword',  
> > filter =\> ['trim'],  
> > char\_filter =\> ['html\_strip'],  
> > stopwords =\> ,  
> > },  
> > },  
> > },  
> > },  
> > mappings =\> {  
> > article =\> {  
> > properties =\> {  
> > id =\> { type =\> 'integer' },  
> > body =\> { type =\> 'string', analyzer =\> 'complex' },  
> > },  
> > },  
> > },  
> > );
> > 
> > $ES-\>index(  
> > index =\> 'foobar',  
> > type =\> 'document',  
> > refresh =\> 1,  
> > id =\> 1,  
> > data =\> {  
> > id =\> 1,  
> > body =\> 'Mexico's military has notched an important success for  
> > President Felipe Calderon with the arrest of Jorge Eduardo Costilla  
> > Sanchez,  
> > boss of the powerful Gulf drug cartel.',  
> > },  
> > );
> > 
> > my $res = $ES-\>searchqs(  
> > index =\> 'foobar',  
> > type =\> 'document',  
> > fields =\> ['body'],  
> > q =\> '"Felipe Calder\*"',  
> > analyze\_wildcard =\> 1,  
> > explain =\> 1,  
> > analyzer =\> 'complex',  
> > );
> > 
> > print Dumper( $res );
> > 
> > ...but this doesn't work. Any idea how I should proceed?
> > 
> > Thanks in advance!
> > 
> > --
> 
> --

--  
Tore Aursand [toreau@gmail.com](mailto:toreau@gmail.com)

--

---

<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:** [July 6, 2017, 3:12am UTC](https://discuss.elastic.co/t/wildcard-queries-and-case-in-sensitivity/9014/4 "2017-07-06T03:12:44Z")

</div>


