@natlibfi/melinda-record-matching 2.2.1-alpha.1 → 2.2.1-alpha.3

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.
Files changed (66) hide show
  1. package/dist/candidate-search/candidate-search-utils.js +55 -0
  2. package/dist/candidate-search/candidate-search-utils.js.map +1 -0
  3. package/dist/candidate-search/index.js +258 -0
  4. package/dist/candidate-search/index.js.map +1 -0
  5. package/dist/candidate-search/index.spec.js +148 -0
  6. package/dist/candidate-search/index.spec.js.map +1 -0
  7. package/dist/candidate-search/query-list/bib.js +230 -0
  8. package/dist/candidate-search/query-list/bib.js.map +1 -0
  9. package/dist/candidate-search/query-list/bib.spec.js +63 -0
  10. package/dist/candidate-search/query-list/bib.spec.js.map +1 -0
  11. package/dist/candidate-search/query-list/index.js +61 -0
  12. package/dist/candidate-search/query-list/index.js.map +1 -0
  13. package/dist/index.js +559 -0
  14. package/dist/index.js.map +1 -0
  15. package/dist/index.spec.js +125 -0
  16. package/dist/index.spec.js.map +1 -0
  17. package/dist/match-detection/features/bib/all-source-ids.js +116 -0
  18. package/dist/match-detection/features/bib/all-source-ids.js.map +1 -0
  19. package/dist/match-detection/features/bib/authors.js +107 -0
  20. package/dist/match-detection/features/bib/authors.js.map +1 -0
  21. package/dist/match-detection/features/bib/bibliographic-level.js +39 -0
  22. package/dist/match-detection/features/bib/bibliographic-level.js.map +1 -0
  23. package/dist/match-detection/features/bib/host-component.js +39 -0
  24. package/dist/match-detection/features/bib/host-component.js.map +1 -0
  25. package/dist/match-detection/features/bib/index.js +91 -0
  26. package/dist/match-detection/features/bib/index.js.map +1 -0
  27. package/dist/match-detection/features/bib/index.spec.js +87 -0
  28. package/dist/match-detection/features/bib/index.spec.js.map +1 -0
  29. package/dist/match-detection/features/bib/isbn.js +50 -0
  30. package/dist/match-detection/features/bib/isbn.js.map +1 -0
  31. package/dist/match-detection/features/bib/issn.js +50 -0
  32. package/dist/match-detection/features/bib/issn.js.map +1 -0
  33. package/dist/match-detection/features/bib/language.js +98 -0
  34. package/dist/match-detection/features/bib/language.js.map +1 -0
  35. package/dist/match-detection/features/bib/melinda-id.js +47 -0
  36. package/dist/match-detection/features/bib/melinda-id.js.map +1 -0
  37. package/dist/match-detection/features/bib/melinda-identifier-factory.js +88 -0
  38. package/dist/match-detection/features/bib/melinda-identifier-factory.js.map +1 -0
  39. package/dist/match-detection/features/bib/other-standard-identifier.js +50 -0
  40. package/dist/match-detection/features/bib/other-standard-identifier.js.map +1 -0
  41. package/dist/match-detection/features/bib/publication-time.js +47 -0
  42. package/dist/match-detection/features/bib/publication-time.js.map +1 -0
  43. package/dist/match-detection/features/bib/record-type.js +41 -0
  44. package/dist/match-detection/features/bib/record-type.js.map +1 -0
  45. package/dist/match-detection/features/bib/standard-identifier-factory.js +112 -0
  46. package/dist/match-detection/features/bib/standard-identifier-factory.js.map +1 -0
  47. package/dist/match-detection/features/bib/title.js +78 -0
  48. package/dist/match-detection/features/bib/title.js.map +1 -0
  49. package/dist/match-detection/features/index.js +11 -0
  50. package/dist/match-detection/features/index.js.map +1 -0
  51. package/dist/match-detection/index.js +162 -0
  52. package/dist/match-detection/index.js.map +1 -0
  53. package/dist/match-detection/index.spec.js +88 -0
  54. package/dist/match-detection/index.spec.js.map +1 -0
  55. package/dist/matching-utils.js +95 -0
  56. package/dist/matching-utils.js.map +1 -0
  57. package/package.json +18 -17
  58. package/src/candidate-search/query-list/bib.js +19 -18
  59. package/src/candidate-search/query-list/index.js +1 -1
  60. package/src/match-detection/features/bib/authors.js +5 -2
  61. package/src/match-detection/features/bib/language.js +2 -2
  62. package/src/match-detection/features/bib/publication-time.js +8 -3
  63. package/src/match-detection/features/bib/record-type.js +2 -0
  64. package/src/match-detection/features/bib/standard-identifier-factory.js +4 -3
  65. package/src/match-detection/features/bib/title.js +6 -4
  66. package/src/matching-utils.js +13 -4
