diff --git a/pkg/storage/unified/search/bleve.go b/pkg/storage/unified/search/bleve.go index d465d2ccdfc..2de6e3a1fe2 100644 --- a/pkg/storage/unified/search/bleve.go +++ b/pkg/storage/unified/search/bleve.go @@ -528,17 +528,19 @@ func (b *bleveBackend) findPreviousFileBasedIndex(resourceDir string, resourceVe continue } - cnt, err := idx.DocCount() - if err != nil { - b.log.Debug("error getting count from index", "indexDir", indexDir, "err", err) - _ = idx.Close() - continue - } + if !searchAfterWrite { + cnt, err := idx.DocCount() + if err != nil { + b.log.Debug("error getting count from index", "indexDir", indexDir, "err", err) + _ = idx.Close() + continue + } - if uint64(size) != cnt { - b.log.Debug("index count mismatch. ignoring index", "indexDir", indexDir, "size", size, "cnt", cnt) - _ = idx.Close() - continue + if uint64(size) != cnt { + b.log.Debug("index count mismatch. ignoring index", "indexDir", indexDir, "size", size, "cnt", cnt) + _ = idx.Close() + continue + } } indexRV, err := getRV(idx) diff --git a/pkg/storage/unified/search/bleve_test.go b/pkg/storage/unified/search/bleve_test.go index 234e67af1c5..9af5a0c1fd5 100644 --- a/pkg/storage/unified/search/bleve_test.go +++ b/pkg/storage/unified/search/bleve_test.go @@ -859,8 +859,6 @@ func TestBuildIndex(t *testing.T) { Resource: "resource", } - tmpDir := t.TempDir() - type RV string const ( RVLessThan RV = "less" @@ -872,7 +870,11 @@ func TestBuildIndex(t *testing.T) { for _, sameSize := range []bool{false, true} { for _, documentRV := range []RV{RVLessThan, RVSame, RVBiggerThan} { shouldRebuild := false - if rebuild || !sameSize || (!searchAfterWrite && documentRV == RVBiggerThan) { + if rebuild { + shouldRebuild = true + } else if !searchAfterWrite && !sameSize { + shouldRebuild = true + } else if !searchAfterWrite && documentRV == RVBiggerThan { shouldRebuild = true } @@ -911,6 +913,8 @@ func TestBuildIndex(t *testing.T) { } t.Run(testName, func(t *testing.T) { + tmpDir := t.TempDir() + var size int64 = 10 var rv int64 = 100 backend1, _ := createBleveBackendAndIndex(t, tmpDir, ns, size, rv, 10, rebuild, searchAfterWrite) @@ -931,9 +935,9 @@ func TestBuildIndex(t *testing.T) { cnt, err := idx.DocCount(context.Background(), "") require.NoError(t, err) if shouldRebuild { - require.Equal(t, int64(1000), cnt) + require.Equal(t, int64(1000), cnt, "Index has been not rebuilt") } else { - require.Equal(t, int64(10), cnt) + require.Equal(t, int64(10), cnt, "Index has not been reused") } backend2.CloseAllIndexes() })