@natlibfi/melinda-record-matching 2.0.0 → 2.1.0-alpha.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -27,7 +27,7 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de
27
27
  *
28
28
  * Melinda record matching modules for Javascript
29
29
  *
30
- * Copyright (C) 2020 University Of Helsinki (The National Library Of Finland)
30
+ * Copyright (C) 2020-2022 University Of Helsinki (The National Library Of Finland)
31
31
  *
32
32
  * This file is part of melinda-record-matching-js
33
33
  *
@@ -52,74 +52,423 @@ var _default = ({
52
52
  detection: detectionOptions,
53
53
  search: searchOptions,
54
54
  maxMatches = 1,
55
- maxCandidates = 25
55
+ maxCandidates = 25,
56
+ returnStrategy = false,
57
+ returnQuery = false,
58
+ returnNonMatches = false
56
59
  }) => {
57
60
  const debug = (0, _debug.default)('@natlibfi/melinda-record-matching:index');
58
- const detect = (0, matchDetection.default)(detectionOptions);
61
+ const debugData = debug.extend('data');
62
+ debugData(`DetectionOptions: ${JSON.stringify(detectionOptions)}`);
63
+ debugData(`SearchOptions: ${JSON.stringify(searchOptions)}`);
64
+ debugData(`MaxMatches: ${JSON.stringify(maxMatches)}`);
65
+ debugData(`MaxCandidates: ${JSON.stringify(maxCandidates)}`);
66
+ debugData(`ReturnStrategy: ${JSON.stringify(returnStrategy)}`);
67
+ debugData(`ReturnQuery: ${JSON.stringify(returnQuery)}`);
68
+ debugData(`ReturnNonMatches: ${JSON.stringify(returnNonMatches)}`);
69
+ const detect = (0, matchDetection.default)(detectionOptions, returnStrategy);
59
70
  return record => {
60
71
  const search = (0, candidateSearch.default)({ ...searchOptions,
61
- record
72
+ record,
73
+ maxCandidates
62
74
  });
63
- return iterate(); // eslint-disable-next-line max-statements
75
+ return iterate({}); // candidateCount : amount of candidate records retrived from SRU for matching, NOT including current record set
76
+ // matches : candidates that have been detected as matches by current matcher job
77
+ // nonMatches : candidates that have been detected as non-matches by current matcher job (only if returnNonMatches is 'true')
78
+ // duplicateCount : amount of candidate records that were retrieved from the SRU but not handled further because they were already found in the matches/nonMatches
79
+ // state.totalRecords : amount of candidate records available to the current query (undefined, if there was no queries left)
80
+ // state.query : current query (undefined if there was no queries left)
81
+ // state.searchCounter : sequence for current search for current query (undefined, if there we no queries left)
82
+ // state.queryCandidateCounter: amount of candidate records retrieved from SRU for matching for current query, including the current record set (undefined if there were no queries left)
83
+ // state.queriesLeft : amount of queries left
84
+ // state.queryCounter : sequence for current query
85
+ // state.maxedQueries : queries that resulted in more than serverMaxResults hits
64
86
 
65
- async function iterate(initialState = {}, matches = [], candidateCount = 0) {
87
+ async function iterate({
88
+ initialState = {},
89
+ matches = [],
90
+ candidateCount = 0,
91
+ nonMatches = [],
92
+ duplicateCount = 0,
93
+ nonMatchCount = 0
94
+ }) {
95
+ debugData(`Starting next matcher iteration.`);
66
96
  const {
67
97
  records,
68
98
  ...state
69
99
  } = await search(initialState);
100
+ debugData(`Current state: ${JSON.stringify(state)}, matches: ${matches.length}, candidateCount: ${candidateCount}, nonMatches: ${nonMatches.length}`);
101
+ const recordSetSize = records.length;
102
+ const newCandidateCount = candidateCount + recordSetSize;
70
103
 
71
- if (records.length > 0 || state.queriesLeft > 0) {
72
- debug(`Checking ${records.length} candidates for matches`);
73
- const matchResult = iterateRecords(records);
104
+ if (recordSetSize > 0) {
105
+ return handleRecordSet();
106
+ }
107
+
108
+ if (state.queriesLeft > 0) {
109
+ debug(`Empty record set ${state.searchCounter} for ${state.query}, but there are ${state.queriesLeft} queries left`);
110
+ return iterate({
111
+ initialState: state,
112
+ matches,
113
+ candidateCount: newCandidateCount,
114
+ nonMatches,
115
+ duplicateCount
116
+ });
117
+ }
118
+
119
+ debug(`No (more) candidate records to check, no more queries left, matches: ${matches.length}`);
120
+ return returnResult({
121
+ matches,
122
+ state,
123
+ stopReason: '',
124
+ nonMatches,
125
+ candidateCount: newCandidateCount,
126
+ duplicateCount
127
+ });
128
+
129
+ function handleRecordSet() {
130
+ debug(`Checking record set of ${recordSetSize} candidate records for possible matches, found by ${state.searchCounter} search for ${state.query}`);
131
+ const matchResult = iterateRecords({
132
+ records,
133
+ recordSetSize,
134
+ maxMatches,
135
+ matches,
136
+ nonMatches
137
+ });
138
+ const newDuplicateCount = duplicateCount + matchResult.duplicateCount;
139
+ const newNonMatchCount = nonMatchCount + matchResult.nonMatchCount;
140
+ const {
141
+ newMatches,
142
+ newNonMatches
143
+ } = handleMatchResult(matchResult, matches, nonMatches);
144
+
145
+ if (maxMatchesFound({
146
+ matches: newMatches,
147
+ maxMatches
148
+ })) {
149
+ return returnResult({
150
+ matches: newMatches,
151
+ state,
152
+ stopReason: 'maxMatches',
153
+ nonMatches: newNonMatches,
154
+ duplicateCount: newDuplicateCount,
155
+ candidateCount: newCandidateCount,
156
+ nonMatchCount: newNonMatchCount
157
+ });
158
+ }
74
159
 
75
- if (matchResult) {
76
- const newMatches = matches.concat(matchResult);
160
+ if (maxCandidatesRetrieved(newCandidateCount, maxCandidates)) {
161
+ return returnResult({
162
+ matches: newMatches,
163
+ state,
164
+ stopReason: 'maxCandidates',
165
+ nonMatches: newNonMatches,
166
+ duplicateCount: newDuplicateCount,
167
+ candidateCount: newCandidateCount,
168
+ nonMatchCount: newNonMatchCount
169
+ });
170
+ }
171
+
172
+ return iterate({
173
+ initialState: state,
174
+ matches: newMatches,
175
+ candidateCount: newCandidateCount,
176
+ nonMatches: newNonMatches,
177
+ duplicateCount: newDuplicateCount,
178
+ nonMatchCount: newNonMatchCount
179
+ });
180
+ }
181
+
182
+ function handleMatchResult(matchResult, matches, nonMatches) {
183
+ debugData(`- Amount of new matches from record set: ${matchResult.matches.length}`); // eslint-disable-next-line functional/no-conditional-statement
184
+
185
+ if (returnNonMatches) {
186
+ debugData(`- Amount of new nonMatches from record set: ${matchResult.nonMatches.length}`);
187
+ }
77
188
 
78
- if (newMatches.length === maxMatches) {
79
- return newMatches;
80
- }
189
+ const newMatches = matches.concat(returnQuery ? addQuery(matchResult.matches) : matchResult.matches);
190
+ const newNonMatches = returnNonMatches ? nonMatches.concat(returnQuery ? addQuery(matchResult.nonMatches) : matchResult.nonMatches) : [];
191
+ debugData(`- Total amount of matches: ${newMatches.length}`); // eslint-disable-next-line functional/no-conditional-statement
81
192
 
82
- return maxCandidatesRetrieved() ? newMatches : iterate(state, newMatches, candidateCount + records.length);
193
+ if (returnNonMatches) {
194
+ debugData(`- Total amount of nonMatches: ${newNonMatches.length}`);
83
195
  }
84
196
 
85
- return maxCandidatesRetrieved() ? matches : iterate(state, matches, candidateCount + records.length);
197
+ return {
198
+ newMatches,
199
+ newNonMatches
200
+ };
86
201
  }
87
202
 
88
- debug(`No (more) candidate records to check, matches: ${matches.length}`);
89
- return matches;
203
+ function addQuery(matches) {
204
+ debugData(`Adding query ${state.query} to matches`);
205
+ return matches.map(match => ({ ...match,
206
+ matchQuery: state.query
207
+ }));
208
+ }
209
+
210
+ function maxCandidatesRetrieved(candidateCount, maxCandidates) {
211
+ debugData(`Total amount of candidate records retrieved: ${newCandidateCount} (max: ${maxCandidates})`);
90
212
 
91
- function maxCandidatesRetrieved() {
92
- if (candidateCount + records.length > maxCandidates) {
93
- debug(`Stopped searching because maximum number of candidates have been retrieved`);
213
+ if (maxCandidates && candidateCount >= maxCandidates) {
214
+ debug(`Stopped matching because maximum number of candidate records ${candidateCount} / ${maxCandidates} have been retrieved`);
94
215
  return true;
95
216
  }
96
217
  }
218
+ } // matches : array of matching candidate records
219
+ // nonMatches : array of nonMatching candidate records (if returnNonMatches option is true, otherwise empty array)
220
+ // - candidate.id
221
+ // - candidate.record
222
+ // - probability
223
+ // - strategy (if returnStrategy option is true)
224
+ // - treshold (if returnStrategy option is true)
225
+ // - matchQuery (if returnQuery option is true)
226
+ // we could have here also returnRecords/returnMatchRecords/returnNonMatchRecord options that could be turned false for not to return actual record data
227
+ // matchStatus.status: boolean, true if matcher retrieved and handled all found candidate records, false if it did not
228
+ // matchStatus.stopReason: string ('maxMatches','maxCandidates','maxedQueries',empty string/undefined), reason for stopping retrieving or handling the candidate records
229
+ // - only one stopReason is returned (if there would be several possible stopReasons, stopReason is picked in the above order)
230
+ // - currently stopReason can be non-empty also in cases where status is true, if matcher hit the stop reason when handling the last available candidate record
231
+
232
+
233
+ function returnResult({
234
+ matches,
235
+ state,
236
+ stopReason,
237
+ nonMatches,
238
+ duplicateCount,
239
+ candidateCount,
240
+ nonMatchCount
241
+ }) {
242
+ checkCounts({
243
+ matches,
244
+ nonMatches,
245
+ candidateCount,
246
+ duplicateCount,
247
+ nonMatchCount
248
+ });
249
+ const matchStatus = getMatchState(state, stopReason); // add nonMatches to result only if returnNonMatches is 'true', otherwise nonMatches have not been gathered
250
+
251
+ const result = returnNonMatches ? {
252
+ matches,
253
+ matchStatus,
254
+ nonMatches
255
+ } : {
256
+ matches,
257
+ matchStatus
258
+ };
259
+ debugData(`${JSON.stringify(result)}`);
260
+ return result; // note that in cases where the matching has been stopped because of maxMatches checkCounts won't (in most cases) match
261
+
262
+ function checkCounts({
263
+ matches,
264
+ nonMatches,
265
+ candidateCount,
266
+ duplicateCount,
267
+ nonMatchCount
268
+ }) {
269
+ const matchCount = matches.length;
270
+ debugData(`Return nonMatches: ${returnNonMatches}`);
271
+ const chosenNonMatchCount = returnNonMatches ? nonMatches.length : nonMatchCount;
272
+ const totalHandled = matchCount + nonMatchCount + duplicateCount;
273
+ debug(`candidateCount: ${candidateCount}, matches: ${matchCount}, nonMatches: ${chosenNonMatchCount}, duplicateCount: ${duplicateCount}`);
274
+ debug(`We got result for ${totalHandled} / ${candidateCount} retrieved candidates`);
275
+
276
+ if (totalHandled !== candidateCount) {
277
+ debug(`WARNING: Missing results for ${candidateCount - totalHandled} candidates`);
278
+ return;
279
+ }
280
+
281
+ return;
282
+ }
283
+
284
+ function getMatchState(state, stopReason) {
285
+ debugData(`${JSON.stringify(state)}`);
286
+ debug(`Queries left ${state.queriesLeft}, Searches for current query left: ${state.resultSetOffset && state.resultSetOffset <= state.totalRecords}, non-retrieved records: ${state.totalRecords - state.queryCandidateCounter}, maxedQueries (${state.maxedQueries.length}): ${state.maxedQueries}`);
287
+ debugData(`StopReason: <${stopReason}>`);
288
+ const searchesLeft = state.resultSetOffset && state.resultSetOffset <= state.totalRecords;
289
+ const nonRetrieved = searchesLeft ? state.totalRecords - state.queryCandidateCounter : 0;
290
+ debugData(`nonRetrieved: ${nonRetrieved}`);
291
+
292
+ if (state.queriesLeft > 0 || nonRetrieved > 0 || state.maxedQueries.length > 0) {
293
+ const maxedQueriesStopReason = state.maxedQueries.length > 0 ? 'maxedQueries' : '';
294
+ const newStopReason = stopReason === '' ? maxedQueriesStopReason : stopReason;
295
+ debugData(`MaxedQueriesStopReason: <${maxedQueriesStopReason}>`);
296
+ debugData(`NewStopReason: <${newStopReason}>`);
297
+ debug(`Match status: false`);
298
+ return {
299
+ status: false,
300
+ stopReason: newStopReason
301
+ };
302
+ }
303
+
304
+ debug(`Match status: true`);
305
+ return {
306
+ status: true,
307
+ stopReason
308
+ };
309
+ }
310
+ }
97
311
 
98
- function iterateRecords(records) {
99
- const [candidate] = records;
312
+ function iterateRecords({
313
+ records,
314
+ recordSetSize,
315
+ maxMatches,
316
+ matches = [],
317
+ nonMatches = [],
318
+ recordMatches = [],
319
+ recordNonMatches = [],
320
+ recordCount = 0,
321
+ recordDuplicateCount = 0,
322
+ recordNonMatchCount = 0
323
+ }) {
324
+ // recordSetSize : total amount of records in the current record set
325
+ // recordCount : amount of records from the current record set that have been handled
326
+ // maxMatches : setting for maximum amount found by current matcher job before the matcher job is stopped
327
+ // recordDuplicateCount : amount of records from the current record set that are already included in matches/nonMatches results
328
+ // recordNonMatchCount: amount of records from the current record set that are nonMatches (only is returnNonMatches setting is false)
329
+ // records : non-handled records in the current record set
330
+ // matches : found matches in the current matcher job
331
+ // recordMatches : found matches in the current record set
332
+ // recordNonMatches : found nonMatches in the current record set (only if returnNonMatches setting is true)
333
+ const [candidate] = records;
334
+ const newRecordCount = candidate ? recordCount + 1 : recordCount; // The matcher uses same matchDetection strategy for candidates from all candidate-searches -> matchDetection result for the same candidate is always same
335
+ // Exceptions would happen if the candidate would have been updated in the database between candidate searches
336
+ // Note that if returnNonMatches is false, matcher won't remember candidates that didn't match, so they will be matched again everytime they are retrieved by
337
+ // different candidate search queries. Same candidate search query won't have duplicate records.
100
338
 
101
- if (candidate) {
339
+ if (candidate) {
340
+ if (candidateNotInMatches(matches.concat(nonMatches), candidate)) {
102
341
  const {
103
342
  record: candidateRecord,
104
343
  id: candidateId
105
344
  } = candidate;
106
- const {
107
- match,
108
- probability
109
- } = detect(record, candidateRecord);
110
-
111
- if (match) {
112
- return {
113
- probability,
114
- candidate: {
115
- id: candidateId,
116
- record: candidateRecord
117
- }
118
- };
119
- }
120
-
121
- return iterateRecords(records.slice(1));
345
+ debug(`Running matchDetection for record ${candidateId} (${newRecordCount}/${recordSetSize})`);
346
+ const detectionResult = detect(record, candidateRecord);
347
+ return handleDetectionResult(detectionResult, candidateId, candidateRecord);
348
+ }
349
+
350
+ return iterateRecords({
351
+ records: records.slice(1),
352
+ recordSetSize,
353
+ maxMatches,
354
+ matches,
355
+ recordMatches,
356
+ recordCount: newRecordCount,
357
+ recordNonMatches,
358
+ recordDuplicateCount: recordDuplicateCount + 1,
359
+ recordNonMatchCount
360
+ });
361
+ }
362
+
363
+ debug(`No more candidates, record set (${recordCount}/${recordSetSize}) done, ${recordMatches.length} matches found, ${recordDuplicateCount} candidates already handled, ${returnNonMatches ? `${recordNonMatches.length}` : `${recordNonMatchCount}`} nonMatches found.`);
364
+ return {
365
+ matches: recordMatches,
366
+ nonMatches: returnNonMatches ? recordNonMatches : [],
367
+ duplicateCount: recordDuplicateCount,
368
+ nonMatchCount: recordNonMatchCount
369
+ };
370
+
371
+ function handleDetectionResult(detectionResult, candidateId, candidateRecord) {
372
+ debugData(`MatchDetection results for ${candidateId} (${newRecordCount}/${recordSetSize}): ${JSON.stringify(detectionResult)}`);
373
+
374
+ if (detectionResult.match || returnNonMatches) {
375
+ debug(`${detectionResult.match ? `Record ${candidateId} (${newRecordCount}/${recordSetSize}) is a match!` : `Record ${candidateId} (${newRecordCount}/${recordSetSize}) is NOT a match!`}`);
376
+ debugData(`Strategy: ${JSON.stringify(detectionResult.strategy)}, Treshold: ${JSON.stringify(detectionResult.treshold)}`);
377
+ const matchResult = {
378
+ probability: detectionResult.probability,
379
+ candidate: {
380
+ id: candidateId,
381
+ record: candidateRecord
382
+ }
383
+ };
384
+ const strategyResult = {
385
+ strategy: detectionResult.strategy,
386
+ treshold: detectionResult.treshold
387
+ };
388
+ const newMatch = returnStrategy ? { ...matchResult,
389
+ ...strategyResult
390
+ } : { ...matchResult
391
+ };
392
+ debugData(`${JSON.stringify(newMatch)}`);
393
+ return handleRecordMatch(detectionResult.match, newMatch);
394
+ }
395
+
396
+ const newRecordNonMatchCount = recordNonMatchCount + 1;
397
+ debugData(`- Total nonMatches after this detection: ${newRecordNonMatchCount}`);
398
+ return iterateRecords({
399
+ records: records.slice(1),
400
+ recordSetSize,
401
+ maxMatches,
402
+ matches,
403
+ recordMatches,
404
+ recordCount: newRecordCount,
405
+ recordNonMatches,
406
+ recordDuplicateCount,
407
+ recordNonMatchCount: newRecordNonMatchCount
408
+ });
409
+ }
410
+
411
+ function handleRecordMatch(isMatch, newMatch) {
412
+ const newRecordMatches = isMatch ? recordMatches.concat(newMatch) : recordMatches;
413
+ const newRecordNonMatches = isMatch ? recordNonMatches : recordNonMatches.concat(newMatch);
414
+ debugData(`- Total matches after this detection: ${matches.concat(newRecordMatches).length} (max: ${maxMatches})`); // eslint-disable-next-line functional/no-conditional-statement
415
+
416
+ if (returnNonMatches) {
417
+ debugData(`- Total nonMatches after this detection: ${nonMatches.concat(newRecordNonMatches).length}`);
418
+ }
419
+
420
+ if (maxMatchesFound({
421
+ matches: matches.concat(newRecordMatches),
422
+ maxMatches
423
+ })) {
424
+ debug(`MaxMatches (${maxMatches}) reached, handled candidates in record set: ${newRecordCount} non-handled candidates in record set ${recordSetSize - newRecordCount}`);
425
+ return {
426
+ matches: newRecordMatches,
427
+ nonMatches: returnNonMatches ? newRecordNonMatches : [],
428
+ duplicateCount: recordDuplicateCount,
429
+ nonMatchCount: recordNonMatchCount
430
+ };
122
431
  }
432
+
433
+ return iterateRecords({
434
+ records: records.slice(1),
435
+ recordSetSize,
436
+ maxMatches,
437
+ matches,
438
+ recordMatches: newRecordMatches,
439
+ recordCount: newRecordCount,
440
+ recordNonMatches: returnNonMatches ? newRecordNonMatches : [],
441
+ duplicateCount: recordDuplicateCount,
442
+ recordNonMatchCount
443
+ });
444
+ }
445
+
446
+ function candidateNotInMatches(matches, candidate) {
447
+ debug(`Checking that record ${candidate.id} is not already included in ${matches.length} matches/nonMatches`);
448
+ const newCandidateId = candidate.id;
449
+ debugData(`newCandidateId: ${newCandidateId}`);
450
+ const result = matches.find(({
451
+ candidate
452
+ }) => candidate.id === newCandidateId);
453
+ debugData(`Result: ${result}`);
454
+
455
+ if (result) {
456
+ debug(`${candidate.id} was already handled.`);
457
+ return false;
458
+ }
459
+
460
+ debug(`${candidate.id} not found in matches/nonMatches`);
461
+ return true;
462
+ }
463
+ }
464
+
465
+ function maxMatchesFound({
466
+ matches,
467
+ maxMatches
468
+ }) {
469
+ if (maxMatches && matches.length >= maxMatches) {
470
+ debug(`Stopping recordSet iteration: maxMatches (${maxMatches}) for matcher job found.`);
471
+ return true;
123
472
  }
124
473
  }
125
474
  };
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/index.js"],"names":["detection","detectionOptions","search","searchOptions","maxMatches","maxCandidates","debug","detect","record","iterate","initialState","matches","candidateCount","records","state","length","queriesLeft","matchResult","iterateRecords","newMatches","concat","maxCandidatesRetrieved","candidate","candidateRecord","id","candidateId","match","probability","slice"],"mappings":";;;;;;;AA4BA;;AACA;;;;AACA;;;;;;;;;;AA9BA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AAC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AL,GAAeF,OAAO,CAACK,KAAD,EAAQH,OAAR,EAAiBC,cAAc,GAAGC,OAAO,CAACE,MAA1C,CAAnD;AACD;;AAEDT,MAAAA,KAAK,CAAE,kDAAiDK,OAAO,CAACI,MAAO,EAAlE,CAAL;AACA,aAAOJ,OAAP;;AAEA,eAASU,sBAAT,GAAkC;AAChC,YAAIT,cAAc,GAAGC,OAAO,CAACE,MAAzB,GAAkCV,aAAtC,EAAqD;AACnDC,UAAAA,KAAK,CAAE,4EAAF,CAAL;AACA,iBAAO,IAAP;AACD;AACF;;AAED,eAASY,cAAT,CAAwBL,OAAxB,EAAiC;AAC/B,cAAM,CAACS,SAAD,IAAcT,OAApB;;AAEA,YAAIS,SAAJ,EAAe;AACb,gBAAM;AAACd,YAAAA,MAAM,EAAEe,eAAT;AAA0BC,YAAAA,EAAE,EAAEC;AAA9B,cAA6CH,SAAnD;AACA,gBAAM;AAACI,YAAAA,KAAD;AAAQC,YAAAA;AAAR,cAAuBpB,MAAM,CAACC,MAAD,EAASe,eAAT,CAAnC;;AAEA,cAAIG,KAAJ,EAAW;AACT,mBAAO;AACLC,cAAAA,WADK;AAELL,cAAAA,SAAS,EAAE;AACTE,gBAAAA,EAAE,EAAEC,WADK;AAETjB,gBAAAA,MAAM,EAAEe;AAFC;AAFN,aAAP;AAOD;;AAED,iBAAOL,cAAc,CAACL,OAAO,CAACe,KAAR,CAAc,CAAd,CAAD,CAArB;AACD;AACF;AACF;AACF,GAzDD;AA0DD,C","sourcesContent":["/**\n*\n* @licstart The following is the entire license notice for the JavaScript code in this file.\n*\n* Melinda record matching modules for Javascript\n*\n* Copyright (C) 2020 University Of Helsinki (The National Library Of Finland)\n*\n* This file is part of melinda-record-matching-js\n*\n* melinda-record-matching-js program is free software: you can redistribute it and/or modify\n* it under the terms of the GNU Lesser General Public License as\n* published by the Free Software Foundation, either version 3 of the\n* License, or (at your option) any later version.\n*\n* melinda-record-matching-js is distributed in the hope that it will be useful,\n* but WITHOUT ANY WARRANTY; without even the implied warranty of\n* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n* GNU Lesser General Public License for more details.\n*\n* You should have received a copy of the GNU Affero General Public License\n* along with this program. If not, see <http://www.gnu.org/licenses/>.\n*\n* @licend The above is the entire license notice\n* for the JavaScript code in this file.\n*\n*/\n\nimport createDebugLogger from 'debug';\nimport createSearchInterface, * as candidateSearch from './candidate-search';\nimport createDetectionInterface, * as matchDetection from './match-detection';\n\nexport {candidateSearch, matchDetection};\n\nexport default ({detection: detectionOptions, search: searchOptions, maxMatches = 1, maxCandidates = 25}) => {\n const debug = createDebugLogger('@natlibfi/melinda-record-matching:index');\n const detect = createDetectionInterface(detectionOptions);\n\n return record => {\n const search = createSearchInterface({...searchOptions, record});\n return iterate();\n\n // eslint-disable-next-line max-statements\n async function iterate(initialState = {}, matches = [], candidateCount = 0) {\n const {records, ...state} = await search(initialState);\n\n if (records.length > 0 || state.queriesLeft > 0) {\n debug(`Checking ${records.length} candidates for matches`);\n\n const matchResult = iterateRecords(records);\n\n if (matchResult) {\n const newMatches = matches.concat(matchResult);\n\n if (newMatches.length === maxMatches) {\n return newMatches;\n }\n\n return maxCandidatesRetrieved() ? newMatches : iterate(state, newMatches, candidateCount + records.length);\n }\n\n return maxCandidatesRetrieved() ? matches : iterate(state, matches, candidateCount + records.length);\n }\n\n debug(`No (more) candidate records to check, matches: ${matches.length}`);\n return matches;\n\n function maxCandidatesRetrieved() {\n if (candidateCount + records.length > maxCandidates) {\n debug(`Stopped searching because maximum number of candidates have been retrieved`);\n return true;\n }\n }\n\n function iterateRecords(records) {\n const [candidate] = records;\n\n if (candidate) {\n const {record: candidateRecord, id: candidateId} = candidate;\n const {match, probability} = detect(record, candidateRecord);\n\n if (match) {\n return {\n probability,\n candidate: {\n id: candidateId,\n record: candidateRecord\n }\n };\n }\n\n return iterateRecords(records.slice(1));\n }\n }\n }\n };\n};\n"],"file":"index.js"}
1
+ {"version":3,"sources":["../src/index.js"],"names":["detection","detectionOptions","search","searchOptions","maxMatches","maxCandidates","returnStrategy","returnQuery","returnNonMatches","debug","debugData","extend","JSON","stringify","detect","record","iterate","initialState","matches","candidateCount","nonMatches","duplicateCount","nonMatchCount","records","state","length","recordSetSize","newCandidateCount","handleRecordSet","queriesLeft","searchCounter","query","returnResult","stopReason","matchResult","iterateRecords","newDuplicateCount","newNonMatchCount","newMatches","newNonMatches","handleMatchResult","maxMatchesFound","maxCandidatesRetrieved","concat","addQuery","map","match","matchQuery","checkCounts","matchStatus","getMatchState","result","matchCount","chosenNonMatchCount","totalHandled","resultSetOffset","totalRecords","queryCandidateCounter","maxedQueries","searchesLeft","nonRetrieved","maxedQueriesStopReason","newStopReason","status","recordMatches","recordNonMatches","recordCount","recordDuplicateCount","recordNonMatchCount","candidate","newRecordCount","candidateNotInMatches","candidateRecord","id","candidateId","detectionResult","handleDetectionResult","slice","strategy","treshold","probability","strategyResult","newMatch","handleRecordMatch","newRecordNonMatchCount","isMatch","newRecordMatches","newRecordNonMatches","newCandidateId","find"],"mappings":";;;;;;;AA4BA;;AACA;;;;AACA;;;;;;;;;;AA9BA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;eAQe,CAAC;AAACA,EAAAA,SAAS,EAAEC,gBAAZ;AAA8BC,EAAAA,MAAM,EAAEC,aAAtC;AAAqDC,EAAAA,UAAU,GAAG,CAAlE;AAAqEC,EAAAA,aAAa,GAAG,EAArF;AAAyFC,EAAAA,cAAc,GAAG,KAA1G;AAAiHC,EAAAA,WAAW,GAAG,KAA/H;AAAsIC,EAAAA,gBAAgB,GAAG;AAAzJ,CAAD,KAAqK;AAClL,QAAMC,KAAK,GAAG,oBAAkB,yCAAlB,CAAd;AACA,QAAMC,SAAS,GAAGD,KAAK,CAACE,MAAN,CAAa,MAAb,CAAlB;AAEAD,EAAAA,SAAS,CAAE,qBAAoB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C,eAAT,CAAyB;AAACvB,MAAAA,OAAD;AAAUd,MAAAA;AAAV,KAAzB,EAAgD;AAC9C,UAAIA,UAAU,IAAIc,OAAO,CAACO,MAAR,IAAkBrB,UAApC,EAAgD;AAC9CK,QAAAA,KAAK,CAAE,6CAA4CL,UAAW,0BAAzD,CAAL;AACA,eAAO,IAAP;AACD;AACF;AACF,GAnQD;AAoQD,C","sourcesContent":["/**\n*\n* @licstart The following is the entire license notice for the JavaScript code in this file.\n*\n* Melinda record matching modules for Javascript\n*\n* Copyright (C) 2020-2022 University Of Helsinki (The National Library Of Finland)\n*\n* This file is part of melinda-record-matching-js\n*\n* melinda-record-matching-js program is free software: you can redistribute it and/or modify\n* it under the terms of the GNU Lesser General Public License as\n* published by the Free Software Foundation, either version 3 of the\n* License, or (at your option) any later version.\n*\n* melinda-record-matching-js is distributed in the hope that it will be useful,\n* but WITHOUT ANY WARRANTY; without even the implied warranty of\n* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n* GNU Lesser General Public License for more details.\n*\n* You should have received a copy of the GNU Affero General Public License\n* along with this program. If not, see <http://www.gnu.org/licenses/>.\n*\n* @licend The above is the entire license notice\n* for the JavaScript code in this file.\n*\n*/\n\nimport createDebugLogger from 'debug';\nimport createSearchInterface, * as candidateSearch from './candidate-search';\nimport createDetectionInterface, * as matchDetection from './match-detection';\n\nexport {candidateSearch, matchDetection};\n\nexport default ({detection: detectionOptions, search: searchOptions, maxMatches = 1, maxCandidates = 25, returnStrategy = false, returnQuery = false, returnNonMatches = false}) => {\n const debug = createDebugLogger('@natlibfi/melinda-record-matching:index');\n const debugData = debug.extend('data');\n\n debugData(`DetectionOptions: ${JSON.stringify(detectionOptions)}`);\n debugData(`SearchOptions: ${JSON.stringify(searchOptions)}`);\n debugData(`MaxMatches: ${JSON.stringify(maxMatches)}`);\n debugData(`MaxCandidates: ${JSON.stringify(maxCandidates)}`);\n debugData(`ReturnStrategy: ${JSON.stringify(returnStrategy)}`);\n debugData(`ReturnQuery: ${JSON.stringify(returnQuery)}`);\n debugData(`ReturnNonMatches: ${JSON.stringify(returnNonMatches)}`);\n\n const detect = createDetectionInterface(detectionOptions, returnStrategy);\n\n return record => {\n const search = createSearchInterface({...searchOptions, record, maxCandidates});\n return iterate({});\n\n // candidateCount : amount of candidate records retrived from SRU for matching, NOT including current record set\n // matches : candidates that have been detected as matches by current matcher job\n // nonMatches : candidates that have been detected as non-matches by current matcher job (only if returnNonMatches is 'true')\n // duplicateCount : amount of candidate records that were retrieved from the SRU but not handled further because they were already found in the matches/nonMatches\n\n // state.totalRecords : amount of candidate records available to the current query (undefined, if there was no queries left)\n // state.query : current query (undefined if there was no queries left)\n // state.searchCounter : sequence for current search for current query (undefined, if there we no queries left)\n // state.queryCandidateCounter: amount of candidate records retrieved from SRU for matching for current query, including the current record set (undefined if there were no queries left)\n // state.queriesLeft : amount of queries left\n // state.queryCounter : sequence for current query\n // state.maxedQueries : queries that resulted in more than serverMaxResults hits\n\n async function iterate({initialState = {}, matches = [], candidateCount = 0, nonMatches = [], duplicateCount = 0, nonMatchCount = 0}) {\n debugData(`Starting next matcher iteration.`);\n const {records, ...state} = await search(initialState);\n\n debugData(`Current state: ${JSON.stringify(state)}, matches: ${matches.length}, candidateCount: ${candidateCount}, nonMatches: ${nonMatches.length}`);\n const recordSetSize = records.length;\n const newCandidateCount = candidateCount + recordSetSize;\n\n if (recordSetSize > 0) {\n return handleRecordSet();\n }\n\n if (state.queriesLeft > 0) {\n debug(`Empty record set ${state.searchCounter} for ${state.query}, but there are ${state.queriesLeft} queries left`);\n return iterate({initialState: state, matches, candidateCount: newCandidateCount, nonMatches, duplicateCount});\n }\n\n debug(`No (more) candidate records to check, no more queries left, matches: ${matches.length}`);\n return returnResult({matches, state, stopReason: '', nonMatches, candidateCount: newCandidateCount, duplicateCount});\n\n function handleRecordSet() {\n debug(`Checking record set of ${recordSetSize} candidate records for possible matches, found by ${state.searchCounter} search for ${state.query}`);\n\n const matchResult = iterateRecords({records, recordSetSize, maxMatches, matches, nonMatches});\n const newDuplicateCount = duplicateCount + matchResult.duplicateCount;\n const newNonMatchCount = nonMatchCount + matchResult.nonMatchCount;\n const {newMatches, newNonMatches} = handleMatchResult(matchResult, matches, nonMatches);\n\n if (maxMatchesFound({matches: newMatches, maxMatches})) {\n return returnResult({matches: newMatches, state, stopReason: 'maxMatches', nonMatches: newNonMatches, duplicateCount: newDuplicateCount, candidateCount: newCandidateCount, nonMatchCount: newNonMatchCount});\n }\n\n if (maxCandidatesRetrieved(newCandidateCount, maxCandidates)) {\n return returnResult({matches: newMatches, state, stopReason: 'maxCandidates', nonMatches: newNonMatches, duplicateCount: newDuplicateCount, candidateCount: newCandidateCount, nonMatchCount: newNonMatchCount});\n }\n\n return iterate({initialState: state, matches: newMatches, candidateCount: newCandidateCount, nonMatches: newNonMatches, duplicateCount: newDuplicateCount, nonMatchCount: newNonMatchCount});\n }\n\n function handleMatchResult(matchResult, matches, nonMatches) {\n debugData(`- Amount of new matches from record set: ${matchResult.matches.length}`);\n // eslint-disable-next-line functional/no-conditional-statement\n if (returnNonMatches) {\n debugData(`- Amount of new nonMatches from record set: ${matchResult.nonMatches.length}`);\n }\n\n const newMatches = matches.concat(returnQuery ? addQuery(matchResult.matches) : matchResult.matches);\n const newNonMatches = returnNonMatches ? nonMatches.concat(returnQuery ? addQuery(matchResult.nonMatches) : matchResult.nonMatches) : [];\n\n debugData(`- Total amount of matches: ${newMatches.length}`);\n // eslint-disable-next-line functional/no-conditional-statement\n if (returnNonMatches) {\n debugData(`- Total amount of nonMatches: ${newNonMatches.length}`);\n }\n\n return {newMatches, newNonMatches};\n }\n\n function addQuery(matches) {\n debugData(`Adding query ${state.query} to matches`);\n return matches.map((match) => ({...match, matchQuery: state.query}));\n }\n\n function maxCandidatesRetrieved(candidateCount, maxCandidates) {\n debugData(`Total amount of candidate records retrieved: ${newCandidateCount} (max: ${maxCandidates})`);\n if (maxCandidates && candidateCount >= maxCandidates) {\n debug(`Stopped matching because maximum number of candidate records ${candidateCount} / ${maxCandidates} have been retrieved`);\n return true;\n }\n }\n }\n\n // matches : array of matching candidate records\n // nonMatches : array of nonMatching candidate records (if returnNonMatches option is true, otherwise empty array)\n // - candidate.id\n // - candidate.record\n // - probability\n // - strategy (if returnStrategy option is true)\n // - treshold (if returnStrategy option is true)\n // - matchQuery (if returnQuery option is true)\n\n // we could have here also returnRecords/returnMatchRecords/returnNonMatchRecord options that could be turned false for not to return actual record data\n\n // matchStatus.status: boolean, true if matcher retrieved and handled all found candidate records, false if it did not\n // matchStatus.stopReason: string ('maxMatches','maxCandidates','maxedQueries',empty string/undefined), reason for stopping retrieving or handling the candidate records\n // - only one stopReason is returned (if there would be several possible stopReasons, stopReason is picked in the above order)\n // - currently stopReason can be non-empty also in cases where status is true, if matcher hit the stop reason when handling the last available candidate record\n\n function returnResult({matches, state, stopReason, nonMatches, duplicateCount, candidateCount, nonMatchCount}) {\n checkCounts({matches, nonMatches, candidateCount, duplicateCount, nonMatchCount});\n const matchStatus = getMatchState(state, stopReason);\n // add nonMatches to result only if returnNonMatches is 'true', otherwise nonMatches have not been gathered\n const result = returnNonMatches ? {matches, matchStatus, nonMatches} : {matches, matchStatus};\n debugData(`${JSON.stringify(result)}`);\n return result;\n\n // note that in cases where the matching has been stopped because of maxMatches checkCounts won't (in most cases) match\n\n function checkCounts({matches, nonMatches, candidateCount, duplicateCount, nonMatchCount}) {\n const matchCount = matches.length;\n debugData(`Return nonMatches: ${returnNonMatches}`);\n const chosenNonMatchCount = returnNonMatches ? nonMatches.length : nonMatchCount;\n const totalHandled = matchCount + nonMatchCount + duplicateCount;\n debug(`candidateCount: ${candidateCount}, matches: ${matchCount}, nonMatches: ${chosenNonMatchCount}, duplicateCount: ${duplicateCount}`);\n debug(`We got result for ${totalHandled} / ${candidateCount} retrieved candidates`);\n if (totalHandled !== candidateCount) {\n debug(`WARNING: Missing results for ${candidateCount - totalHandled} candidates`);\n return;\n }\n return;\n }\n\n function getMatchState(state, stopReason) {\n debugData(`${JSON.stringify(state)}`);\n debug(`Queries left ${state.queriesLeft}, Searches for current query left: ${state.resultSetOffset && state.resultSetOffset <= state.totalRecords}, non-retrieved records: ${state.totalRecords - state.queryCandidateCounter}, maxedQueries (${state.maxedQueries.length}): ${state.maxedQueries}`);\n\n debugData(`StopReason: <${stopReason}>`);\n\n const searchesLeft = state.resultSetOffset && state.resultSetOffset <= state.totalRecords;\n const nonRetrieved = searchesLeft ? state.totalRecords - state.queryCandidateCounter : 0;\n debugData(`nonRetrieved: ${nonRetrieved}`);\n\n if (state.queriesLeft > 0 || nonRetrieved > 0 || state.maxedQueries.length > 0) {\n const maxedQueriesStopReason = state.maxedQueries.length > 0 ? 'maxedQueries' : '';\n const newStopReason = stopReason === '' ? maxedQueriesStopReason : stopReason;\n debugData(`MaxedQueriesStopReason: <${maxedQueriesStopReason}>`);\n debugData(`NewStopReason: <${newStopReason}>`);\n debug(`Match status: false`);\n return {status: false, stopReason: newStopReason};\n }\n\n debug(`Match status: true`);\n return {status: true, stopReason};\n }\n }\n\n function iterateRecords({records, recordSetSize, maxMatches, matches = [], nonMatches = [], recordMatches = [], recordNonMatches = [], recordCount = 0, recordDuplicateCount = 0, recordNonMatchCount = 0}) {\n\n // recordSetSize : total amount of records in the current record set\n // recordCount : amount of records from the current record set that have been handled\n // maxMatches : setting for maximum amount found by current matcher job before the matcher job is stopped\n // recordDuplicateCount : amount of records from the current record set that are already included in matches/nonMatches results\n // recordNonMatchCount: amount of records from the current record set that are nonMatches (only is returnNonMatches setting is false)\n\n // records : non-handled records in the current record set\n // matches : found matches in the current matcher job\n // recordMatches : found matches in the current record set\n // recordNonMatches : found nonMatches in the current record set (only if returnNonMatches setting is true)\n\n const [candidate] = records;\n const newRecordCount = candidate ? recordCount + 1 : recordCount;\n\n // The matcher uses same matchDetection strategy for candidates from all candidate-searches -> matchDetection result for the same candidate is always same\n // Exceptions would happen if the candidate would have been updated in the database between candidate searches\n // Note that if returnNonMatches is false, matcher won't remember candidates that didn't match, so they will be matched again everytime they are retrieved by\n // different candidate search queries. Same candidate search query won't have duplicate records.\n\n if (candidate) {\n\n if (candidateNotInMatches(matches.concat(nonMatches), candidate)) {\n const {record: candidateRecord, id: candidateId} = candidate;\n debug(`Running matchDetection for record ${candidateId} (${newRecordCount}/${recordSetSize})`);\n const detectionResult = detect(record, candidateRecord);\n return handleDetectionResult(detectionResult, candidateId, candidateRecord);\n }\n return iterateRecords({records: records.slice(1), recordSetSize, maxMatches, matches, recordMatches, recordCount: newRecordCount, recordNonMatches, recordDuplicateCount: recordDuplicateCount + 1, recordNonMatchCount});\n }\n\n debug(`No more candidates, record set (${recordCount}/${recordSetSize}) done, ${recordMatches.length} matches found, ${recordDuplicateCount} candidates already handled, ${returnNonMatches ? `${recordNonMatches.length}` : `${recordNonMatchCount}`} nonMatches found.`);\n return {matches: recordMatches, nonMatches: returnNonMatches ? recordNonMatches : [], duplicateCount: recordDuplicateCount, nonMatchCount: recordNonMatchCount};\n\n function handleDetectionResult(detectionResult, candidateId, candidateRecord) {\n debugData(`MatchDetection results for ${candidateId} (${newRecordCount}/${recordSetSize}): ${JSON.stringify(detectionResult)}`);\n\n if (detectionResult.match || returnNonMatches) {\n debug(`${detectionResult.match ? `Record ${candidateId} (${newRecordCount}/${recordSetSize}) is a match!` : `Record ${candidateId} (${newRecordCount}/${recordSetSize}) is NOT a match!`}`);\n debugData(`Strategy: ${JSON.stringify(detectionResult.strategy)}, Treshold: ${JSON.stringify(detectionResult.treshold)}`);\n\n const matchResult = {\n probability: detectionResult.probability,\n candidate: {\n id: candidateId,\n record: candidateRecord\n }\n };\n const strategyResult = {\n strategy: detectionResult.strategy,\n treshold: detectionResult.treshold\n };\n const newMatch = returnStrategy ? {...matchResult, ...strategyResult} : {...matchResult};\n\n debugData(`${JSON.stringify(newMatch)}`);\n\n return handleRecordMatch(detectionResult.match, newMatch);\n }\n\n const newRecordNonMatchCount = recordNonMatchCount + 1;\n debugData(`- Total nonMatches after this detection: ${newRecordNonMatchCount}`);\n\n return iterateRecords({records: records.slice(1), recordSetSize, maxMatches, matches, recordMatches, recordCount: newRecordCount, recordNonMatches, recordDuplicateCount, recordNonMatchCount: newRecordNonMatchCount});\n }\n\n function handleRecordMatch(isMatch, newMatch) {\n const newRecordMatches = isMatch ? recordMatches.concat(newMatch) : recordMatches;\n const newRecordNonMatches = isMatch ? recordNonMatches : recordNonMatches.concat(newMatch);\n\n debugData(`- Total matches after this detection: ${matches.concat(newRecordMatches).length} (max: ${maxMatches})`);\n\n // eslint-disable-next-line functional/no-conditional-statement\n if (returnNonMatches) {\n debugData(`- Total nonMatches after this detection: ${nonMatches.concat(newRecordNonMatches).length}`);\n }\n\n if (maxMatchesFound({matches: matches.concat(newRecordMatches), maxMatches})) {\n debug(`MaxMatches (${maxMatches}) reached, handled candidates in record set: ${newRecordCount} non-handled candidates in record set ${recordSetSize - newRecordCount}`);\n return {matches: newRecordMatches, nonMatches: returnNonMatches ? newRecordNonMatches : [], duplicateCount: recordDuplicateCount, nonMatchCount: recordNonMatchCount};\n }\n\n return iterateRecords({records: records.slice(1), recordSetSize, maxMatches, matches, recordMatches: newRecordMatches, recordCount: newRecordCount, recordNonMatches: returnNonMatches ? newRecordNonMatches : [], duplicateCount: recordDuplicateCount, recordNonMatchCount});\n }\n\n function candidateNotInMatches(matches, candidate) {\n debug(`Checking that record ${candidate.id} is not already included in ${matches.length} matches/nonMatches`);\n const newCandidateId = candidate.id;\n debugData(`newCandidateId: ${newCandidateId}`);\n const result = matches.find(({candidate}) => candidate.id === newCandidateId);\n debugData(`Result: ${result}`);\n if (result) {\n debug(`${candidate.id} was already handled.`);\n return false;\n }\n debug(`${candidate.id} not found in matches/nonMatches`);\n return true;\n }\n }\n\n function maxMatchesFound({matches, maxMatches}) {\n if (maxMatches && matches.length >= maxMatches) {\n debug(`Stopping recordSet iteration: maxMatches (${maxMatches}) for matcher job found.`);\n return true;\n }\n }\n };\n};\n"],"file":"index.js"}