Multiple Terms filters not working

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.

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

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

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

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

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);

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.

1 Like

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?

Index mappings:

{
  "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
          }
        }
      }
    }
  }
}

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

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.

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