Atlas search filters does not match with AND?

Hi, So I have a small document where I’m using some facets and filters. They are indexed in atlas search using “string” and “StringFacet” type. The analysers I’ve used for the “string” index is “Keyword analyzer” because I want to match on whole world/phrase and not tokens. The facets are one for subject area and one for degreelevel.

My search stage is defined as follows:

{$search: { index: “default”, compound: { must|mustNot|should|filter } }

The issue:
I generate the compound clause as follows (output log from my golang app)

1:47 Getting compound clause for search stage
2024/04/04 17:31:47 ------------------------------------------------------------------------------------
2024/04/04 17:31:47 Must: [{text [{path title} {query computer}]}]
2024/04/04 17:31:47 Must Not: [{range [{path course_fee.units} {lte 0}]}]
2024/04/04 17:31:47 Should: [{range [{path duration_in_weeks} {gte 0} {lt 52}]}]
2024/04/04 17:31:47 Filter: [{in [{path subject_area} {value [Computer Science and IT]}]} {in [{path degree_level} {value [Bachelor]}]}]

But when I get the results, I also get documents with degree_level Masters.

If I remove the subject_area filter, then I start seeing the right documents again (ie., documents with degree_level Bachelor)

What am I doing wrong?

For exact-value filtering, use the token field type for those fields. I’m not sure if that’s the issue you’re experiencing or not. If it doesn’t help, please provide the actual JSON index configuration and JSON aggregation pipeline.

Thanks for the prompt reply Erik!

I realised this was a oversight on my part. I used bson.D in all the must|mustNot|should|filter fields. Converting them to bson.A solved the issue.

I’ll look more into the “token” data type. Right now, choosing the analyzer of the field to be “lucene.keyword” seems to work for me as I need exact matches on these fields.

1 Like