AtlasSearch filter vs must

While developing a pipeline in Golang for MongoDB, I am in the process of migrating to AtlasSearch. I have some questions about filter, must, and should.

As far as I know, must represents an AND condition, should represents an OR condition, and filter acts as an AND condition. However, I’m not entirely sure about the differences between must and filter.

The query I am currently using is as follows:

searchStage := bson.D{
		{"$search", bson.M{
			"index": "sampleAddress",
			"compound": bson.D{
				{"filter", bson.A{
					bson.D{{"text", bson.D{{"query", "1994"}, {"path", "address"}}}},
					bson.D{{"text", bson.D{{"query", "test"}, {"path", "image"}}}},
				}},
			},
		}},
	}

Could you explain the differences between must and filter using examples? The query results appear to be the same, so I’m not sure how they differ.

Hi @_WM2,

As per the filter documentation:

filter behaves the same as must , except that the filter clause is not considered in a returned document’s score, and therefore does not affect the order of the returned documents.

Hope this helps.

Regards,
Jason

1 Like