# Why is a wildcard query string matching on stemmed terms?

**URL:** https://discuss.elastic.co/t/why-is-a-wildcard-query-string-matching-on-stemmed-terms/346576
**Category:** Elasticsearch
**Created:** [November 6, 2023, 10:10pm UTC](https://discuss.elastic.co/t/why-is-a-wildcard-query-string-matching-on-stemmed-terms/346576 "2023-11-06T22:10:37Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![cphramington](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/cphramington/32/127566_2.png) [@cphramington](https://discuss.elastic.co/u/cphramington)
#### Post date: [November 6, 2023, 10:10pm UTC](https://discuss.elastic.co/t/why-is-a-wildcard-query-string-matching-on-stemmed-terms/346576/1 "2023-11-06T22:10:37Z")

</div>

First, some background. I understand that the algorithmic stemmer is not perfect, e.g. "focused" is stemmed to "focus," while "focus" is stemmed to "focu," which I've validated by looking through the term vectors.

However, when I execute queries like:

```auto
GET /_search
{
     "query": {
           "query_string": {
                "query": "focus*",
                "analyze_wildcard": True,
                "allow_leading_wildcard": True
            },
    }
}

```

across documents that contain explicitly mapped "text" fields with instances of "focus" and "focused," I retrieve results of both instances.

I assumed that since the wildcard contents wouldn't be analyzed, I would only get results for "focused," since "focu" wouldn't match in the inverted index.

The only conclusion I can draw is that the query is searching the entire source document, rather than solely the inverted index, however I haven't come across any documentation confirming this.

This leaves me with the following questions:

1. How do query string queries containing wildcards search for matches?
2. If the query is doing a full sweep of the source documents, is there a way to disable this behavior and only utilize the inverted index?

Please do not comment on the inefficiency of using wildcard queries, I'm here purely to understand how the operation is being completed.

---

<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: [November 7, 2023, 6:25am UTC](https://discuss.elastic.co/t/why-is-a-wildcard-query-string-matching-on-stemmed-terms/346576/2 "2023-11-07T06:25:31Z")

</div>

You did not specify any [default\_field](https://www.elastic.co/guide/en/elasticsearch/reference/8.10/query-dsl-query-string-query.html#query-string-top-level-params) so I believe all fields in the document that support term queries are searched. In cases like this it always helps if you also provide the mapping you are using.

---

<div class="post-metadata">

### Author: ![cphramington](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/cphramington/32/127566_2.png) [@cphramington](https://discuss.elastic.co/u/cphramington)
#### Post date: [November 7, 2023, 3:05pm UTC](https://discuss.elastic.co/t/why-is-a-wildcard-query-string-matching-on-stemmed-terms/346576/3 "2023-11-07T15:05:45Z")

</div>

Yes, I want to search across all searchable fields of all documents, but doesn't that mean searching across the inverted index, NOT the source document? This distinction is what I'm trying to resolve.

This is not the exact index, but is representative of the problem:

```auto
{
  "settings": {
    "index": {
      "number_of_shards": 2,
      "number_of_replicas": 0
    },
    "analysis": {
      "analyzer": {
        "default": {
          "type": "custom",
          "tokenizer": "whitespace",
          "filter": [
            "lowercase",
            "porter_stem"
          ]
        }
      }
    }
  },
  "mappings": {
    "dynamic": false,
    "properties": {
      "data": {
        "type": "text"
      }
    }
}

```

---

<div class="post-metadata">

### Author: ![cphramington](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/cphramington/32/127566_2.png) [@cphramington](https://discuss.elastic.co/u/cphramington)
#### Post date: [November 7, 2023, 9:49pm UTC](https://discuss.elastic.co/t/why-is-a-wildcard-query-string-matching-on-stemmed-terms/346576/4 "2023-11-07T21:49:30Z")

</div>

Just to be sure, I tested the search on a single field and the results were the same.

---

<div class="post-metadata">

### Author: ![cphramington](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/cphramington/32/127566_2.png) [@cphramington](https://discuss.elastic.co/u/cphramington)
#### Post date: [November 8, 2023, 10:54pm UTC](https://discuss.elastic.co/t/why-is-a-wildcard-query-string-matching-on-stemmed-terms/346576/5 "2023-11-08T22:54:52Z")

</div>

The explanation was right in front of me the whole time. "analyze\_wildcard" does, in fact, analyze the wildcard.

---

<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: [December 6, 2023, 10:55pm UTC](https://discuss.elastic.co/t/why-is-a-wildcard-query-string-matching-on-stemmed-terms/346576/6 "2023-12-06T22:55:51Z")

</div>

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