# Searching on text fields using wildcard

**URL:** <https://discuss.elastic.co/t/searching-on-text-fields-using-wildcard/248270>\
**Category:** Elasticsearch\
**Created:** [September 11, 2020, 7:03am UTC](https://discuss.elastic.co/t/searching-on-text-fields-using-wildcard/248270 "2020-09-11T07:03:33Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![vasek](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/vasek/32/136636_2.png) [@vasek](https://discuss.elastic.co/u/vasek)\
**Post date:** [September 11, 2020, 7:03am UTC](https://discuss.elastic.co/t/searching-on-text-fields-using-wildcard/248270/1 "2020-09-11T07:03:33Z")

</div>

Hello,  
I have simple question about searching on **text** field.

Let's assume we have a text field `message` with value:  
`This is example of search query. Can anyone help me?`

```
PUT index
{
  "mappings": {
    "properties": {
      "message" : {
        "type" : "text"
      }
    }
  }
}

POST index/_doc
{
  "message": "This is example of search query. Can anyone help me?"
}

```

I'm using **standard** analyzer. So sentence is tokenized like this:

```
POST _analyze
{
  "text": "This is example of search query. Can anyone help me?",
  "analyzer": "standard"
}

```

I found out that **wildcard** is possible to apply only on tokenized **terms**.

```
POST index/_search
{
  "query": {
    "query_string": {
      "default_field": "message",
      "query": "s*r*h"
    }
  }
}

```

and

```
GET index/_search
{
  "query": {
    "wildcard": {
      "message": {
        "value": "s*r*h*"
      }
    }
  }
}

```

and

```
GET index/_search
{
  "query": {
    "regexp": {
      "message": {
        "value": "s.*ch",
        "flags": "ALL"
      }
    }
  }
}

```

**Is it possible to perform query on field `message` that will met these requirements?**

1. field begins with tokens **this** , **is**
2. somewhere after these tokens is token **that**
3. field ends with token **me**

**or is necessary to index field message as `keyword` type too and perform regexp query like** : `/^This is.*that.*me/`

---

<div class="post-metadata">

**Author:** ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)\
**Post date:** [September 12, 2020, 8:07am UTC](https://discuss.elastic.co/t/searching-on-text-fields-using-wildcard/248270/2 "2020-09-12T08:07:04Z")

</div>

If you’re always searching for whole-words in a sequence then [interval](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-intervals-query.html) queries on a text field would work.

Keyword field would allow for whole-value matching using regex or wildcard, however in recently released 7.9 we have what may be a better option [https://www.elastic.co/blog/find-strings-within-strings-faster-with-the-new-elasticsearch-wildcard-field](https://www.elastic.co/blog/find-strings-within-strings-faster-with-the-new-elasticsearch-wildcard-field)

---

<div class="post-metadata">

**Author:** ![vasek](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/vasek/32/136636_2.png) [@vasek](https://discuss.elastic.co/u/vasek)\
**Post date:** [September 14, 2020, 6:48am UTC](https://discuss.elastic.co/t/searching-on-text-fields-using-wildcard/248270/3 "2020-09-14T06:48:48Z")

</div>

Thank you @Mark_Harwood for explanation.

The interval queries are possible only with **DSL**?  
Is it possible to perform interval queries by **KQL** or **Lucene Query**?

---

<div class="post-metadata">

**Author:** ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)\
**Post date:** [September 14, 2020, 10:58am UTC](https://discuss.elastic.co/t/searching-on-text-fields-using-wildcard/248270/4 "2020-09-14T10:58:30Z")

</div>

> [@vasek](#):
>
> The interval queries are possible only with **DSL**?

Yes. If I recall correctly the Kibana search bar accepts DSL if you have the "Lucene" syntax option enabled - you search string just has to be JSON so starting with a `{`

KQL and Lucene both support basic phrase queries if you put quotes around words e.g. `"this that"` but I think only the Lucene syntax supports the `slop` operator that dictates how many positions apart the words can be e.g. `"this that"~2`.  
Neither of these support the more advanced features `interval` queries offer e.g. the ability to search for this or that near something else..

---

<div class="post-metadata">

**Author:** ![vasek](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/vasek/32/136636_2.png) [@vasek](https://discuss.elastic.co/u/vasek)\
**Post date:** [September 15, 2020, 7:22am UTC](https://discuss.elastic.co/t/searching-on-text-fields-using-wildcard/248270/5 "2020-09-15T07:22:12Z")

</div>

Great! I can confirm that DLS works in Kibana when Lucene syntax is enabled.

`{"bool":{"must":[{"match":{"code":200}}]}}`

---

<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:** [October 13, 2020, 7:22am UTC](https://discuss.elastic.co/t/searching-on-text-fields-using-wildcard/248270/6 "2020-10-13T07:22:12Z")

</div>

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