# Filter stop words in simple\_query\_string

**URL:** <https://discuss.elastic.co/t/filter-stop-words-in-simple-query-string/363283>\
**Category:** Elasticsearch\
**Created:** [July 17, 2024, 12:43pm UTC](https://discuss.elastic.co/t/filter-stop-words-in-simple-query-string/363283 "2024-07-17T12:43:49Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jahedi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jahedi/32/135254_2.png) [@jahedi](https://discuss.elastic.co/u/jahedi)\
**Post date:** [July 17, 2024, 12:43pm UTC](https://discuss.elastic.co/t/filter-stop-words-in-simple-query-string/363283/1 "2024-07-17T12:43:49Z")

</div>

Hi,  
I have an index named `news_headline` with such mapping:

```auto
{
  "mappings": {
    "properties": {
      "authors": {
        "type": "text"
      },
      "category": {
        "type": "keyword"
      },
      "date": {
        "type": "date",
        "format": "iso8601"
      },
      "headline": {
        "type": "text"
      },
      "link": {
        "type": "keyword"
      },
      "short_description": {
        "type": "text"
      }
    }
  }
}

```

for example, consider the query below:

```auto
GET news_headlines/_search
{
  "query": {
    "simple_query_string" : {
        "query": "\"shape of you\" a song of Ed ",
        "fields": ["headline^2", "short_description"],
    }
  }
} 

```

How can I apply stop word on the query? I want to filter such words like `a` and `of` (the one that is out of double quotes).

The desired query response should be same as such query:

```auto
GET news_headlines/_search
{
  "query": {
    "simple_query_string" : {
        "query": "\"shape of you\" song Ed",
        "fields": ["headline^2", "short_description"],
    }
  }
} 

```

note that the `of` in the `shape of you` is still there!

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![RabBit\_BR](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rabbit_br/32/82261_2.png) [@RabBit\_BR](https://discuss.elastic.co/u/RabBit_BR)\
**Post date:** [July 17, 2024, 2:12pm UTC](https://discuss.elastic.co/t/filter-stop-words-in-simple-query-string/363283/2 "2024-07-17T14:12:59Z")

</div>

Hi @jahedi

Do you want to apply a stop words filter on the query? Why wouldn't using an analyzer with the stopwords filter work?

---

<div class="post-metadata">

**Author:** ![jahedi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jahedi/32/135254_2.png) [@jahedi](https://discuss.elastic.co/u/jahedi)\
**Post date:** [July 18, 2024, 5:13am UTC](https://discuss.elastic.co/t/filter-stop-words-in-simple-query-string/363283/3 "2024-07-18T05:13:00Z")

</div>

How can i use analyzer in this case? Can you help me?  
I tried to add a analyzer named `custom_english` to my index like this:

```auto
"analysis": {
          "analyzer": {
            "custom_english": {
              "type": "standard",
              "stopwords": "_english_"
            }
          }
        },

```

So now the complete settings of this index is like this:

```auto
{
  "news_headlines": {
    "settings": {
      "index": {
        "routing": {
          "allocation": {
            "include": {
              "_tier_preference": "data_content"
            }
          }
        },
        "number_of_shards": "1",
        "provided_name": "news_headlines",
        "creation_date": "1720937104190",
        "analysis": {
          "analyzer": {
            "custom_english": {
              "type": "standard",
              "stopwords": "_english_"
            }
          }
        },
        "number_of_replicas": "1",
        "uuid": "_V_MthiiR8-s_Ksor1WqXQ",
        "version": {
          "created": "8503000"
        }
      }
    }
  }
}

```

After adding the analyzer I tried to query like this:

```auto
GET news_headlines/_search
{
  "query": {
    "simple_query_string" : {
        "query": "\"shape of you\" a song of Ed ",
        "fields": ["headline^2", "short_description"],
        "analyzer": "custom_english"
    }
  }
} 

```

but didin't work. and the `of` inside the `shape of you` was eliminated and that is not i want.

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [July 18, 2024, 7:16am UTC](https://discuss.elastic.co/t/filter-stop-words-in-simple-query-string/363283/4 "2024-07-18T07:16:51Z")

</div>

The analyzer needs to be applied at index time. Which means it needs to be set when you create the index within the mapping section on each field where you want to use it.

---

<div class="post-metadata">

**Author:** ![RabBit\_BR](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rabbit_br/32/82261_2.png) [@RabBit\_BR](https://discuss.elastic.co/u/RabBit_BR)\
**Post date:** [July 18, 2024, 10:35am UTC](https://discuss.elastic.co/t/filter-stop-words-in-simple-query-string/363283/5 "2024-07-18T10:35:26Z")

</div>

It would be something like this described below. You define the analyzer using stop words and all data will be indexed without stop words in English.  
When you run the query, your term will eliminate stop words.  
For example, if you search just for "of" you will not have any results because this filter was applied in the indexing.

```auto
PUT idx_test
{
  "settings": {
    "analysis": {
      "analyzer": {
        "custom_english": {
          "type": "standard",
          "stopwords": "_english_"
        }
      }
    }
  },
  "mappings": {
    "properties": {
      "headline": {
        "type": "text",
        "analyzer": "custom_english"
      }
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![jahedi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jahedi/32/135254_2.png) [@jahedi](https://discuss.elastic.co/u/jahedi)\
**Post date:** [July 20, 2024, 4:53am UTC](https://discuss.elastic.co/t/filter-stop-words-in-simple-query-string/363283/6 "2024-07-20T04:53:11Z")

</div>

Nice. Thanks a lot. It worked!
