diff --git a/services/search/pkg/bleve/testdata/mapping.golden.json b/services/search/pkg/bleve/testdata/mapping.golden.json index 73f6baa748..ba07083d29 100644 --- a/services/search/pkg/bleve/testdata/mapping.golden.json +++ b/services/search/pkg/bleve/testdata/mapping.golden.json @@ -30,6 +30,19 @@ } ] }, + "ExtractionFailed": { + "enabled": true, + "dynamic": true, + "fields": [ + { + "type": "boolean", + "store": true, + "index": true, + "include_in_all": true, + "docvalues": true + } + ] + }, "Favorites": { "enabled": true, "dynamic": true, diff --git a/services/search/pkg/content/tika.go b/services/search/pkg/content/tika.go index 035d4b9fa4..b32ec15a79 100644 --- a/services/search/pkg/content/tika.go +++ b/services/search/pkg/content/tika.go @@ -86,6 +86,17 @@ func (t Tika) Extract(ctx context.Context, ri *provider.ResourceInfo) (Document, if len(metas) == 0 { return doc, fmt.Errorf("tika returned no metadata for %q", ri.GetName()) } + // tika answers a failed parse (ocr crash, broken pdf) with 200 and empty + // content, only this key tells it apart from a file without text + cause, err := getFirstValue(metas[0], + "tk:exception:container-exception", // tika 4 + "X-TIKA:EXCEPTION:container_exception", // tika 3 legacy + ) + if err == nil { + // the value is a java stack trace, its first line names the cause + cause, _, _ = strings.Cut(cause, "\n") + return doc, fmt.Errorf("tika failed to parse %q: %s", ri.GetName(), cause) + } for _, meta := range metas { title, err := getFirstValue(meta, "dc:title") diff --git a/services/search/pkg/content/tika_test.go b/services/search/pkg/content/tika_test.go index 62c5f248b1..61940f2a84 100644 --- a/services/search/pkg/content/tika_test.go +++ b/services/search/pkg/content/tika_test.go @@ -110,6 +110,23 @@ var _ = Describe("Tika", func() { Expect(err).To(MatchError(ContainSubstring("report.pdf"))) }) + DescribeTable("fails when tika could not parse the file", + func(response string) { + fullResponse = response + + doc, err := tika.Extract(context.TODO(), &provider.ResourceInfo{ + Type: provider.ResourceType_RESOURCE_TYPE_FILE, + Name: "scan.png", + Size: 1, + }) + Expect(err).To(MatchError(And(ContainSubstring("scan.png"), ContainSubstring("bad exit value 1")))) + Expect(err.Error()).ToNot(ContainSubstring("at org.apache")) + Expect(doc.Name).To(Equal("scan.png")) + }, + Entry("tika 4", `[{"tk:content": "", "tk:exception:container-exception": "org.apache.tika.exception.TikaException: TesseractOCRParser bad exit value 1\n\tat org.apache.tika.parser.ocr.TesseractOCRParser.doOCR"}]`), + Entry("tika 3", `[{"X-TIKA:content": "", "X-TIKA:EXCEPTION:container_exception": "org.apache.tika.exception.TikaException: TesseractOCRParser bad exit value 1\n\tat org.apache.tika.parser.ocr.TesseractOCRParser.doOCR"}]`), + ) + It("adds the title", func() { fullResponse = `[{"dc:title": "quarterly report", "X-TIKA:content": "some data"}]` diff --git a/services/search/pkg/opensearch/testdata/resource.golden.json b/services/search/pkg/opensearch/testdata/resource.golden.json index ab343eef11..ff94b32f7e 100644 --- a/services/search/pkg/opensearch/testdata/resource.golden.json +++ b/services/search/pkg/opensearch/testdata/resource.golden.json @@ -9,6 +9,9 @@ "Deleted": { "type": "boolean" }, + "ExtractionFailed": { + "type": "boolean" + }, "Favorites": { "type": "keyword" }, diff --git a/services/search/pkg/search/search.go b/services/search/pkg/search/search.go index 463b6936e3..162083f177 100644 --- a/services/search/pkg/search/search.go +++ b/services/search/pkg/search/search.go @@ -85,6 +85,9 @@ type Resource struct { Type uint64 `json:"Type"` Deleted bool `json:"Deleted"` Hidden bool `json:"Hidden"` + // ExtractionFailed marks a document indexed without its content, the next + // reindex extracts it again; entries from before the field count as extracted + ExtractionFailed bool `json:"ExtractionFailed"` } // resourceFieldOverrides is built once (it never changes) and reused on hot diff --git a/services/search/pkg/search/service.go b/services/search/pkg/search/service.go index 64acfbb57c..75b1e076c7 100644 --- a/services/search/pkg/search/service.go +++ b/services/search/pkg/search/service.go @@ -730,10 +730,12 @@ func (s *Service) doUpsertItem(ctx context.Context, ref *provider.Reference, bat return } + // a failed extraction still yields the name, path and tags, the resource + // stays findable and the next space walk retries it doc, err := s.extractor.Extract(ctx, stat.Info) + extractionFailed := err != nil if err != nil { - s.logger.Error().Err(err).Msg("failed to extract resource content") - return + s.logger.Error().Err(err).Str("path", path).Msg("failed to extract resource content") } r := Resource{ @@ -748,6 +750,7 @@ func (s *Service) doUpsertItem(ctx context.Context, ref *provider.Reference, bat Document: doc, } r.Hidden = IsHidden(r.Path) + r.ExtractionFailed = extractionFailed if parentID := stat.GetInfo().GetParentId(); parentID != nil { r.ParentID = storagespace.FormatResourceID(parentID) @@ -764,6 +767,11 @@ func (s *Service) doUpsertItem(ctx context.Context, ref *provider.Reference, bat logDocCount(s.engine, s.logger) } + // without an extraction the facets are unknown, not gone + if extractionFailed { + return + } + // determine if metadata needs to be stored in storage as well metadata := map[string]string{} facetToMetadata(metadata, doc.Audio, facetKeyPrefix+"audio.") @@ -943,7 +951,7 @@ func (s *Service) isIndexed(ctx context.Context, info *provider.ResourceInfo) bo s.logger.Error().Err(err).Interface("id", info.GetId()).Msg("failed to look up the indexed resource, indexing it again") return false } - return !r.Deleted && r.Mtime != nil && !r.Mtime.Before(utils.TSToTime(info.GetMtime())) + return !r.Deleted && !r.ExtractionFailed && r.Mtime != nil && !r.Mtime.Before(utils.TSToTime(info.GetMtime())) } func (s *Service) resInfo(ctx context.Context, ref *provider.Reference) (*provider.StatResponse, string) { diff --git a/services/search/pkg/search/service_test.go b/services/search/pkg/search/service_test.go index 6b0ba1210b..9db111a2ce 100644 --- a/services/search/pkg/search/service_test.go +++ b/services/search/pkg/search/service_test.go @@ -158,12 +158,14 @@ var _ = Describe("Searchprovider", func() { info("outdated", sprovider.ResourceType_RESOURCE_TYPE_FILE, 5000), info("missing", sprovider.ResourceType_RESOURCE_TYPE_FILE, 4000), info("trashed", sprovider.ResourceType_RESOURCE_TYPE_FILE, 4000), + info("failed", sprovider.ResourceType_RESOURCE_TYPE_FILE, 4000), } indexed := map[string]*search.Resource{ "storageid$spaceid!spaceid": {Document: content.Document{Mtime: &indexedAt}}, "storageid$spaceid!current": {Document: content.Document{Mtime: &indexedAt}}, "storageid$spaceid!outdated": {Document: content.Document{Mtime: &indexedAt}}, "storageid$spaceid!trashed": {Document: content.Document{Mtime: &indexedAt}, Deleted: true}, + "storageid$spaceid!failed": {Document: content.Document{Mtime: &indexedAt}, ExtractionFailed: true}, } gatewayClient.On("GetUserByClaim", mock.Anything, mock.Anything).Return(&userv1beta1.GetUserByClaimResponse{ @@ -202,7 +204,7 @@ var _ = Describe("Searchprovider", func() { err := s.IndexSpace(&sprovider.StorageSpaceId{OpaqueId: "storageid$spaceid!spaceid"}, false) Expect(err).ShouldNot(HaveOccurred()) - Expect(upserted).To(ConsistOf("storageid$spaceid!outdated", "storageid$spaceid!missing", "storageid$spaceid!trashed")) + Expect(upserted).To(ConsistOf("storageid$spaceid!outdated", "storageid$spaceid!missing", "storageid$spaceid!trashed", "storageid$spaceid!failed")) }) }) @@ -244,6 +246,23 @@ var _ = Describe("Searchprovider", func() { Entry("/", "/", false), ) + It("indexes a resource whose extraction failed by name and marks it", func() { + gatewayClient.On("Stat", mock.Anything, mock.Anything).Return(&sprovider.StatResponse{ + Status: status.NewOK(context.Background()), + Info: ri, + }, nil) + extractor.On("Extract", mock.Anything, mock.Anything, mock.Anything).Return(content.Document{Name: "scan.png"}, errors.New("tika failed to parse")) + + var indexed search.Resource + indexClient.On("Upsert", mock.Anything, mock.Anything).Run(func(args mock.Arguments) { + indexed = args.Get(1).(search.Resource) + }).Return(nil) + + s.UpsertItem(&sprovider.Reference{ResourceId: ri.GetId()}) + Expect(indexed.Name).To(Equal("scan.png")) + Expect(indexed.ExtractionFailed).To(BeTrue()) + }) + Context("with facets stored on the resource", func() { var ( info *sprovider.ResourceInfo @@ -342,6 +361,13 @@ var _ = Describe("Searchprovider", func() { s.UpsertItem(&sprovider.Reference{ResourceId: info.GetId()}) Expect(calls).To(BeEmpty()) }) + + It("keeps the stored facets when the extraction failed", func() { + extractor.On("Extract", mock.Anything, mock.Anything, mock.Anything).Return(content.Document{}, errors.New("tika failed to parse")) + + s.UpsertItem(&sprovider.Reference{ResourceId: info.GetId()}) + Expect(calls).To(BeEmpty()) + }) }) })