# Wildcard / regexp in a phrase which has space

**URL:** <https://discuss.elastic.co/t/wildcard-regexp-in-a-phrase-which-has-space/117962>\
**Category:** Elasticsearch\
**Created:** [February 1, 2018, 8:21am UTC](https://discuss.elastic.co/t/wildcard-regexp-in-a-phrase-which-has-space/117962 "2018-02-01T08:21:21Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![anoopvalluthadam](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/anoopvalluthadam/32/30286_2.png) [@anoopvalluthadam](https://discuss.elastic.co/u/anoopvalluthadam)\
**Post date:** [February 1, 2018, 8:21am UTC](https://discuss.elastic.co/t/wildcard-regexp-in-a-phrase-which-has-space/117962/1 "2018-02-01T08:21:21Z")

</div>

How to match phrases using wildcards.  
Example:  
I wanna search `clean* car*` in

1. Cleaning car and room
2. Cleaning room and car

It should return only `1`

I tried and what it returns is,` it will search for clean* AND car*`

My mapping:

> PUT my\_test  
> {  
> "mappings": {  
> "my\_type": {  
> "properties": {  
> "city": {  
> "type": "text",  
> "fields": {  
> "raw": {  
> "type": "text",  
> "index": "true"  
> },  
> "raw2": {  
> "type": "keyword"
> 
> }  
> }  
> }  
> }  
> }  
> }  
> }

> PUT my\_test/my\_type/1  
> {  
> "city": "Cleaning room and car"  
> }  
> PUT my\_test/my\_type/1  
> {  
> "city": "Cleaning car and room"  
> }

---

<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:** [February 1, 2018, 8:41am UTC](https://discuss.elastic.co/t/wildcard-regexp-in-a-phrase-which-has-space/117962/2 "2018-02-01T08:41:41Z")

</div>

I'd probably use a edge ngram based analyzer and a phrase query. That should work I guess. I never tried it though.

---

<div class="post-metadata">

**Author:** ![anoopvalluthadam](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/anoopvalluthadam/32/30286_2.png) [@anoopvalluthadam](https://discuss.elastic.co/u/anoopvalluthadam)\
**Post date:** [February 1, 2018, 8:54am UTC](https://discuss.elastic.co/t/wildcard-regexp-in-a-phrase-which-has-space/117962/3 "2018-02-01T08:54:13Z")

</div>

Thanks. Let me try

---

<div class="post-metadata">

**Author:** ![anoopvalluthadam](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/anoopvalluthadam/32/30286_2.png) [@anoopvalluthadam](https://discuss.elastic.co/u/anoopvalluthadam)\
**Post date:** [February 1, 2018, 8:55am UTC](https://discuss.elastic.co/t/wildcard-regexp-in-a-phrase-which-has-space/117962/4 "2018-02-01T08:55:36Z")

</div>

ES directly supporting these kind of wildcards?

None of the forums has a clear answer for this, that's why that straight question. Will ES support this?

---

<div class="post-metadata">

**Author:** ![anoopvalluthadam](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/anoopvalluthadam/32/30286_2.png) [@anoopvalluthadam](https://discuss.elastic.co/u/anoopvalluthadam)\
**Post date:** [February 1, 2018, 9:17am UTC](https://discuss.elastic.co/t/wildcard-regexp-in-a-phrase-which-has-space/117962/5 "2018-02-01T09:17:01Z")

</div>

David,

It is not working with ngram

If I am searching for clean\* car\*  
I wanna match "Cleaning car and room" only, though second option "Cleaning room and car" has both the words. even with ngram it is matching both the docs

---

<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:** [February 1, 2018, 9:23am UTC](https://discuss.elastic.co/t/wildcard-regexp-in-a-phrase-which-has-space/117962/6 "2018-02-01T09:23:29Z")

</div>

Could you provide a full recreation script as described in [About the Elasticsearch category](https://discuss.elastic.co/t/about-the-elasticsearch-category/21). It will help to better understand what you are doing. Please, try to keep the example as simple as possible.

---

<div class="post-metadata">

**Author:** ![anoopvalluthadam](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/anoopvalluthadam/32/30286_2.png) [@anoopvalluthadam](https://discuss.elastic.co/u/anoopvalluthadam)\
**Post date:** [February 1, 2018, 9:29am UTC](https://discuss.elastic.co/t/wildcard-regexp-in-a-phrase-which-has-space/117962/7 "2018-02-01T09:29:51Z")

</div>

Create an index:

```
PUT my_index
{
  "settings": {
    "analysis": {
      "analyzer": {
        "my_analyzer": {
          "tokenizer": "my_tokenizer"
        }
      },
      "tokenizer": {
        "my_tokenizer": {
          "type": "edge_ngram",
          "min_gram": 3,
          "max_gram": 3,
          "token_chars": [
            "letter",
            "digit"
          ]
        }
      }
    }
  },
  "mappings": {
    "my_type": {
      "properties": {
        "city": {
          "type": "keyword",
          "fields": {
            "raw": { 
              "type": "text",
              "analyzer": "my_analyzer"
            }
          }
        }
      }
    }
  }
  
}

POST my_index/my_type/1
{
  "text": "2 #Quick Foxes lived and died"
}

POST my_index/my_type/2
    {
      "text": "2 #Quick Foxes lived died"
    }

```

Now when we search

```
GET my_index/my_type/_search
{
  "query": {
    "query_string": {
      "default_operator" : "AND",
       "query" : "f* d*",
      "fields": ["text.raw"]
    }
  }
}

```

Only ID 2 should list. But nothing returns.

when you try this

```
GET my_index/my_type/_search
{
 "query": {
   "query_string": {
	 "default_operator" : "AND",
	  "query" : "f* d*",
	 "fields": ["text"]
   }
 }
}

```

It will return both

---

<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:** [February 1, 2018, 9:59am UTC](https://discuss.elastic.co/t/wildcard-regexp-in-a-phrase-which-has-space/117962/8 "2018-02-01T09:59:41Z")

</div>

But you did not try a phrase query here.

---

<div class="post-metadata">

**Author:** ![anoopvalluthadam](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/anoopvalluthadam/32/30286_2.png) [@anoopvalluthadam](https://discuss.elastic.co/u/anoopvalluthadam)\
**Post date:** [February 1, 2018, 10:28am UTC](https://discuss.elastic.co/t/wildcard-regexp-in-a-phrase-which-has-space/117962/9 "2018-02-01T10:28:22Z")

</div>

are you talking about something like this?

```
GET my_index_1/my_type/_search
{
  "query": {
    "match_phrase": {
       "text.raw" : "f* d*"
      
    }
  }
}

```

Anyway, above query is not working

---

<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:** [February 1, 2018, 10:46am UTC](https://discuss.elastic.co/t/wildcard-regexp-in-a-phrase-which-has-space/117962/10 "2018-02-01T10:46:34Z")

</div>

Yes but not with any wildcard

---

<div class="post-metadata">

**Author:** ![anoopvalluthadam](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/anoopvalluthadam/32/30286_2.png) [@anoopvalluthadam](https://discuss.elastic.co/u/anoopvalluthadam)\
**Post date:** [February 1, 2018, 11:00am UTC](https://discuss.elastic.co/t/wildcard-regexp-in-a-phrase-which-has-space/117962/11 "2018-02-01T11:00:15Z")

</div>

David,

I am looking for wildcard. Is there any option which supports wildcards which I have mentioned in the previous posts?

requirement is something like this: We have a huge data set of movies. Customers will search using wildcards. how do we support those kinds of wildcards. Examples are mentioned above. Even a phrase has a wildcards, I wanted to support it.

---

<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:** [February 1, 2018, 2:41pm UTC](https://discuss.elastic.co/t/wildcard-regexp-in-a-phrase-which-has-space/117962/12 "2018-02-01T14:41:15Z")

</div>

So. I don't know from the top of my head and I probably need to test. Unfortunately I don't have a lot of time as we speak.

But let me share my thoughts about search engines. Never ever a end user should have to worry about wildcards. As an example, when I'm searching for a movie on Google, I never ever use wildcards. Instead the search engine helps me to find what I'm looking for.

I'm not saying that you should not do it. May be you have a different use case than a regular search, but I just wanted to share my thoughts.

---

<div class="post-metadata">

**Author:** ![anoopvalluthadam](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/anoopvalluthadam/32/30286_2.png) [@anoopvalluthadam](https://discuss.elastic.co/u/anoopvalluthadam)\
**Post date:** [February 1, 2018, 3:27pm UTC](https://discuss.elastic.co/t/wildcard-regexp-in-a-phrase-which-has-space/117962/13 "2018-02-01T15:27:10Z")

</div>

Everything makes sense, but still I couldn't find any solution for my problem from any of the blogs / forums. Looks like either nobody tried this (such a big user base, I can't believe that nobody tried) or elasticsearch does not support it

---

<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:** [February 1, 2018, 4:41pm UTC](https://discuss.elastic.co/t/wildcard-regexp-in-a-phrase-which-has-space/117962/14 "2018-02-01T16:41:13Z")

</div>

Here is what I meant:

```auto
DELETE test
PUT test
{
  "settings": {
    "analysis": {
      "analyzer": {
        "my_analyzer": {
          "tokenizer": "standard",
          "filter": [
            "lowercase", "ngram"
          ]
        }
      },
      "filter": {
        "ngram": {
          "type": "edge_ngram",
          "min_gram": 1,
          "max_gram": 5,
          "token_chars": [
            "letter",
            "digit"
          ]
        }
      }
    }
  },
  "mappings": {
    "doc": {
      "properties": {
        "text": {
          "type": "text",
          "analyzer": "my_analyzer"
        }
      }
    }
  }
}
PUT test/doc/1
{
  "text": "2 #Quick Foxes lived and died"
}
PUT test/doc/2
{
  "text": "2 #Quick Foxes lived died"
}
GET test/_search
{
  "query": {
    "match_phrase": {
      "text": "liv die"
    }
  }
}

```

It gives:

```auto
{
  "took": 5,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 3.231833,
    "hits": [
      {
        "_index": "test",
        "_type": "doc",
        "_id": "2",
        "_score": 3.231833,
        "_source": {
          "text": "2 #Quick Foxes lived died"
        }
      }
    ]
  }
}

```

---

<div class="post-metadata">

**Author:** ![anoopvalluthadam](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/anoopvalluthadam/32/30286_2.png) [@anoopvalluthadam](https://discuss.elastic.co/u/anoopvalluthadam)\
**Post date:** [February 2, 2018, 12:56am UTC](https://discuss.elastic.co/t/wildcard-regexp-in-a-phrase-which-has-space/117962/15 "2018-02-02T00:56:50Z")

</div>

Great. You mean to say that it will work with out \* and have the same effect of a wildcard. 🙂

---

<div class="post-metadata">

**Author:** ![anoopvalluthadam](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/anoopvalluthadam/32/30286_2.png) [@anoopvalluthadam](https://discuss.elastic.co/u/anoopvalluthadam)\
**Post date:** [February 2, 2018, 2:28am UTC](https://discuss.elastic.co/t/wildcard-regexp-in-a-phrase-which-has-space/117962/16 "2018-02-02T02:28:59Z")

</div>

It has issues.

```
DELETE test
PUT test
{
  "settings": {
    "analysis": {
      "analyzer": {
        "my_analyzer": {
          "tokenizer": "standard",
          "filter": [
            "lowercase", "ngram"
          ]
        }
      },
      "filter": {
        "ngram": {
          "type": "edge_ngram",
          "min_gram": 1,
          "max_gram": 20,
          "token_chars": [
            "letter",
            "digit"
          ]
        }
      }
    }
  },
  "mappings": {
    "doc": {
      "properties": {
        "text": {
          "type": "text",
          "analyzer": "my_analyzer"
        }
      }
    }
  }
}
PUT test/doc/1
{
  "text": "2 #Quick Foxes lived and died"
}
PUT test/doc/2
{
  "text": "2 #Quick Foxes lived died"
}
PUT test/doc/3
{
  "text": "2 #Quick Foxes lived died and resurrected their wys "
}
PUT test/doc/4
{
  "text": """Sports Report:
Cricket - The Adelaide Strikers have blasted their way back to the top of the Big Bash ladder, after beating the Melbourne Stars."""
}

```

And the query is

```
GET test/_search
{
  "query": {
    "match_phrase": {
      "text": "th wa"
    }
  }
}

```

Result is

```
{
  "took": 0,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 2,
    "max_score": 4.558014,
    "hits": [
      {
        "_index": "test",
        "_type": "doc",
        "_id": "4",
        "_score": 4.558014,
        "_source": {
          "text": "Sports Report:\nCricket - The Adelaide Strikers have blasted their way back to the top of the Big Bash ladder, after beating the Melbourne Stars."
        }
      },
      {
        "_index": "test",
        "_type": "doc",
        "_id": "3",
        "_score": 3.9283106,
        "_source": {
          "text": "2 #Quick Foxes lived died and resurrected their wys "
        }
      }
    ]
  }
}

```

of-course the second one is wrong.

becuse query was

> th wa

so it should not bring anything with

> wy

isn't it?

---

<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:** [February 2, 2018, 7:19am UTC](https://discuss.elastic.co/t/wildcard-regexp-in-a-phrase-which-has-space/117962/17 "2018-02-02T07:19:58Z")

</div>

Sure.

This is what you want:

```auto
DELETE test
PUT test
{
  "settings": {
    "analysis": {
      "analyzer": {
        "my_analyzer": {
          "tokenizer": "standard",
          "filter": [
            "lowercase", "ngram"
          ]
        }
      },
      "filter": {
        "ngram": {
          "type": "edge_ngram",
          "min_gram": 1,
          "max_gram": 20,
          "token_chars": [
            "letter",
            "digit"
          ]
        }
      }
    }
  },
  "mappings": {
    "doc": {
      "properties": {
        "text": {
          "type": "text",
          "analyzer": "my_analyzer",
          "search_analyzer": "simple"
        }
      }
    }
  }
}
PUT test/doc/1
{
  "text": "2 #Quick Foxes lived and died"
}
PUT test/doc/2
{
  "text": "2 #Quick Foxes lived died"
}
PUT test/doc/3
{
  "text": "2 #Quick Foxes lived died and resurrected their wys "
}
PUT test/doc/4
{
  "text": """Sports Report:
Cricket - The Adelaide Strikers have blasted their way back to the top of the Big Bash ladder, after beating the Melbourne Stars."""
}
POST test/_refresh
GET test/_search
{
  "query": {
    "match_phrase": {
      "text": "th wa"
    }
  }
}

```

It gives:

```auto
{
  "took": 13,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 1.8853602,
    "hits": [
      {
        "_index": "test",
        "_type": "doc",
        "_id": "4",
        "_score": 1.8853602,
        "_source": {
          "text": "Sports Report:\nCricket - The Adelaide Strikers have blasted their way back to the top of the Big Bash ladder, after beating the Melbourne Stars."
        }
      }
    ]
  }
}

```

---

<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:** [February 2, 2018, 7:21am UTC](https://discuss.elastic.co/t/wildcard-regexp-in-a-phrase-which-has-space/117962/18 "2018-02-02T07:21:39Z")

</div>

BTW in general I'd not start with only one letter but at least 2 letters in the edge ngram.

---

<div class="post-metadata">

**Author:** ![anoopvalluthadam](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/anoopvalluthadam/32/30286_2.png) [@anoopvalluthadam](https://discuss.elastic.co/u/anoopvalluthadam)\
**Post date:** [February 2, 2018, 8:20am UTC](https://discuss.elastic.co/t/wildcard-regexp-in-a-phrase-which-has-space/117962/19 "2018-02-02T08:20:33Z")

</div>

Thanks @dadoonet. Working on it. Will let you know.

---

<div class="post-metadata">

**Author:** ![anoopvalluthadam](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/anoopvalluthadam/32/30286_2.png) [@anoopvalluthadam](https://discuss.elastic.co/u/anoopvalluthadam)\
**Post date:** [February 2, 2018, 9:42am UTC](https://discuss.elastic.co/t/wildcard-regexp-in-a-phrase-which-has-space/117962/20 "2018-02-02T09:42:42Z")

</div>

@dadoonet The solution which you have given is fine for applications like word complete!

say for example, if I am searching like

> rts Re

it should return

> Sports Report

Any idea how can I do that?

[Next page](https://discuss.elastic.co/t/wildcard-regexp-in-a-phrase-which-has-space/117962.md?page=2)
