diff --git a/hub3/fragments/graph.go b/hub3/fragments/graph.go index cc1a9348..b8501905 100644 --- a/hub3/fragments/graph.go +++ b/hub3/fragments/graph.go @@ -89,6 +89,17 @@ func (fg *FragmentGraph) AddMoreLikeThis(item *ItemV1) { fg.MoreLikeThis.Items = append(fg.MoreLikeThis.Items, item) } +// SemanticRelatedItem is fg as a related item in the semantic item format: +// the same {meta, semantic, summary} as the main item, so related items carry +// latLong too. +func (fg *FragmentGraph) SemanticRelatedItem(langs ...string) *FragmentGraph { + return &FragmentGraph{ + Meta: fg.Meta, + Semantic: fg.GenerateSemantic(), + Summary: fg.NewResultSummary(langs...), + } +} + func (fg *FragmentGraph) AddSemanticMoreLikeThis(related *FragmentGraph) { if fg.MoreLikeThis == nil { fg.MoreLikeThis = &RelatedItems{} diff --git a/hub3/fragments/resource.go b/hub3/fragments/resource.go index 4d621b1f..05c238fc 100644 --- a/hub3/fragments/resource.go +++ b/hub3/fragments/resource.go @@ -2151,6 +2151,10 @@ func (sum *ResultSummary) AddEntry(entry *ResourceEntry, langs ...string) { if sum.Date == "" { sum.Date = entry.Value } + case "latLong": + if sum.LatLong == "" { + sum.LatLong = entry.LatLong + } } } } diff --git a/hub3/fragments/resource_test.go b/hub3/fragments/resource_test.go index 6bc83521..bc8cdd78 100644 --- a/hub3/fragments/resource_test.go +++ b/hub3/fragments/resource_test.go @@ -305,6 +305,18 @@ var _ = Describe("Resource", func() { Expect(sum.Title).To(Equal("test1")) Expect(sum.Thumbnail).To(BeEmpty()) }) + + It("should set latLong from a latLong tagged entry", func() { + entry := &ResourceEntry{ + Value: "52.54081312693048,5.058354400324099", + LatLong: "52.54081312693048,5.058354400324099", + Tags: []string{"latLong"}, + } + sum := &ResultSummary{} + Expect(sum.LatLong).To(BeEmpty()) + sum.AddEntry(entry) + Expect(sum.LatLong).To(Equal("52.54081312693048,5.058354400324099")) + }) }) }) diff --git a/hub3/fragments/semantic_test.go b/hub3/fragments/semantic_test.go index 7956b03e..93ab83ae 100644 --- a/hub3/fragments/semantic_test.go +++ b/hub3/fragments/semantic_test.go @@ -978,3 +978,27 @@ func TestCycleDetection(t *testing.T) { assert.False(t, hasRefType, "fully expanded resource should not have hub3:referenceType") }) } + +func TestSemanticRelatedItem(t *testing.T) { + // The coordinates sit on a blank node no triple points at, as + // nave:DelvingResource does in Narthex-mapped records. + fg := &FragmentGraph{ + Meta: &Header{EntryURI: "http://example.org/record/1"}, + Resources: []*FragmentResource{ + {ID: "http://example.org/record/1", Entries: []*ResourceEntry{ + {Predicate: "http://purl.org/dc/elements/1.1/title", Value: "Kaart", Tags: []string{"title"}}, + }}, + {ID: "_:b0", Entries: []*ResourceEntry{ + {Value: "52.54,5.05", LatLong: "52.54,5.05", Tags: []string{"latLong"}}, + }}, + }, + } + + rel := fg.SemanticRelatedItem("nl") + + require.NotNil(t, rel.Summary) + assert.Equal(t, "52.54,5.05", rel.Summary.LatLong) + assert.Same(t, fg.Meta, rel.Meta) + assert.NotNil(t, rel.Semantic) + assert.Nil(t, rel.Resources) +} diff --git a/hub3/server/http/handlers/search.go b/hub3/server/http/handlers/search.go index 02349ee6..1f6d1374 100644 --- a/hub3/server/http/handlers/search.go +++ b/hub3/server/http/handlers/search.go @@ -814,11 +814,17 @@ func ProcessSearchRequest(w http.ResponseWriter, r *http.Request, searchRequest case fragments.ItemFormatType_SEMANTIC: for _, rec := range records { rec.Semantic = rec.GenerateSemantic() - // Clear other formats to reduce payload + // Summary is not stored in the index, so it has to be generated here — + // and before Resources is cleared, since it is built by walking them. + // It carries latLong, which the semantic tree cannot: nave:DelvingResource + // is a blank node no triple points at, so a walk from the entry URI never + // reaches it. + rec.NewResultSummary(searchRequest.Language...) + // Clear the other formats to reduce payload. Summary is the exception: + // it is small, and carries the geo values the tree cannot reach. rec.Resources = nil rec.Fields = nil rec.JSONLD = nil - rec.Summary = nil rec.ProtoBuf = nil rec.Item = nil } @@ -1486,11 +1492,17 @@ func getSearchRecord(w http.ResponseWriter, r *http.Request) { record.Resources = nil case "semantic": record.Semantic = record.GenerateSemantic() - // Clear other formats to reduce payload + // Summary is not stored in the index, so it has to be generated here — + // and before Resources is cleared, since it is built by walking them. + // It carries latLong, which the semantic tree cannot: nave:DelvingResource + // is a blank node no triple points at, so a walk from the entry URI never + // reaches it. + record.NewResultSummary(lang) + // Clear the other formats to reduce payload. Summary is the exception: + // it is small, and carries the geo values the tree cannot reach. record.Resources = nil record.Fields = nil record.JSONLD = nil - record.Summary = nil record.ProtoBuf = nil record.Item = nil case "summary": @@ -1533,13 +1545,9 @@ func getSearchRecord(w http.ResponseWriter, r *http.Request) { // Use semantic format for MLT when itemFormat is semantic // This ensures consistent field names between parent and related items - // Related items have same structure as main item: {meta, semantic} + // Related items have same structure as main item: {meta, semantic, summary} if itemFormat == "semantic" { - relatedItem := &fragments.FragmentGraph{ - Meta: fg.Meta, - Semantic: fg.GenerateSemantic(), - } - record.AddSemanticMoreLikeThis(relatedItem) + record.AddSemanticMoreLikeThis(fg.SemanticRelatedItem(lang)) } else { // Use ItemV1 format for other itemFormats fg.NewV1Fields()