Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions hub3/fragments/graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand Down
4 changes: 4 additions & 0 deletions hub3/fragments/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}
}
Expand Down
12 changes: 12 additions & 0 deletions hub3/fragments/resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
})
})
})

Expand Down
24 changes: 24 additions & 0 deletions hub3/fragments/semantic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
28 changes: 18 additions & 10 deletions hub3/server/http/handlers/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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":
Expand Down Expand Up @@ -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()
Expand Down
Loading