# Wildcard search for exact phrase

**URL:** <https://discuss.elastic.co/t/wildcard-search-for-exact-phrase/258022>\
**Category:** Elasticsearch\
**Created:** [December 8, 2020, 3:59pm UTC](https://discuss.elastic.co/t/wildcard-search-for-exact-phrase/258022 "2020-12-08T15:59:52Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![vadim-permakoff](https://avatars.discourse-cdn.com/v4/letter/v/77aa72/32.png) [@vadim-permakoff](https://discuss.elastic.co/u/vadim-permakoff)\
**Post date:** [December 8, 2020, 3:59pm UTC](https://discuss.elastic.co/t/wildcard-search-for-exact-phrase/258022/1 "2020-12-08T15:59:53Z")

</div>

We want to migrate from Solr to Elasticsearch and everything looks fine, but there is one task we are doing in Solr for which I cannot find a solution using Elasticsearch. I need to find exactly matching text in a content of large document, which can start and end at the middle of any word. For example, if the document has words in exact phrase: "one two three", I need to find this document if the user searches for: "\*ne two thr\*". Note one wildcard at the beginning of the phrase and one at the end. I need to find only exactly matching phrase, which contains spaces. In Solr I can achieve this using Complex Phrase Query Parser with ReversedWildcardFilterFactory filter. How can it be done in Elasticsearch?

---

<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:** [December 8, 2020, 4:53pm UTC](https://discuss.elastic.co/t/wildcard-search-for-exact-phrase/258022/2 "2020-12-08T16:53:51Z")

</div>

Hi Vadim. Check out [interval](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-intervals-query.html) queries.

You may need to parse the user input (using the [analyze API)](https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-analyze.html) and use the terms in the result to construct the equivalent JSON for the interval query you need.

---

<div class="post-metadata">

**Author:** ![vadim-permakoff](https://avatars.discourse-cdn.com/v4/letter/v/77aa72/32.png) [@vadim-permakoff](https://discuss.elastic.co/u/vadim-permakoff)\
**Post date:** [December 10, 2020, 6:46pm UTC](https://discuss.elastic.co/t/wildcard-search-for-exact-phrase/258022/3 "2020-12-10T18:46:32Z")

</div>

Thank you Mark! Following your advice I tried the following query:

```auto
{
  "query": {
    "intervals" : {
      "_text_" : {
        "all_of" : {
          "ordered" : true,
          "intervals" : [
            {
              "wildcard": {
                "pattern": "*olicy"
              }
            },
            {
              "match": {
                "query": "changes. This endorsement ",
                "max_gaps" : 0,
                "ordered" : true
              }
            },
            {
              "wildcard": {
                "pattern": "modi*"
              }
            }
          ]
        }
      }
    }
  }
}

```

Works like a charm, even without reversed filter. Would reverse filter improve the search speed? Also it looks like I'll have to increase the number of partial matches using 'index-prefixes' option. Is there anything else I should do to improve such queries?

---

<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:** [December 10, 2020, 7:48pm UTC](https://discuss.elastic.co/t/wildcard-search-for-exact-phrase/258022/4 "2020-12-10T19:48:41Z")

</div>

The use of reverse-indexed terms would help but I’m not clear if a positional query that mixes prefix and normal terms is smart enough to match positions across multiple Lucene fields - @romseygeek might be able to help with this question.

---

<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:** [January 7, 2021, 7:48pm UTC](https://discuss.elastic.co/t/wildcard-search-for-exact-phrase/258022/5 "2021-01-07T19:48:49Z")

</div>

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