package/dist/index.js ADDED
@@ -0,0 +1,559 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.matchDetection = exports.default = exports.candidateSearch = void 0;
7
+ var _debug = _interopRequireDefault(require("debug"));
8
+ var candidateSearch = _interopRequireWildcard(require("./candidate-search"));
9
+ exports.candidateSearch = candidateSearch;
10
+ var matchDetection = _interopRequireWildcard(require("./match-detection"));
11
+ exports.matchDetection = matchDetection;
12
+ function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
13
+ function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
14
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
15
+ /**
16
+ *
17
+ * @licstart The following is the entire license notice for the JavaScript code in this file.
18
+ *
19
+ * Melinda record matching modules for Javascript
20
+ *
21
+ * Copyright (C) 2020-2022 University Of Helsinki (The National Library Of Finland)
22
+ *
23
+ * This file is part of melinda-record-matching-js
24
+ *
25
+ * melinda-record-matching-js program is free software: you can redistribute it and/or modify
26
+ * it under the terms of the GNU Lesser General Public License as
27
+ * published by the Free Software Foundation, either version 3 of the
28
+ * License, or (at your option) any later version.
29
+ *
30
+ * melinda-record-matching-js is distributed in the hope that it will be useful,
31
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
32
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
33
+ * GNU Lesser General Public License for more details.
34
+ *
35
+ * You should have received a copy of the GNU Affero General Public License
36
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
37
+ *
38
+ * @licend The above is the entire license notice
39
+ * for the JavaScript code in this file.
40
+ *
41
+ */
42
+ //import inspect from 'util';
43
+ var _default = ({
44
+ detection: detectionOptions,
45
+ search: searchOptions,
46
+ maxMatches = 1,
47
+ maxCandidates = 25,
48
+ returnStrategy = false,
49
+ returnQuery = false,
50
+ returnNonMatches = false,
51
+ returnFailures = false
52
+ }) => {
53
+ const debug = (0, _debug.default)('@natlibfi/melinda-record-matching:index');
54
+ const debugData = debug.extend('data');
55
+ debugData(`DetectionOptions: ${JSON.stringify(detectionOptions)}`);
56
+ debugData(`SearchOptions: ${JSON.stringify(searchOptions)}`);
57
+ debugData(`MaxMatches: ${JSON.stringify(maxMatches)}`);
58
+ debugData(`MaxCandidates: ${JSON.stringify(maxCandidates)}`);
59
+ debugData(`ReturnStrategy: ${JSON.stringify(returnStrategy)}`);
60
+ debugData(`ReturnQuery: ${JSON.stringify(returnQuery)}`);
61
+ debugData(`ReturnNonMatches: ${JSON.stringify(returnNonMatches)}`);
62
+ debugData(`ReturnFailures: ${JSON.stringify(returnFailures)}`);
63
+ const detect = (0, matchDetection.default)(detectionOptions, returnStrategy);
64
+ return record => {
65
+ const search = (0, candidateSearch.default)({
66
+ ...searchOptions,
67
+ record,
68
+ maxCandidates
69
+ });
70
+ return iterate({});
71
+
72
+ // candidateCount : amount of candidate records retrived from SRU for matching, NOT including current record set
73
+ // matches : candidates that have been detected as matches by current matcher job
74
+ // nonMatches : candidates that have been detected as non-matches by current matcher job (only if returnNonMatches is 'true')
75
+ // duplicateCount : amount of candidate records that were retrieved from the SRU but not handled further because they were already found in the matches/nonMatches
76
+
77
+ // state.totalRecords : amount of candidate records available to the current query (undefined, if there was no queries left)
78
+ // state.query : current query (undefined if there was no queries left)
79
+ // state.searchCounter : sequence for current search for current query (undefined, if there we no queries left)
80
+ // 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)
81
+ // state.queriesLeft : amount of queries left
82
+ // state.queryCounter : sequence for current query
83
+ // state.maxedQueries : queries that resulted in more than serverMaxResults hits
84
+
85
+ async function iterate({
86
+ initialState = {},
87
+ matches = [],
88
+ candidateCount = 0,
89
+ nonMatches = [],
90
+ duplicateCount = 0,
91
+ nonMatchCount = 0,
92
+ conversionFailures = [],
93
+ matchErrors = []
94
+ }) {
95
+ debugData(`Starting next matcher iteration.`);
96
+ const {
97
+ records,
98
+ failures,
99
+ ...state
100
+ } = await search(initialState);
101
+ debugData(`Current state: ${JSON.stringify(state)}, matches: ${matches.length}, candidateCount: ${candidateCount}, nonMatches: ${nonMatches.length}, nonMatchCount: ${nonMatchCount}, conversionFailures: ${conversionFailures}, matchErrors: ${matchErrors.length}`);
102
+ const recordSetSize = records.length;
103
+ const failureSetSize = failures.length;
104
+ const newCandidateCount = candidateCount + recordSetSize + failureSetSize;
105
+ const newConversionFailures = conversionFailures.concat(failures);
106
+ debugData(`Failures: ${failures.length}, ConversionFailures: ${conversionFailures.length}, NewConversionFailures: ${newConversionFailures.length}`);
107
+ if (recordSetSize > 0) {
108
+ return handleRecordSet();
109
+ }
110
+ if (state.queriesLeft > 0) {
111
+ debug(`Empty record set ${state.searchCounter} for ${state.query}, but there are ${state.queriesLeft} queries left`);
112
+ return iterate({
113
+ initialState: state,
114
+ matches,
115
+ candidateCount: newCandidateCount,
116
+ nonMatches,
117
+ nonMatchCount,
118
+ duplicateCount,
119
+ conversionFailures: newConversionFailures,
120
+ matchErrors
121
+ });
122
+ }
123
+ debug(`No (more) candidate records to check, no more queries left, matches: ${matches.length}`);
124
+ return returnResult({
125
+ matches,
126
+ state,
127
+ stopReason: '',
128
+ nonMatches,
129
+ nonMatchCount,
130
+ candidateCount: newCandidateCount,
131
+ duplicateCount,
132
+ conversionFailures: newConversionFailures,
133
+ matchErrors
134
+ });
135
+ function handleRecordSet() {
136
+ debug(`Checking record set of ${recordSetSize} candidate records for possible matches, found by ${state.searchCounter} search for ${state.query}`);
137
+ const matchResult = iterateRecords({
138
+ records,
139
+ recordSetSize,
140
+ maxMatches,
141
+ matches,
142
+ nonMatches,
143
+ nonMatchCount
144
+ });
145
+ const newDuplicateCount = duplicateCount + matchResult.duplicateCount;
146
+ const newNonMatchCount = nonMatchCount + matchResult.nonMatchCount;
147
+ const {
148
+ newMatches,
149
+ newNonMatches,
150
+ newMatchErrors
151
+ } = handleMatchResult(matchResult, matches, nonMatches, matchErrors);
152
+ if (maxMatchesFound({
153
+ matches: newMatches,
154
+ maxMatches
155
+ })) {
156
+ return returnResult({
157
+ matches: newMatches,
158
+ state,
159
+ stopReason: 'maxMatches',
160
+ nonMatches: newNonMatches,
161
+ duplicateCount: newDuplicateCount,
162
+ candidateCount: newCandidateCount,
163
+ nonMatchCount: newNonMatchCount,
164
+ conversionFailures: newConversionFailures,
165
+ matchErrors: newMatchErrors
166
+ });
167
+ }
168
+ if (maxCandidatesRetrieved(newCandidateCount, maxCandidates)) {
169
+ return returnResult({
170
+ matches: newMatches,
171
+ state,
172
+ stopReason: 'maxCandidates',
173
+ nonMatches: newNonMatches,
174
+ duplicateCount: newDuplicateCount,
175
+ candidateCount: newCandidateCount,
176
+ nonMatchCount: newNonMatchCount,
177
+ conversionFailures: newConversionFailures,
178
+ matchErrors: newMatchErrors
179
+ });
180
+ }
181
+ return iterate({
182
+ initialState: state,
183
+ matches: newMatches,
184
+ candidateCount: newCandidateCount,
185
+ nonMatches: newNonMatches,
186
+ duplicateCount: newDuplicateCount,
187
+ nonMatchCount: newNonMatchCount,
188
+ conversionFailures: newConversionFailures,
189
+ matchErrors: newMatchErrors
190
+ });
191
+ }
192
+ function handleMatchResult(matchResult, matches, nonMatches, matchErrors) {
193
+ debugData(`- Amount of new matches from record set: ${matchResult.matches.length}`);
194
+ // eslint-disable-next-line functional/no-conditional-statement
195
+ if (returnNonMatches) {
196
+ debugData(`- Amount of new nonMatches from record set: ${matchResult.nonMatches.length}`);
197
+ }
198
+ const newMatches = matches.concat(returnQuery ? addQuery(matchResult.matches) : matchResult.matches);
199
+ const newNonMatches = returnNonMatches ? nonMatches.concat(returnQuery ? addQuery(matchResult.nonMatches) : matchResult.nonMatches) : [];
200
+ const newMatchErrors = matchErrors.concat(matchResult.matchErrors);
201
+ debugData(`- Total amount of matches: ${newMatches.length}`);
202
+ // eslint-disable-next-line functional/no-conditional-statement
203
+ if (returnNonMatches) {
204
+ debugData(`- Total amount of nonMatches: ${newNonMatches.length}`);
205
+ }
206
+ debugData(`MatchResult: ${JSON.stringify(matchResult)}`);
207
+ debugData(`Old matchErrors: ${JSON.stringify(matchErrors)}, matchErrors from matchResult: ${JSON.stringify(matchResult.matchErrors)}, New matchErrors: ${JSON.stringify(newMatchErrors)}`);
208
+ debugData(`- Total amount of matchErrors: ${newMatchErrors.length}`);
209
+ return {
210
+ newMatches,
211
+ newNonMatches,
212
+ newMatchErrors
213
+ };
214
+ }
215
+ function addQuery(matches) {
216
+ debugData(`Adding query ${state.query} to matches`);
217
+ return matches.map(match => ({
218
+ ...match,
219
+ matchQuery: state.query
220
+ }));
221
+ }
222
+ function maxCandidatesRetrieved(candidateCount, maxCandidates) {
223
+ debugData(`Total amount of candidate records retrieved: ${newCandidateCount} (max: ${maxCandidates})`);
224
+ if (maxCandidates && candidateCount >= maxCandidates) {
225
+ debug(`Stopped matching because maximum number of candidate records ${candidateCount} / ${maxCandidates} have been retrieved`);
226
+ return true;
227
+ }
228
+ }
229
+ }
230
+
231
+ // matches : array of matching candidate records
232
+ // nonMatches : array of nonMatching candidate records (if returnNonMatches option is true, otherwise empty array)
233
+ // - candidate.id
234
+ // - candidate.record
235
+ // - probability
236
+ // - strategy (if returnStrategy option is true)
237
+ // - treshold (if returnStrategy option is true)
238
+ // - matchQuery (if returnQuery option is true)
239
+ // failures: array of conversionFailures from candidate-search and matchErrors from matchDetection in error format {status, payload: {message, id}} if returnFailures is true
240
+
241
+ // we could have here also returnRecords/returnMatchRecords/returnNonMatchRecord options that could be turned false for not to return actual record data
242
+
243
+ // matchStatus.status: boolean, true if matcher retrieved and handled all found candidate records, false if it did not
244
+ // matchStatus.stopReason: string ('maxMatches','maxCandidates','maxedQueries','conversionFailures', empty string/undefined), reason for stopping retrieving or handling the candidate records
245
+ // - only one stopReason is returned (if there would be several possible stopReasons, stopReason is picked in the above order)
246
+ // - 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
247
+
248
+ function returnResult({
249
+ matches,
250
+ state,
251
+ stopReason,
252
+ nonMatches,
253
+ duplicateCount,
254
+ candidateCount,
255
+ nonMatchCount,
256
+ conversionFailures,
257
+ matchErrors
258
+ }) {
259
+ const conversionFailureCount = conversionFailures.length;
260
+ const matchErrorCount = matchErrors.length;
261
+ checkCounts({
262
+ matches,
263
+ nonMatches,
264
+ candidateCount,
265
+ duplicateCount,
266
+ nonMatchCount,
267
+ conversionFailureCount,
268
+ matchErrorCount
269
+ });
270
+ const matchStatus = getMatchState(state, stopReason, conversionFailureCount, matchErrorCount);
271
+ // add nonMatches to result only if returnNonMatches is 'true', otherwise nonMatches have not been gathered
272
+ const matchesResult = returnNonMatches ? {
273
+ matches,
274
+ matchStatus,
275
+ nonMatches
276
+ } : {
277
+ matches,
278
+ matchStatus
279
+ };
280
+ const failures = [...conversionFailures, ...matchErrors];
281
+ const result = returnFailures ? {
282
+ ...matchesResult,
283
+ conversionFailures: failures
284
+ } : matchesResult;
285
+ debugData(`ReturnFailures ${returnFailures}`);
286
+ debugData(`${JSON.stringify(result)}`);
287
+ return result;
288
+
289
+ // note that in cases where the matching has been stopped because of maxMatches checkCounts won't (in most cases) match
290
+
291
+ function checkCounts({
292
+ matches,
293
+ nonMatches,
294
+ candidateCount,
295
+ duplicateCount,
296
+ nonMatchCount,
297
+ conversionFailureCount,
298
+ matchErrorCount
299
+ }) {
300
+ const matchCount = matches.length;
301
+ debugData(`Return nonMatches: ${returnNonMatches}`);
302
+ const chosenNonMatchCount = returnNonMatches ? nonMatches.length : nonMatchCount;
303
+ const totalHandled = matchCount + chosenNonMatchCount + duplicateCount;
304
+ debug(`candidateCount: ${candidateCount}, matches: ${matchCount}, nonMatches: ${chosenNonMatchCount}, duplicateCount: ${duplicateCount}, conversionFailureCount: ${conversionFailureCount}, matchErrorCount: ${matchErrorCount}`);
305
+ debug(`We got result for ${totalHandled} / ${candidateCount} retrieved candidates`);
306
+ if (totalHandled !== candidateCount) {
307
+ debug(`WARNING: Missing results for ${candidateCount - totalHandled} candidates`);
308
+ return;
309
+ }
310
+ return;
311
+ }
312
+
313
+ // eslint-disable-next-line max-statements
314
+ function getMatchState(state, stopReason, conversionFailuresCount, matchErrorCount) {
315
+ debugData(`${JSON.stringify(state)}`);
316
+ debug(`We had ${conversionFailuresCount} retrieved candidates that could not be converted.`);
317
+ debug(`We had ${matchErrorCount} retrieved candidates that errored in matchDetection.`);
318
+ 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}`);
319
+ debugData(`StopReason: <${stopReason}>`);
320
+ const searchesLeft = state.resultSetOffset && state.resultSetOffset <= state.totalRecords;
321
+ const nonRetrieved = searchesLeft ? state.totalRecords - state.queryCandidateCounter : 0;
322
+ debugData(`nonRetrieved: ${nonRetrieved}`);
323
+
324
+ // matchStatus.stopReason: string ('maxMatches','maxCandidates','maxedQueries','conversionFailures', empty string/undefined), reason for stopping retrieving or handling the candidate records
325
+ // 'maxMatches' and 'maxCandidates' are in stopReason, 'maxedQueries', 'conversionFailures' and 'matchErrors' are created here
326
+
327
+ if (state.queriesLeft > 0 || nonRetrieved > 0 || state.maxedQueries.length > 0 || conversionFailureCount > 0 || matchErrorCount > 0) {
328
+ const maxedQueriesStopReason = state.maxedQueries.length > 0 ? 'maxedQueries' : undefined;
329
+ const conversionFailuresStopReason = conversionFailureCount > 0 ? 'conversionFailures' : undefined;
330
+ const matchErrorsStopReason = matchErrorCount > 0 ? 'matchErrors' : undefined;
331
+ const newStopReason = stopReason === '' || stopReason === undefined ? maxedQueriesStopReason || conversionFailuresStopReason || matchErrorsStopReason : stopReason;
332
+ debugData(`MaxedQueriesStopReason: <${maxedQueriesStopReason}>`);
333
+ debugData(`ConversionFailureStopReason <${conversionFailuresStopReason}>`);
334
+ debugData(`MatchErrorsStopReason <${matchErrorsStopReason}>`);
335
+ debugData(`NewStopReason: <${newStopReason}>`);
336
+ debug(`Match status: false`);
337
+ return {
338
+ status: false,
339
+ stopReason: newStopReason
340
+ };
341
+ }
342
+ debug(`Match status: true`);
343
+ return {
344
+ status: true,
345
+ stopReason
346
+ };
347
+ }
348
+ }
349
+
350
+ // NOTES:
351
+ // - we could optimize by creating the featureSet for the incoming record once and using it for all database/candidateRecords
352
+ // - if creating the featureSet for the incoming record fails we have an unprocessable entity
353
+ // - if creating the featureSet for a candidate record fails we could skip that candidate - but list the case as a detectionFailure, same as conversionFailures
354
+
355
+ // eslint-disable-next-line max-statements
356
+ function iterateRecords({
357
+ records,
358
+ recordSetSize,
359
+ maxMatches,
360
+ matches = [],
361
+ nonMatches = [],
362
+ recordMatches = [],
363
+ recordNonMatches = [],
364
+ recordCount = 0,
365
+ recordDuplicateCount = 0,
366
+ recordNonMatchCount = 0,
367
+ recordMatchErrors = []
368
+ }) {
369
+ // recordSetSize : total amount of records in the current record set
370
+ // recordCount : amount of records from the current record set that have been handled
371
+ // maxMatches : setting for maximum amount found by current matcher job before the matcher job is stopped
372
+ // recordDuplicateCount : amount of records from the current record set that are already included in matches/nonMatches results
373
+ // recordNonMatchCount: amount of records from the current record set that are nonMatches (only is returnNonMatches setting is false)
374
+
375
+ // records : non-handled records in the current record set
376
+ // matches : found matches in the current matcher job
377
+ // recordMatches : found matches in the current record set
378
+ // recordNonMatches : found nonMatches in the current record set (only if returnNonMatches setting is true)
379
+ // recordMatchErrors: errored matchDetection in the current record set
380
+
381
+ const [candidate] = records;
382
+ const newRecordCount = candidate ? recordCount + 1 : recordCount;
383
+
384
+ // The matcher uses same matchDetection strategy for candidates from all candidate-searches -> matchDetection result for the same candidate is always same
385
+ // Exceptions would happen if the candidate would have been updated in the database between candidate searches
386
+ // 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
387
+ // different candidate search queries. Same candidate search query won't have duplicate records.
388
+
389
+ /* We could optimize and detect all retrieved candidates at once
390
+ const candidateRecords = records.map(record => record.record);
391
+ const recordsIsArray = Array.isArray(candidateRecords);
392
+ debug(`records is an array: ${recordsIsArray}`);
393
+ const result = detect(record, candidateRecords);
394
+ debug(`${JSON.stringify(result)}`);
395
+ */
396
+
397
+ if (candidate) {
398
+ // eslint-disable-next-line functional/no-conditional-statement
399
+ if (candidateNotInMatches(matches.concat(nonMatches), candidate)) {
400
+ const {
401
+ record: candidateRecord,
402
+ id: candidateId
403
+ } = candidate;
404
+ try {
405
+ debug(`Running matchDetection for record ${candidateId} (${newRecordCount}/${recordSetSize})`);
406
+ // we should handle errors from detection somehow - ie. cases where either record or candidateRecord errors
407
+ const detectionResult = detect(record, candidateRecord);
408
+ return handleDetectionResult(detectionResult, candidateId, candidateRecord);
409
+ } catch (error) {
410
+ debug(`MatchDetection errored: database record ${candidateId}: ${error}`);
411
+ const matchError = {
412
+ status: 422,
413
+ payload: {
414
+ message: `Matching errored for database record ${candidateId}. ${error.message}.`,
415
+ id: candidateId
416
+ }
417
+ };
418
+ const newRecordMatchErrors = recordMatchErrors.concat(matchError);
419
+ return iterateRecords({
420
+ records: records.slice(1),
421
+ recordSetSize,
422
+ maxMatches,
423
+ matches,
424
+ recordMatches,
425
+ recordCount: newRecordCount,
426
+ recordNonMatches,
427
+ recordDuplicateCount,
428
+ recordNonMatchCount,
429
+ recordMatchErrors: newRecordMatchErrors
430
+ });
431
+ }
432
+ }
433
+ return iterateRecords({
434
+ records: records.slice(1),
435
+ recordSetSize,
436
+ maxMatches,
437
+ matches,
438
+ recordMatches,
439
+ recordCount: newRecordCount,
440
+ recordNonMatches,
441
+ recordDuplicateCount: recordDuplicateCount + 1,
442
+ recordNonMatchCount,
443
+ recordMatchErrors
444
+ });
445
+ }
446
+ debug(`No more candidates, record set (${recordCount}/${recordSetSize}) done, ${recordMatches.length} matches found, ${recordDuplicateCount} candidates already handled, ${returnNonMatches ? `${recordNonMatches.length}` : `${recordNonMatchCount}`} nonMatches found.`);
447
+ return {
448
+ matches: recordMatches,
449
+ nonMatches: returnNonMatches ? recordNonMatches : [],
450
+ duplicateCount: recordDuplicateCount,
451
+ nonMatchCount: recordNonMatchCount,
452
+ matchErrors: recordMatchErrors
453
+ };
454
+ function handleDetectionResult(detectionResult, candidateId, candidateRecord) {
455
+ debugData(`MatchDetection results for ${candidateId} (${newRecordCount}/${recordSetSize}): ${JSON.stringify(detectionResult)}`);
456
+ if (detectionResult.match || returnNonMatches) {
457
+ debug(`${detectionResult.match ? `Record ${candidateId} (${newRecordCount}/${recordSetSize}) is a match!` : `Record ${candidateId} (${newRecordCount}/${recordSetSize}) is NOT a match!`}`);
458
+ debugData(`Strategy: ${JSON.stringify(detectionResult.strategy)}, Treshold: ${JSON.stringify(detectionResult.treshold)}`);
459
+ const matchResult = {
460
+ probability: detectionResult.probability,
461
+ candidate: {
462
+ id: candidateId,
463
+ record: candidateRecord
464
+ }
465
+ };
466
+ const strategyResult = {
467
+ strategy: detectionResult.strategy,
468
+ treshold: detectionResult.treshold
469
+ };
470
+ const newMatch = returnStrategy ? {
471
+ ...matchResult,
472
+ ...strategyResult
473
+ } : {
474
+ ...matchResult
475
+ };
476
+ debugData(`${JSON.stringify(newMatch)}`);
477
+ return handleRecordMatch(detectionResult.match, newMatch);
478
+ }
479
+ const newRecordNonMatchCount = recordNonMatchCount + 1;
480
+ debugData(`- Total nonMatches after this detection: ${newRecordNonMatchCount}`);
481
+ return iterateRecords({
482
+ records: records.slice(1),
483
+ recordSetSize,
484
+ maxMatches,
485
+ matches,
486
+ recordMatches,
487
+ recordCount: newRecordCount,
488
+ recordNonMatches,
489
+ recordDuplicateCount,
490
+ recordNonMatchCount: newRecordNonMatchCount,
491
+ recordMatchErrors
492
+ });
493
+ }
494
+ function handleRecordMatch(isMatch, newMatch) {
495
+ const newRecordMatches = isMatch ? recordMatches.concat(newMatch) : recordMatches;
496
+ const newRecordNonMatches = isMatch ? recordNonMatches : recordNonMatches.concat(newMatch);
497
+ const newRecordNonMatchCount = isMatch ? recordNonMatchCount : recordNonMatchCount + 1;
498
+ debugData(`- Total matches after this detection: ${matches.concat(newRecordMatches).length} (max: ${maxMatches})`);
499
+
500
+ // eslint-disable-next-line functional/no-conditional-statement
501
+ if (returnNonMatches) {
502
+ debugData(`- Total nonMatches after this detection: ${nonMatches.concat(newRecordNonMatches).length}`);
503
+ }
504
+ debugData(`- Total nonMatchCount after this detection: ${recordNonMatchCount}`);
505
+ if (maxMatchesFound({
506
+ matches: matches.concat(newRecordMatches),
507
+ maxMatches
508
+ })) {
509
+ debug(`MaxMatches (${maxMatches}) reached, handled candidates in record set: ${newRecordCount} non-handled candidates in record set ${recordSetSize - newRecordCount}`);
510
+ return {
511
+ matches: newRecordMatches,
512
+ nonMatches: returnNonMatches ? newRecordNonMatches : [],
513
+ duplicateCount: recordDuplicateCount,
514
+ nonMatchCount: newRecordNonMatchCount,
515
+ matchErrors: recordMatchErrors
516
+ };
517
+ }
518
+ return iterateRecords({
519
+ records: records.slice(1),
520
+ recordSetSize,
521
+ maxMatches,
522
+ matches,
523
+ recordMatches: newRecordMatches,
524
+ recordCount: newRecordCount,
525
+ recordNonMatches: returnNonMatches ? newRecordNonMatches : [],
526
+ duplicateCount: recordDuplicateCount,
527
+ recordNonMatchCount: newRecordNonMatchCount,
528
+ matchErrors: recordMatchErrors
529
+ });
530
+ }
531
+ function candidateNotInMatches(matches, candidate) {
532
+ debug(`Checking that record ${candidate.id} is not already included in ${matches.length} matches/nonMatches`);
533
+ const newCandidateId = candidate.id;
534
+ debugData(`newCandidateId: ${newCandidateId}`);
535
+ const result = matches.find(({
536
+ candidate
537
+ }) => candidate.id === newCandidateId);
538
+ debugData(`Result: ${result}`);
539
+ if (result) {
540
+ debug(`${candidate.id} was already handled.`);
541
+ return false;
542
+ }
543
+ debug(`${candidate.id} not found in matches/nonMatches`);
544
+ return true;
545
+ }
546
+ }
547
+ function maxMatchesFound({
548
+ matches,
549
+ maxMatches
550
+ }) {
551
+ if (maxMatches && matches.length >= maxMatches) {
552
+ debug(`Stopping recordSet iteration: maxMatches (${maxMatches}) for matcher job found.`);
553
+ return true;
554
+ }
555
+ }
556
+ };
557
+ };
558
+ exports.default = _default;
559
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","names":["detection","detectionOptions","search","searchOptions","maxMatches","maxCandidates","returnStrategy","returnQuery","returnNonMatches","returnFailures","debug","createDebugLogger","debugData","extend","JSON","stringify","detect","createDetectionInterface","record","createSearchInterface","iterate","initialState","matches","candidateCount","nonMatches","duplicateCount","nonMatchCount","conversionFailures","matchErrors","records","failures","state","length","recordSetSize","failureSetSize","newCandidateCount","newConversionFailures","concat","handleRecordSet","queriesLeft","searchCounter","query","returnResult","stopReason","matchResult","iterateRecords","newDuplicateCount","newNonMatchCount","newMatches","newNonMatches","newMatchErrors","handleMatchResult","maxMatchesFound","maxCandidatesRetrieved","addQuery","map","match","matchQuery","conversionFailureCount","matchErrorCount","checkCounts","matchStatus","getMatchState","matchesResult","result","matchCount","chosenNonMatchCount","totalHandled","conversionFailuresCount","resultSetOffset","totalRecords","queryCandidateCounter","maxedQueries","searchesLeft","nonRetrieved","maxedQueriesStopReason","undefined","conversionFailuresStopReason","matchErrorsStopReason","newStopReason","status","recordMatches","recordNonMatches","recordCount","recordDuplicateCount","recordNonMatchCount","recordMatchErrors","candidate","newRecordCount","candidateNotInMatches","candidateRecord","id","candidateId","detectionResult","handleDetectionResult","error","matchError","payload","message","newRecordMatchErrors","slice","strategy","treshold","probability","strategyResult","newMatch","handleRecordMatch","newRecordNonMatchCount","isMatch","newRecordMatches","newRecordNonMatches","newCandidateId","find"],"sources":["../src/index.js"],"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//import inspect from 'util';\n\nexport {candidateSearch, matchDetection};\n\nexport default ({detection: detectionOptions, search: searchOptions, maxMatches = 1, maxCandidates = 25, returnStrategy = false, returnQuery = false, returnNonMatches = false, returnFailures = 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 debugData(`ReturnFailures: ${JSON.stringify(returnFailures)}`);\n\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, conversionFailures = [], matchErrors = []}) {\n debugData(`Starting next matcher iteration.`);\n const {records, failures, ...state} = await search(initialState);\n\n debugData(`Current state: ${JSON.stringify(state)}, matches: ${matches.length}, candidateCount: ${candidateCount}, nonMatches: ${nonMatches.length}, nonMatchCount: ${nonMatchCount}, conversionFailures: ${conversionFailures}, matchErrors: ${matchErrors.length}`);\n const recordSetSize = records.length;\n const failureSetSize = failures.length;\n const newCandidateCount = candidateCount + recordSetSize + failureSetSize;\n\n const newConversionFailures = conversionFailures.concat(failures);\n debugData(`Failures: ${failures.length}, ConversionFailures: ${conversionFailures.length}, NewConversionFailures: ${newConversionFailures.length}`);\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, nonMatchCount, duplicateCount, conversionFailures: newConversionFailures, matchErrors});\n }\n\n debug(`No (more) candidate records to check, no more queries left, matches: ${matches.length}`);\n return returnResult({matches, state, stopReason: '', nonMatches, nonMatchCount, candidateCount: newCandidateCount, duplicateCount, conversionFailures: newConversionFailures, matchErrors});\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, nonMatchCount});\n\n const newDuplicateCount = duplicateCount + matchResult.duplicateCount;\n const newNonMatchCount = nonMatchCount + matchResult.nonMatchCount;\n const {newMatches, newNonMatches, newMatchErrors} = handleMatchResult(matchResult, matches, nonMatches, matchErrors);\n\n if (maxMatchesFound({matches: newMatches, maxMatches})) {\n return returnResult({matches: newMatches, state, stopReason: 'maxMatches', nonMatches: newNonMatches, duplicateCount: newDuplicateCount, candidateCount: newCandidateCount, nonMatchCount: newNonMatchCount, conversionFailures: newConversionFailures, matchErrors: newMatchErrors});\n }\n\n if (maxCandidatesRetrieved(newCandidateCount, maxCandidates)) {\n return returnResult({matches: newMatches, state, stopReason: 'maxCandidates', nonMatches: newNonMatches, duplicateCount: newDuplicateCount, candidateCount: newCandidateCount, nonMatchCount: newNonMatchCount, conversionFailures: newConversionFailures, matchErrors: newMatchErrors});\n }\n\n return iterate({initialState: state, matches: newMatches, candidateCount: newCandidateCount, nonMatches: newNonMatches, duplicateCount: newDuplicateCount, nonMatchCount: newNonMatchCount, conversionFailures: newConversionFailures, matchErrors: newMatchErrors});\n }\n\n function handleMatchResult(matchResult, matches, nonMatches, matchErrors) {\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 const newMatchErrors = matchErrors.concat(matchResult.matchErrors);\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 debugData(`MatchResult: ${JSON.stringify(matchResult)}`);\n debugData(`Old matchErrors: ${JSON.stringify(matchErrors)}, matchErrors from matchResult: ${JSON.stringify(matchResult.matchErrors)}, New matchErrors: ${JSON.stringify(newMatchErrors)}`);\n\n debugData(`- Total amount of matchErrors: ${newMatchErrors.length}`);\n\n return {newMatches, newNonMatches, newMatchErrors};\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 // failures: array of conversionFailures from candidate-search and matchErrors from matchDetection in error format {status, payload: {message, id}} if returnFailures 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','conversionFailures', 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, conversionFailures, matchErrors}) {\n const conversionFailureCount = conversionFailures.length;\n const matchErrorCount = matchErrors.length;\n checkCounts({matches, nonMatches, candidateCount, duplicateCount, nonMatchCount, conversionFailureCount, matchErrorCount});\n const matchStatus = getMatchState(state, stopReason, conversionFailureCount, matchErrorCount);\n // add nonMatches to result only if returnNonMatches is 'true', otherwise nonMatches have not been gathered\n const matchesResult = returnNonMatches ? {matches, matchStatus, nonMatches} : {matches, matchStatus};\n const failures = [...conversionFailures, ...matchErrors];\n const result = returnFailures ? {...matchesResult, conversionFailures: failures} : matchesResult;\n debugData(`ReturnFailures ${returnFailures}`);\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, conversionFailureCount, matchErrorCount}) {\n const matchCount = matches.length;\n debugData(`Return nonMatches: ${returnNonMatches}`);\n const chosenNonMatchCount = returnNonMatches ? nonMatches.length : nonMatchCount;\n const totalHandled = matchCount + chosenNonMatchCount + duplicateCount;\n debug(`candidateCount: ${candidateCount}, matches: ${matchCount}, nonMatches: ${chosenNonMatchCount}, duplicateCount: ${duplicateCount}, conversionFailureCount: ${conversionFailureCount}, matchErrorCount: ${matchErrorCount}`);\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 // eslint-disable-next-line max-statements\n function getMatchState(state, stopReason, conversionFailuresCount, matchErrorCount) {\n debugData(`${JSON.stringify(state)}`);\n debug(`We had ${conversionFailuresCount} retrieved candidates that could not be converted.`);\n debug(`We had ${matchErrorCount} retrieved candidates that errored in matchDetection.`);\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 // matchStatus.stopReason: string ('maxMatches','maxCandidates','maxedQueries','conversionFailures', empty string/undefined), reason for stopping retrieving or handling the candidate records\n // 'maxMatches' and 'maxCandidates' are in stopReason, 'maxedQueries', 'conversionFailures' and 'matchErrors' are created here\n\n if (state.queriesLeft > 0 || nonRetrieved > 0 || state.maxedQueries.length > 0 || conversionFailureCount > 0 || matchErrorCount > 0) {\n const maxedQueriesStopReason = state.maxedQueries.length > 0 ? 'maxedQueries' : undefined;\n const conversionFailuresStopReason = conversionFailureCount > 0 ? 'conversionFailures' : undefined;\n const matchErrorsStopReason = matchErrorCount > 0 ? 'matchErrors' : undefined;\n const newStopReason = stopReason === '' || stopReason === undefined ? maxedQueriesStopReason || conversionFailuresStopReason || matchErrorsStopReason : stopReason;\n debugData(`MaxedQueriesStopReason: <${maxedQueriesStopReason}>`);\n debugData(`ConversionFailureStopReason <${conversionFailuresStopReason}>`);\n debugData(`MatchErrorsStopReason <${matchErrorsStopReason}>`);\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 // NOTES:\n // - we could optimize by creating the featureSet for the incoming record once and using it for all database/candidateRecords\n // - if creating the featureSet for the incoming record fails we have an unprocessable entity\n // - if creating the featureSet for a candidate record fails we could skip that candidate - but list the case as a detectionFailure, same as conversionFailures\n\n // eslint-disable-next-line max-statements\n function iterateRecords({records, recordSetSize, maxMatches, matches = [], nonMatches = [], recordMatches = [], recordNonMatches = [], recordCount = 0, recordDuplicateCount = 0, recordNonMatchCount = 0, recordMatchErrors = []}) {\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 // recordMatchErrors: errored matchDetection in the current record set\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 /* We could optimize and detect all retrieved candidates at once\n const candidateRecords = records.map(record => record.record);\n const recordsIsArray = Array.isArray(candidateRecords);\n debug(`records is an array: ${recordsIsArray}`);\n const result = detect(record, candidateRecords);\n debug(`${JSON.stringify(result)}`);\n */\n\n if (candidate) {\n\n // eslint-disable-next-line functional/no-conditional-statement\n if (candidateNotInMatches(matches.concat(nonMatches), candidate)) {\n const {record: candidateRecord, id: candidateId} = candidate;\n try {\n debug(`Running matchDetection for record ${candidateId} (${newRecordCount}/${recordSetSize})`);\n // we should handle errors from detection somehow - ie. cases where either record or candidateRecord errors\n const detectionResult = detect(record, candidateRecord);\n\n return handleDetectionResult(detectionResult, candidateId, candidateRecord);\n } catch (error) {\n debug(`MatchDetection errored: database record ${candidateId}: ${error}`);\n\n const matchError = {status: 422, payload: {message: `Matching errored for database record ${candidateId}. ${error.message}.`, id: candidateId}};\n const newRecordMatchErrors = recordMatchErrors.concat(matchError);\n return iterateRecords({records: records.slice(1), recordSetSize, maxMatches, matches, recordMatches, recordCount: newRecordCount, recordNonMatches, recordDuplicateCount, recordNonMatchCount, recordMatchErrors: newRecordMatchErrors});\n }\n }\n\n return iterateRecords({records: records.slice(1), recordSetSize, maxMatches, matches, recordMatches, recordCount: newRecordCount, recordNonMatches, recordDuplicateCount: recordDuplicateCount + 1, recordNonMatchCount, recordMatchErrors});\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, matchErrors: recordMatchErrors};\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, recordMatchErrors});\n }\n\n function handleRecordMatch(isMatch, newMatch) {\n const newRecordMatches = isMatch ? recordMatches.concat(newMatch) : recordMatches;\n const newRecordNonMatches = isMatch ? recordNonMatches : recordNonMatches.concat(newMatch);\n const newRecordNonMatchCount = isMatch ? recordNonMatchCount : recordNonMatchCount + 1;\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 debugData(`- Total nonMatchCount after this detection: ${recordNonMatchCount}`);\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: newRecordNonMatchCount, matchErrors: recordMatchErrors};\n }\n\n return iterateRecords({records: records.slice(1), recordSetSize, maxMatches, matches, recordMatches: newRecordMatches, recordCount: newRecordCount, recordNonMatches: returnNonMatches ? newRecordNonMatches : [], duplicateCount: recordDuplicateCount, recordNonMatchCount: newRecordNonMatchCount, matchErrors: recordMatchErrors});\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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