# Filtering out stop words at search time only

**URL:** <https://discuss.elastic.co/t/filtering-out-stop-words-at-search-time-only/153144>\
**Category:** Elasticsearch\
**Created:** [October 19, 2018, 8:46am UTC](https://discuss.elastic.co/t/filtering-out-stop-words-at-search-time-only/153144 "2018-10-19T08:46:37Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![newnewb](https://avatars.discourse-cdn.com/v4/letter/n/c37758/32.png) [@newnewb](https://discuss.elastic.co/u/newnewb)\
**Post date:** [October 19, 2018, 8:46am UTC](https://discuss.elastic.co/t/filtering-out-stop-words-at-search-time-only/153144/1 "2018-10-19T08:46:38Z")

</div>

**Problem:**  
I'm using a simple match query to return some results with filtered stop words. But if a stop word is searched for with a simple match query, it will have 0 hits since it's unable to match stop words which are already filtered out.

This is my analyser:

```
                "stopwords_analyzer": {
                    "filter": [
                        "lowercase",
                        "trim",
                        "stopwords_filter"
                    ],
                    "type": "custom",
                    "tokenizer": "standard"

```

It works great in all use cases except when when trying to match stop words like:

```
"query": {
    "constant_score": {
      "filter": {
        "bool": {
          "must": [
            {"match": {"word.stopwords": "the great pyramid"}}
          ]
        }
      }
    }
  }

```

It would match results for "great pyramid", but return 0 results for "the great pyramid".

**Unsuccessful solution:**  
Tried adding another analyser without stopwords filter as a index\_analyser, and using the existing stopwords\_analyser as search\_analyser.

```
                "normal_analyzer": {
                    "filter": [
                        "lowercase",
                        "trim"
                    ],
                    "type": "custom",
                    "tokenizer": "standard"

```

From my understanding, stop words should be available after indexing, but should be filtered out when searching with queries. Yet, now it returns all results, including stop words.

Why doesn't this work? Is there an another way to search for stop words, while excluding them from results?

---

<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:** [October 26, 2018, 9:20am UTC](https://discuss.elastic.co/t/filtering-out-stop-words-at-search-time-only/153144/2 "2018-10-26T09:20:38Z")

</div>

> [@newnewb](#):
>
> "constant\_score": { "filter": { "bool": { "must": [{"match": {"word.stopwords": "the great pyramid"}}] } } }

Not sure I follow the problem but a more complete reconstruction would help.  
This is working for me:

```
PUT test
{
  "settings": {
	"analysis": {
	  "analyzer": {
		"stopwords_analyzer": {
		  "filter": [
			"lowercase",
			"trim",
			"stopwords_filter"
		  ],
		  "type": "custom",
		  "tokenizer": "standard"
		}
	  },
	  "filter": {
		"stopwords_filter": {
		  "type": "stop",
		  "stopwords": [
			"the",
			"great"
		  ]
		}
	  }
	}
  },
  "mappings": {
	"_doc": {
	  "properties": {
		"word": {
		  "type": "text",
		  "analyzer": "stopwords_analyzer"
		}
	  }
	}
  }
}
POST test/_doc
{
  "word":"the great pyramid"
}
POST test/_search
{
  "query": {
	"match": {
	  "word": "the great pyramid"
	}
  }
}

```

---

<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 23, 2018, 9:20am UTC](https://discuss.elastic.co/t/filtering-out-stop-words-at-search-time-only/153144/3 "2018-11-23T09:20:41Z")

</div>

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