-
Notifications
You must be signed in to change notification settings - Fork 7
/
solr.go
248 lines (217 loc) · 8.25 KB
/
solr.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
// TODO.
package finc
import (
"encoding/json"
"fmt"
"strings"
"github.com/kennygrant/sanitize"
"github.com/miku/span/container"
)
// Solr5Vufind3 is the basic solr 5 schema as of 2016-04-14. It is based on
// VuFind 3. Same as Solr5Vufind3v12, but with fullrecord field, refs. #8031.
type Solr5Vufind3 struct {
AccessFacet string `json:"access_facet,omitempty"`
AuthorFacet []string `json:"author_facet,omitempty"`
AuthorCorporate []string `json:"author_corporate,omitempty"`
Authors []string `json:"author,omitempty"`
AuthorSort string `json:"author_sort,omitempty"`
SecondaryAuthors []string `json:"author2,omitempty"`
Allfields string `json:"allfields,omitempty"`
Edition string `json:"edition,omitempty"`
FacetAvail []string `json:"facet_avail"`
FincClassFacet []string `json:"finc_class_facet,omitempty"`
Formats []string `json:"format,omitempty"`
Fullrecord string `json:"fullrecord,omitempty"`
Fulltext string `json:"fulltext,omitempty"`
HierarchyParentTitle []string `json:"hierarchy_parent_title,omitempty"`
ID string `json:"id,omitempty"`
Institutions []string `json:"institution,omitempty"`
Imprint string `json:"imprint,omitempty"`
ISSN []string `json:"issn,omitempty"`
ISBN []string `json:"isbn,omitempty"`
Languages []string `json:"language,omitempty"`
MegaCollections []string `json:"mega_collection,omitempty"`
PublishDateSort int `json:"publishDateSort,omitempty"`
Publishers []string `json:"publisher,omitempty"`
RecordID string `json:"record_id,omitempty"`
RecordType string `json:"recordtype,omitempty"`
Series []string `json:"series,omitempty"`
SourceID string `json:"source_id,omitempty"`
Subtitle string `json:"title_sub,omitempty"`
Title string `json:"title,omitempty"`
TitleFull string `json:"title_full,omitempty"`
TitleShort string `json:"title_short,omitempty"`
TitleSort string `json:"title_sort,omitempty"`
Topics []string `json:"topic,omitempty"`
URL []string `json:"url,omitempty"`
PublishDate []string `json:"publishDate,omitempty"`
Physical []string `json:"physical,omitempty"`
Description string `json:"description"`
ContainerIssue string `json:"container_issue,omitempty"`
ContainerStartPage string `json:"container_start_page,omitempty"`
ContainerTitle string `json:"container_title,omitempty"`
ContainerVolume string `json:"container_volume,omitempty"`
FormatDe105 []string `json:"format_de105,omitempty"`
FormatDe14 []string `json:"format_de14,omitempty"`
FormatDe15 []string `json:"format_de15,omitempty"`
FormatDe520 []string `json:"format_de520,omitempty"`
FormatDe540 []string `json:"format_de540,omitempty"`
FormatDeCh1 []string `json:"format_dech1,omitempty"`
FormatDed117 []string `json:"format_ded117,omitempty"`
FormatDeGla1 []string `json:"format_degla1,omitempty"`
FormatDel152 []string `json:"format_del152,omitempty"`
FormatDel189 []string `json:"format_del189,omitempty"`
FormatDeZi4 []string `json:"format_dezi4,omitempty"`
FormatDeZwi2 []string `json:"format_dezwi2,omitempty"`
FormatNrw []string `json:"format_nrw,omitempty"`
BranchNrw string `json:"branch_nrw,omitempty"` // refs #11605
}
// Export fulfuls finc.Exporter interface, so we can plug this into cmd/span-export. Takes
// an intermediate schema and returns serialized JSON.
func (s *Solr5Vufind3) Export(is IntermediateSchema, withFullrecord bool) ([]byte, error) {
if err := s.convert(is, withFullrecord); err != nil {
return []byte{}, err
}
return json.Marshal(s)
}
// convert converts intermediate schema to the Solr5Vufind3. The struct fields are populated.
func (s *Solr5Vufind3) convert(is IntermediateSchema, withFullrecord bool) error {
s.Allfields = is.Allfields()
s.Formats = append(s.Formats, is.Format)
s.Fullrecord = "blob:" + is.ID
s.Fulltext = is.Fulltext
s.ID = is.ID
s.RecordID = is.RecordID
s.Imprint = is.Imprint()
s.ISSN = is.ISSNList()
s.ISBN = is.ISBNList()
s.Edition = is.Edition
s.MegaCollections = is.MegaCollections
s.PublishDateSort = is.Date.Year()
s.PublishDate = []string{is.Date.Format("2006-01-02")}
s.Publishers = is.Publishers
if withFullrecord {
s.RecordType = IntermediateSchemaRecordType
} else {
s.RecordType = AIRecordType
}
if is.JournalTitle != "" {
s.Series = append(s.Series, is.JournalTitle)
}
if is.Series != "" {
s.Series = append(s.Series, is.Series)
}
s.SourceID = is.SourceID
s.Subtitle = is.ArticleSubtitle
s.TitleSort = is.SortableTitle()
s.Topics = is.Subjects
// refs. #12127
s.URL = is.URL
// refs. #8709
if is.DOI != "" {
containsDOI := false
for _, u := range s.URL {
if strings.Contains(u, "doi") {
containsDOI = true
}
}
if !containsDOI {
// refs. GH #9
s.URL = append(s.URL, fmt.Sprintf("https://doi.org/%s", is.DOI))
}
}
classes := container.NewStringSet()
for _, s := range is.Subjects {
for _, class := range SubjectMapping.LookupDefault(s, []string{}) {
classes.Add(class)
}
}
s.FincClassFacet = classes.Values()
var sanitized string
switch {
case is.BookTitle != "":
sanitized = sanitize.HTML(is.BookTitle)
default:
sanitized = sanitize.HTML(is.ArticleTitle)
}
s.Title, s.TitleFull, s.TitleShort = sanitized, sanitized, sanitized
// In intermediate schema we do not have a title yet but rft.btitle is
// non-empty, use that.
if s.Title == "" && is.BookTitle != "" {
sanitized := sanitize.HTML(is.BookTitle)
s.Title, s.TitleFull, s.TitleShort = sanitized, sanitized, sanitized
}
for _, lang := range is.Languages {
s.Languages = append(s.Languages, LanguageMap.LookupDefault(lang, lang))
}
// Collect sanitized authors.
var authors []string
// Refs. https://github.com/miku/span/issues/12.
var authorCorporate []string
for _, author := range is.Authors {
sanitized := AuthorReplacer.Replace(author.String())
if sanitized == "" {
continue
}
authors = append(authors, sanitized)
s.AuthorFacet = append(s.AuthorFacet, sanitized)
// Refs. https://github.com/miku/span/issues/12.
if author.Corporate != "" {
authorCorporate = append(authorCorporate, author.Corporate)
}
}
if len(authorCorporate) > 0 {
s.AuthorCorporate = authorCorporate
}
// refs #7092, gh #8, refs #12310
if len(authors) > 0 {
s.Authors = authors
s.AuthorSort = strings.ToLower(authors[0])
}
s.AccessFacet = AIAccessFacet
s.BranchNrw = s.AccessFacet // refs #11605
// Site specific formats, TODO: fix this now.
s.FormatDe105 = []string{FormatDe105.LookupDefault(is.Format, "")}
s.FormatDe14 = []string{FormatDe14.LookupDefault(is.Format, "")}
s.FormatDe15 = []string{FormatDe15.LookupDefault(is.Format, "")}
s.FormatDe520 = []string{FormatDe520.LookupDefault(is.Format, "")}
s.FormatDe540 = []string{FormatDe540.LookupDefault(is.Format, "")}
s.FormatDeCh1 = []string{FormatDeCh1.LookupDefault(is.Format, "")}
s.FormatDed117 = []string{FormatDed117.LookupDefault(is.Format, "")}
s.FormatDeGla1 = []string{FormatDeGla1.LookupDefault(is.Format, "")}
s.FormatDel152 = []string{FormatDel152.LookupDefault(is.Format, "")}
s.FormatDel189 = []string{FormatDel189.LookupDefault(is.Format, "")}
s.FormatDeZi4 = []string{FormatDeZi4.LookupDefault(is.Format, "")}
s.FormatDeZwi2 = []string{FormatDeZwi2.LookupDefault(is.Format, "")}
s.FormatNrw = []string{FormatNrw.LookupDefault(is.Format, "")}
s.ContainerVolume = is.Volume
s.ContainerIssue = is.Issue
s.ContainerStartPage = is.StartPage
s.ContainerTitle = is.JournalTitle
s.Institutions = is.Labels
s.Description = is.Abstract
if withFullrecord {
// refs. #8031
b, err := json.Marshal(is)
if err != nil {
return err
}
s.Fullrecord = string(b)
}
// Default facet for online contents, refs #11285.
s.FacetAvail = []string{"Online"}
if is.OpenAccess {
s.FacetAvail = append(s.FacetAvail, "Free")
}
// refs #11478
s.Physical = []string{is.Pages}
// refs #14215
if is.SourceID == "48" {
s.ISSN = []string{}
s.Description = ""
s.Languages = []string{}
s.Publishers = []string{}
s.Fulltext = ""
}
return nil
}