# ElasticSearch - Searching partial text in String

**URL:** <https://discuss.elastic.co/t/elasticsearch-searching-partial-text-in-string/316649>\
**Category:** Elasticsearch\
**Created:** [October 14, 2022, 2:42pm UTC](https://discuss.elastic.co/t/elasticsearch-searching-partial-text-in-string/316649 "2022-10-14T14:42:17Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sachin\_Sharma](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sachin_sharma/32/112079_2.png) [@Sachin\_Sharma](https://discuss.elastic.co/u/Sachin_Sharma)\
**Post date:** [October 14, 2022, 2:42pm UTC](https://discuss.elastic.co/t/elasticsearch-searching-partial-text-in-string/316649/1 "2022-10-14T14:42:17Z")

</div>

What is the best way to use Elasticsearch to search exact partial text in String?

In SQL the method would be: %PARTIAL TEXT%, %ARTIAL TEX%

In Elastic Search current method being used:

```auto
{
    "query": {
        "match_phrase_prefix": {
             "name": "PARTIAL TEXT"
        }
    }
}

```

However, it breaks whenever you remove first and last character of string as shown below (No results found):

```auto
{
    "query": {
        "match_phrase_prefix": {
             "name": "ARTIAL TEX"
        }
    }
}

```

---

<div class="post-metadata">

**Author:** ![allan-silva](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/allan-silva/32/111206_2.png) [@allan-silva](https://discuss.elastic.co/u/allan-silva)\
**Post date:** [October 16, 2022, 3:33am UTC](https://discuss.elastic.co/t/elasticsearch-searching-partial-text-in-string/316649/2 "2022-10-16T03:33:16Z")

</div>

You probably is looking for a wildcard query.

Suppose a index like this, with one document:

```auto
PUT netflix_movie_title
{
    "mappings": {
        "properties": {
            "title": {
                "type": "keyword"
            }
        }
    }
}

POST netflix_movie_title/_doc
{
  "title": "The Hitchhiker's Guide to the Galaxy"
}

```

A wildcard query lets you perform a "SQL LIKE" like query.

Match start:

```auto
POST netflix_movie_title/_search
{
  "query": {
    "wildcard": {
      "title": {
        "value": "The Hitchhiker's Guide to th*"
      }
    }
  }
}

```

Match end:

```auto
POST netflix_movie_title/_search
{
  "query": {
    "wildcard": {
      "title": {
        "value": "*er's Guide to the Galaxy"
      }
    }
  }
}

```

Match middle:

```auto
POST netflix_movie_title/_search
{
  "query": {
    "wildcard": {
      "title": {
        "value": "*er's Guide to the Gal*"
      }
    }
  }
}

```

No match:

```auto
POST netflix_movie_title/_search
{
  "query": {
    "wildcard": {
      "title": {
        "value": "*er's Gui o the Gal*"
      }
    }
  }
}

```

However, the keyword field is designed to do exact match queries. If you needs full text search capabilities too, consider use a multifield mapping ([Field data types | Elasticsearch Guide [8.4] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-types.html#types-multi-fields)).

---

<div class="post-metadata">

**Author:** ![Mark\_Harwood1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood1/32/101255_2.png) [@Mark\_Harwood1](https://discuss.elastic.co/u/Mark_Harwood1)\
**Post date:** [October 16, 2022, 8:54am UTC](https://discuss.elastic.co/t/elasticsearch-searching-partial-text-in-string/316649/3 "2022-10-16T08:54:37Z")

</div>

Running wildcard queries on keyword fields has two problems:

1. It wont work on large values
2. the search cost is linear with the number of unique values

That’s why the wildcard field was created and [this blog](https://www.elastic.co/blog/find-strings-within-strings-faster-with-the-new-elasticsearch-wildcard-field) gives the background. This too has shortcomings because the search cost is linear with the number of docs that hold a value that roughly matches the search.  
There’s always some kind of performance trade off.

---

<div class="post-metadata">

**Author:** ![allan-silva](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/allan-silva/32/111206_2.png) [@allan-silva](https://discuss.elastic.co/u/allan-silva)\
**Post date:** [October 16, 2022, 11:59am UTC](https://discuss.elastic.co/t/elasticsearch-searching-partial-text-in-string/316649/4 "2022-10-16T11:59:59Z")

</div>

Totally agree, performance issues should be considered on this use case. The blog post above has a guide to choose data type. If you not sure what data type to use. A possible approach is create a new sample index to explore your data using the desired type, and use the reindex API to reindex part or the whole production index.

---

<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:** [November 13, 2022, 12:00pm UTC](https://discuss.elastic.co/t/elasticsearch-searching-partial-text-in-string/316649/5 "2022-11-13T12:00:15Z")

</div>

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