# How to restrict documents to only include the wildcard search phrase?

**URL:** <https://discuss.elastic.co/t/how-to-restrict-documents-to-only-include-the-wildcard-search-phrase/368949>\
**Category:** Elastic Search\
**Created:** [October 16, 2024, 9:27pm UTC](https://discuss.elastic.co/t/how-to-restrict-documents-to-only-include-the-wildcard-search-phrase/368949 "2024-10-16T21:27:37Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![dev8100](https://avatars.discourse-cdn.com/v4/letter/d/8c91f0/32.png) [@dev8100](https://discuss.elastic.co/u/dev8100)\
**Post date:** [October 16, 2024, 9:27pm UTC](https://discuss.elastic.co/t/how-to-restrict-documents-to-only-include-the-wildcard-search-phrase/368949/1 "2024-10-16T21:27:37Z")

</div>

I want to do a wild card search below to return results that _only_ include the phrase "joe smith". This works but it also includes records without his name. I've played with the "min\_score" value to help on this but I want something to be more precise. If it doesn't include the phrase don't include it in the result.  
Or is my query approach incorrect?

```auto
GET my_index/_search
{
  "query": {
    "query_string": {
      "query": "*\"joe smith\"*",
      "fields": ["*"],
      "analyze_wildcard": true
    }
  },
  "size": 8000,
 "min_score" : 2
}

```

---

<div class="post-metadata">

**Author:** ![Alex\_Salgado-Elastic](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/alex_salgado-elastic/32/103081_2.png) [@Alex\_Salgado-Elastic](https://discuss.elastic.co/u/Alex_Salgado-Elastic)\
**Post date:** [October 16, 2024, 11:12pm UTC](https://discuss.elastic.co/t/how-to-restrict-documents-to-only-include-the-wildcard-search-phrase/368949/2 "2024-10-16T23:12:20Z")

</div>

Hi @dev8100  
Have you try [Match phrase query | Elasticsearch Guide [8.15] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-query-phrase.html)

?

---

<div class="post-metadata">

**Author:** ![dev8100](https://avatars.discourse-cdn.com/v4/letter/d/8c91f0/32.png) [@dev8100](https://discuss.elastic.co/u/dev8100)\
**Post date:** [October 17, 2024, 4:30pm UTC](https://discuss.elastic.co/t/how-to-restrict-documents-to-only-include-the-wildcard-search-phrase/368949/3 "2024-10-17T16:30:42Z")

</div>

Thanks. I tried it

```auto
GET /_search
{
  "query": {
    "match_phrase": {
      "message": "joe smith"
    }
  }
}

```

and it returned 0 results.

```auto
  "hits": {
    "total": {
      "value": 0,
      "relation": "eq"
    },
    "max_score": null,
    "hits": []
  }

```

Is "message" supposed to be field in one of our indexes or is it a generic value that will be searched across all indexes? I'm unclear on that. We'd like to search all fields as of now.

---

<div class="post-metadata">

**Author:** ![ashishtiwari1993](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ashishtiwari1993/32/135241_2.png) [@ashishtiwari1993](https://discuss.elastic.co/u/ashishtiwari1993)\
**Post date:** [October 18, 2024, 9:30am UTC](https://discuss.elastic.co/t/how-to-restrict-documents-to-only-include-the-wildcard-search-phrase/368949/4 "2024-10-18T09:30:44Z")

</div>

Can you share how is your mapping looks like using mapping query -

`GET my-index/_mapping`

---

<div class="post-metadata">

**Author:** ![dev8100](https://avatars.discourse-cdn.com/v4/letter/d/8c91f0/32.png) [@dev8100](https://discuss.elastic.co/u/dev8100)\
**Post date:** [October 18, 2024, 12:44pm UTC](https://discuss.elastic.co/t/how-to-restrict-documents-to-only-include-the-wildcard-search-phrase/368949/5 "2024-10-18T12:44:38Z")

</div>

We want to search across all indexes so each mapping is different

---

<div class="post-metadata">

**Author:** ![ashishtiwari1993](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ashishtiwari1993/32/135241_2.png) [@ashishtiwari1993](https://discuss.elastic.co/u/ashishtiwari1993)\
**Post date:** [October 18, 2024, 5:52pm UTC](https://discuss.elastic.co/t/how-to-restrict-documents-to-only-include-the-wildcard-search-phrase/368949/6 "2024-10-18T17:52:42Z")

</div>

To understand this, we need to see how data has been indexed across the indexes. For that we need to know the mapping. Or if you can share some sample data with index query and what you're looking for. We can do quick poc.

---

<div class="post-metadata">

**Author:** ![Christian\_Dahlqvist](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christian_dahlqvist/32/4617_2.png) [@Christian\_Dahlqvist](https://discuss.elastic.co/u/Christian_Dahlqvist)\
**Post date:** [October 18, 2024, 5:57pm UTC](https://discuss.elastic.co/t/how-to-restrict-documents-to-only-include-the-wildcard-search-phrase/368949/7 "2024-10-18T17:57:14Z")

</div>

The mapping of the fields searched will impact what results a particular query returns. If you index the same string in a field mapped as a keyword and a different fields that is analyzed a specific query may find only one, both or maybe even none of them.

---

<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 15, 2024, 5:57pm UTC](https://discuss.elastic.co/t/how-to-restrict-documents-to-only-include-the-wildcard-search-phrase/368949/8 "2024-11-15T17:57:36Z")

</div>

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