diff --git a/__tests__/Indexer.test.js b/__tests__/Indexer.test.js index ea668eb..44c94f1 100644 --- a/__tests__/Indexer.test.js +++ b/__tests__/Indexer.test.js @@ -42,8 +42,14 @@ describe('Indexer', () => { 'mainTitle': { '@value': 'Hamlet' }, 'subtitle': { '@value': 'A Tragic Tale about a Prince of Denmark' }, 'someText': { '@value': 'There is nothing either good or bad, but thinking makes it so.' }, - 'hasResourceTemplate': 'ld4p:RT:bf2:Title:AbbrTitle' - }, { + 'hasResourceTemplate': 'ld4p:RT:bf2:Title:AbbrTitle', + 'issuance': 'http://id.loc.gov/vocabulary/issuance/mono', + }, + { + '@id': 'http://id.loc.gov/vocabulary/issuance/mono', + 'label': 'single unit' + }, + { '@id': '_:b0', '@type': ['as:Update', 'prov:Activity'], 'atTime': '2019-10-18T16:11:33.772Z', @@ -86,7 +92,8 @@ describe('Indexer', () => { text: [ 'Hamlet', 'A Tragic Tale about a Prince of Denmark', - 'There is nothing either good or bad, but thinking makes it so.' + 'There is nothing either good or bad, but thinking makes it so.', + 'single unit', ], subtitle: ['A Tragic Tale about a Prince of Denmark'], title: ['Hamlet'], @@ -348,7 +355,8 @@ describe('Indexer', () => { text: [ 'Hamlet', 'A Tragic Tale about a Prince of Denmark', - 'There is nothing either good or bad, but thinking makes it so.' + 'There is nothing either good or bad, but thinking makes it so.', + 'single unit' ], subtitle: ['A Tragic Tale about a Prince of Denmark'], title: ['Hamlet'], diff --git a/src/ResourceIndexer.js b/src/ResourceIndexer.js index 4d57066..5c31fb0 100644 --- a/src/ResourceIndexer.js +++ b/src/ResourceIndexer.js @@ -23,7 +23,7 @@ export default class { this.buildFromPath('title', '$..[mainTitle,P10223,P20315,P40085,P30156]') //BIBFRAME and RDA this.buildFromPath('subtitle', '$..subtitle') this.buildLabel() - this.buildFromPath('text', '$..*') + this.buildAllText() this.buildActivityStreamField('created', ['Create']) this.buildActivityStreamField('modified', ['Create', 'Update']) this.buildRDFTypes() @@ -31,13 +31,12 @@ export default class { if (!this.indexObject.uri || !this.indexObject.label) { throw `${this.uri} requires a uri and label: ${this.indexObject}` } - return this.indexObject } buildFromPath(fieldName, path) { const fieldValues = JSONPath({ - json: this.json, + json: this.json['@graph'], path: path, flatten: true }) @@ -47,6 +46,25 @@ export default class { } + buildAllText() { + const fieldValues = JSONPath({ + json: this.json['@graph'], + path: '$..*', + flatten: true + }) + .filter(obj => obj['@value']) // Filter out fields without values or labels + .map(obj => obj['@value']) // Extract the value or label + const labelValues = JSONPath({ + json: this.json['@graph'], + path: '$..*', + flatten: false + }) + .filter(obj => obj['label']) // Filter out fields without values or labels + .map(obj => obj['label']) // Extract the value or label + + this.indexObject['text'] = [...fieldValues, ...labelValues] + } + buildLabel() { const labelValues = [] const fieldNames = ['title', 'subtitle']