diff --git a/services/search/pkg/opensearch/engine.go b/services/search/pkg/opensearch/engine.go index f78342c167..7dc9ea7be4 100644 --- a/services/search/pkg/opensearch/engine.go +++ b/services/search/pkg/opensearch/engine.go @@ -36,18 +36,18 @@ func (e *Engine) Search(ctx context.Context, sir *searchService.SearchIndexReque return nil, fmt.Errorf("failed to create KQL compiler: %w", err) } - query, err := compiler.Compile(ast) + builder, err := compiler.Compile(ast) if err != nil { return nil, fmt.Errorf("failed to compile query: %w", err) } - body, err := NewRootQuery(query).MarshalJSON() + body, err := NewRootQuery(builderToBoolQuery(builder).Filter( + NewTermQuery[bool]("Deleted").Value(false), + )).MarshalJSON() if err != nil { return nil, fmt.Errorf("failed to marshal query: %w", err) } - // todo: ignore deleted resources - resp, err := e.client.Search(context.Background(), &opensearchgoAPI.SearchReq{ Indices: []string{e.index}, Body: bytes.NewReader(body), diff --git a/services/search/pkg/opensearch/engine_test.go b/services/search/pkg/opensearch/engine_test.go index b4ddfbdc10..0e8a300287 100644 --- a/services/search/pkg/opensearch/engine_test.go +++ b/services/search/pkg/opensearch/engine_test.go @@ -29,12 +29,29 @@ func TestEngine_Search(t *testing.T) { t.Run("most simple search", func(t *testing.T) { resp, err := engine.Search(t.Context(), &searchService.SearchIndexRequest{ - Query: fmt.Sprintf(`"%s" Content:"%s"`, document.Name, document.Content), + Query: fmt.Sprintf(`"%s"`, document.Name), }) assert.NoError(t, err) require.Len(t, resp.Matches, 1) assert.Equal(t, int32(1), resp.TotalMatches) - assert.Equal(t, document.Name, resp.Matches[0].Entity.Name) + assert.Equal(t, document.ID, fmt.Sprintf("%s$%s!%s", resp.Matches[0].Entity.Id.StorageId, resp.Matches[0].Entity.Id.SpaceId, resp.Matches[0].Entity.Id.OpaqueId)) + }) + + t.Run("ignores files that are marked as deleted", func(t *testing.T) { + deletedDocument := opensearchtest.Testdata.Resources.Full + deletedDocument.ID = "1$2!4" + deletedDocument.Deleted = true + + tc.Require.DocumentCreate(index, deletedDocument.ID, opensearchtest.ToJSON(t, deletedDocument)) + tc.Require.IndicesCount([]string{index}, "", 2) + + resp, err := engine.Search(t.Context(), &searchService.SearchIndexRequest{ + Query: fmt.Sprintf(`"%s"`, document.Name), + }) + assert.NoError(t, err) + require.Len(t, resp.Matches, 1) + assert.Equal(t, int32(1), resp.TotalMatches) + assert.Equal(t, document.ID, fmt.Sprintf("%s$%s!%s", resp.Matches[0].Entity.Id.StorageId, resp.Matches[0].Entity.Id.SpaceId, resp.Matches[0].Entity.Id.OpaqueId)) }) }