# Multiple Terms filters not working

**URL:** <https://discuss.elastic.co/t/multiple-terms-filters-not-working/284077>\
**Category:** Elasticsearch\
**Created:** [September 13, 2021, 1:13pm UTC](https://discuss.elastic.co/t/multiple-terms-filters-not-working/284077 "2021-09-13T13:13:40Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jay2](https://avatars.discourse-cdn.com/v4/letter/j/a6a055/32.png) [@Jay2](https://discuss.elastic.co/u/Jay2)\
**Post date:** [September 13, 2021, 1:13pm UTC](https://discuss.elastic.co/t/multiple-terms-filters-not-working/284077/1 "2021-09-13T13:13:40Z")

</div>

Hi, I am new to the DSL and im using the below DSL query:  
`{"from":0,"min_score":0.5,"query":{"bool":{"filter":[{"terms":{"extension":["bmp","jpg","tif","img","png","gif","jpeg"]}},{"terms":{"userContentType":["webpage"]}},{"terms":{"language":["en","gen"]}},{"terms":{"metaLanguage":["en"]}}],"should":[{"multi_match":{"fields":["title","description^5","partNumber","guid","metaId","keywords"],"lenient":true,"query":"technology"}}]}},"size":50,"sort":[{"userContentType.keyword":{"order":"desc"}},{"_score":{"order":"desc"}}]}`

But I get no results. However if I take out one of the terms, say 'extension' or 'userContentType' it works fine.. any help would be greatly appreciated.

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [September 13, 2021, 2:21pm UTC](https://discuss.elastic.co/t/multiple-terms-filters-not-working/284077/2 "2021-09-13T14:21:09Z")

</div>

Simplify the query further to reduce the potential issue

1. Remove the `should` part. It 'only contributes to scoring'
2. (my hot take): Remove the `min_score` part and see if that changes anything

If any of those changes the documents being returned, take a look at the explain output how score is calculated. See [Search API | Elasticsearch Guide [7.14] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/7.14/search-search.html#request-body-search-explain)

---

<div class="post-metadata">

**Author:** ![Jay2](https://avatars.discourse-cdn.com/v4/letter/j/a6a055/32.png) [@Jay2](https://discuss.elastic.co/u/Jay2)\
**Post date:** [September 13, 2021, 2:31pm UTC](https://discuss.elastic.co/t/multiple-terms-filters-not-working/284077/3 "2021-09-13T14:31:04Z")

</div>

I removed both as you suggested and individually, still no results.

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [September 13, 2021, 2:36pm UTC](https://discuss.elastic.co/t/multiple-terms-filters-not-working/284077/4 "2021-09-13T14:36:33Z")

</div>

Can you share a fully reproducible **but minimal** example including index & document creation plus querying?

---

<div class="post-metadata">

**Author:** ![Jay2](https://avatars.discourse-cdn.com/v4/letter/j/a6a055/32.png) [@Jay2](https://discuss.elastic.co/u/Jay2)\
**Post date:** [September 13, 2021, 3:21pm UTC](https://discuss.elastic.co/t/multiple-terms-filters-not-working/284077/5 "2021-09-13T15:21:38Z")

</div>

Indexes are generated using a separate console application, but I think i'm confident the index data is correct, otherwise it wouldn't work at all when I remove one of the terms..unless im missing something else there...  
The query is generated using the NEST C# api v7.12.1

```auto
List<Func<QueryContainerDescriptor<ExpandoObject>, QueryContainer>> filters = searchModel.FilterValues
                        .Select(filter => (Func<QueryContainerDescriptor<ExpandoObject>, QueryContainer>)(fq => fq
                        .Terms(t => t
                            .Field(filter.FieldToSearch.Trim())
                           .Terms(filter.Values)))).ToList();

 
searchDescriptor
                            .Query(q => q
                                   .Bool(b => b
                                        .Should(s => s
                                           .MultiMatch(m => m
                                               .Fields( _elasticSearchServiceHelper.GetFieldsToSearch(searchModel.FieldsToSearch))
                                               .Query(searchModel.SearchTerm)
                                               .Lenient(true)))
                                       .Filter(filters)))
                               .Sort(SortDescriptor);

```

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [September 13, 2021, 3:32pm UTC](https://discuss.elastic.co/t/multiple-terms-filters-not-working/284077/6 "2021-09-13T15:32:57Z")

</div>

I know it's tedious, but without a reproducible example it's hard to help. The document without mapping is not helpful and I for example cannot do any .NET code execution to reproduce this, so I would need to raw JSON of document creation/indexing, index creation and query.

Also, please do not paste screenshots, but rather code snippets, as not everyone is able to read those.

---

<div class="post-metadata">

**Author:** ![Jay2](https://avatars.discourse-cdn.com/v4/letter/j/a6a055/32.png) [@Jay2](https://discuss.elastic.co/u/Jay2)\
**Post date:** [September 14, 2021, 11:23am UTC](https://discuss.elastic.co/t/multiple-terms-filters-not-working/284077/8 "2021-09-14T11:23:17Z")

</div>

I cant help much with the json indexing/creating unfortunately.  
Apologies, I wont paste images again, my bad.  
Just a hunch here, but could the query be doing a AND for each terms hence the no results?  
If so, I think I may have incorrectly assumed that the should part of the query handled this..in which case would I need to make the terms OR based?

---

<div class="post-metadata">

**Author:** ![Jay2](https://avatars.discourse-cdn.com/v4/letter/j/a6a055/32.png) [@Jay2](https://discuss.elastic.co/u/Jay2)\
**Post date:** [September 16, 2021, 10:09am UTC](https://discuss.elastic.co/t/multiple-terms-filters-not-working/284077/9 "2021-09-16T10:09:36Z")

</div>

Index mappings:

```auto
{
  "mappings": {
    "properties": {
      "Id": {
        "type": "long"
      },
      "applicationName": {
        "type": "text",
        "fields": {
          "keyword": {
            "type": "keyword",
            "ignore_above": 256
          }
        }
      },
      "content": {
        "type": "text",
        "fields": {
          "keyword": {
            "type": "keyword",
            "ignore_above": 256
          }
        }
      },
      "createdDate": {
        "type": "date"
      },
      "description": {
        "type": "text",
        "fields": {
          "keyword": {
            "type": "keyword",
            "ignore_above": 256
          }
        }
      },
      "extension": {
        "type": "text",
        "fields": {
          "keyword": {
            "type": "keyword",
            "ignore_above": 256
          }
        }
      },
      "industryCategoryIdentifiers": {
        "type": "text",
        "fields": {
          "keyword": {
            "type": "keyword",
            "ignore_above": 256
          }
        }
      },
      "keywords": {
        "type": "text",
        "fields": {
          "keyword": {
            "type": "keyword",
            "ignore_above": 256
          }
        }
      },
      "language": {
        "type": "text",
        "fields": {
          "keyword": {
            "type": "keyword",
            "ignore_above": 256
          }
        }
      },
      "metaLanguage": {
        "type": "text",
        "fields": {
          "keyword": {
            "type": "keyword",
            "ignore_above": 256
          }
        }
      },
      "pageId": {
        "type": "long"
      },
      "productLineCategoryIdentifiers": {
        "type": "text",
        "fields": {
          "keyword": {
            "type": "keyword",
            "ignore_above": 256
          }
        }
      },
      "title": {
        "type": "text",
        "fields": {
          "keyword": {
            "type": "keyword",
            "ignore_above": 256
          }
        }
      },
      "updatedDate": {
        "type": "date"
      },
      "url": {
        "type": "text",
        "fields": {
          "keyword": {
            "type": "keyword",
            "ignore_above": 256
          }
        }
      },
      "userContentType": {
        "type": "text",
        "fields": {
          "keyword": {
            "type": "keyword",
            "ignore_above": 256
          }
        }
      }
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![Jay2](https://avatars.discourse-cdn.com/v4/letter/j/a6a055/32.png) [@Jay2](https://discuss.elastic.co/u/Jay2)\
**Post date:** [September 16, 2021, 10:10am UTC](https://discuss.elastic.co/t/multiple-terms-filters-not-working/284077/10 "2021-09-16T10:10:17Z")

</div>

Tried using .keyword on the usercontenttype and extension, still no joy

---

<div class="post-metadata">

**Author:** ![Jay2](https://avatars.discourse-cdn.com/v4/letter/j/a6a055/32.png) [@Jay2](https://discuss.elastic.co/u/Jay2)\
**Post date:** [September 23, 2021, 11:48am UTC](https://discuss.elastic.co/t/multiple-terms-filters-not-working/284077/11 "2021-09-23T11:48:53Z")

</div>

Hi, just a quick update.  
I still couldn't figure this out, ultimately in the end, I had to make sure that I was only ever using up to a maximum of 3 terms per query, this meant updating my indexes to have an attributes field that contained a collection of fields that I wanted to search on, this seems to work fine now.

---

<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:** [October 21, 2021, 11:49am UTC](https://discuss.elastic.co/t/multiple-terms-filters-not-working/284077/12 "2021-10-21T11:49:06Z")

</div>

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