diff --git a/tsdb/querier.go b/tsdb/querier.go index efd4daf26..1071c4a71 100644 --- a/tsdb/querier.go +++ b/tsdb/querier.go @@ -357,8 +357,8 @@ func inversePostingsForMatcher(ctx context.Context, ix IndexReader, m *labels.Ma } res := vals[:0] - // If the inverse match is ="", we just want all the values. - if m.Type == labels.MatchEqual && m.Value == "" { + // If the match before inversion was !="" or !~"", we just want all the values. + if m.Value == "" && (m.Type == labels.MatchRegexp || m.Type == labels.MatchEqual) { res = vals } else { count := 1 diff --git a/tsdb/querier_test.go b/tsdb/querier_test.go index bb13531d7..c7e60a0e1 100644 --- a/tsdb/querier_test.go +++ b/tsdb/querier_test.go @@ -2808,6 +2808,13 @@ func TestPostingsForMatchers(t *testing.T) { }, }, // Not regex. + { + matchers: []*labels.Matcher{labels.MustNewMatcher(labels.MatchNotRegexp, "i", "")}, + exp: []labels.Labels{ + labels.FromStrings("n", "1", "i", "a"), + labels.FromStrings("n", "1", "i", "b"), + }, + }, { matchers: []*labels.Matcher{labels.MustNewMatcher(labels.MatchNotRegexp, "n", "^1$")}, exp: []labels.Labels{