From 6afd041ace0d741701769fdda1b9d1550dd3cd9a Mon Sep 17 00:00:00 2001 From: HeeZJee Date: Fri, 2 Jul 2021 01:24:00 +0500 Subject: [PATCH 1/6] special character filter term issue fix --- lib/buildFilter.js | 2 +- lib/buildNestedQueries.js | 8 ++++---- lib/buildWhere.js | 2 +- lib/create.js | 2 +- lib/replaceById.js | 2 +- lib/replaceOrCreate.js | 2 +- lib/save.js | 2 +- lib/updateOrCreate.js | 2 +- 8 files changed, 11 insertions(+), 11 deletions(-) diff --git a/lib/buildFilter.js b/lib/buildFilter.js index fb8f29f..9eb4b9b 100644 --- a/lib/buildFilter.js +++ b/lib/buildFilter.js @@ -76,7 +76,7 @@ function buildFilter(modelName, idName, criteria = {}, size = null, offset = nul }, filter: [{ term: { - docType: modelName + 'docType.keyword': model } }] } diff --git a/lib/buildNestedQueries.js b/lib/buildNestedQueries.js index 881607b..f2f74ae 100644 --- a/lib/buildNestedQueries.js +++ b/lib/buildNestedQueries.js @@ -16,7 +16,7 @@ function buildNestedQueries(body, model, idName, where, nestedFields) { }, filter: [{ term: { - docType: model + 'docType.keyword': model } }] } @@ -28,16 +28,16 @@ function buildNestedQueries(body, model, idName, where, nestedFields) { const rootPath = body.query; self.buildDeepNestedQueries(true, idName, where, body, rootPath, model, nestedFields); - const docTypeQuery = _.find(rootPath.bool.filter, (v) => v.term && v.term.docType); + const docTypeQuery = _.find(rootPath.bool.filter, (v) => v.term && v.term['docType.keyword']); let addedDocTypeToRootPath = false; if (typeof docTypeQuery !== 'undefined') { addedDocTypeToRootPath = true; - docTypeQuery.term.docType = model; + docTypeQuery.term['docType.keyword'] = model; } else { addedDocTypeToRootPath = true; rootPath.bool.filter.push({ term: { - docType: model + 'docType.keyword': model } }); } diff --git a/lib/buildWhere.js b/lib/buildWhere.js index 16609e3..4ac5cc7 100644 --- a/lib/buildWhere.js +++ b/lib/buildWhere.js @@ -48,7 +48,7 @@ function buildWhere(model, idName, where) { }, filter: [{ term: { - docType: model + 'docType.keyword': model } }] } diff --git a/lib/create.js b/lib/create.js index 3b06c74..125a52a 100644 --- a/lib/create.js +++ b/lib/create.js @@ -28,7 +28,7 @@ function create(model, data, done) { if (!document[self.idField]) { method = 'index'; // if there is no/empty id field, we must use the index method to create it (API 5.0) } - document.body.docType = model; + document.body['docType.keyword']= model; if (document.body[SEARCHAFTERKEY] || document.body[TOTALCOUNTKEY]) { document.body[SEARCHAFTERKEY] = undefined; document.body[TOTALCOUNTKEY] = undefined; diff --git a/lib/replaceById.js b/lib/replaceById.js index 30e3a43..e75ebad 100644 --- a/lib/replaceById.js +++ b/lib/replaceById.js @@ -20,7 +20,7 @@ function replaceById(modelName, id, data, options, callback) { document[self.idField] = self.getDocumentId(id); document.body = {}; _.assign(document.body, data); - document.body.docType = modelName; + document.body['docType.keyword'] = modelName; if (Object.prototype.hasOwnProperty.call(modelProperties, idName)) { document.body[idName] = id; } diff --git a/lib/replaceOrCreate.js b/lib/replaceOrCreate.js index 0e4e06a..76dd670 100644 --- a/lib/replaceOrCreate.js +++ b/lib/replaceOrCreate.js @@ -18,7 +18,7 @@ function replaceOrCreate(modelName, data, callback) { document[self.idField] = id; document.body = {}; _.assign(document.body, data); - document.body.docType = modelName; + document.body['docType.keyword'] = modelName; if (document.body[SEARCHAFTERKEY] || document.body[TOTALCOUNTKEY]) { document.body[SEARCHAFTERKEY] = undefined; document.body[TOTALCOUNTKEY] = undefined; diff --git a/lib/save.js b/lib/save.js index 7a64d2b..fdd22d4 100644 --- a/lib/save.js +++ b/lib/save.js @@ -18,7 +18,7 @@ function save(model, data, done) { if (id === undefined || id === null) { return done('Document id not setted!', null); } - data.docType = model; + data['docType.keyword'] = model; if (data[SEARCHAFTERKEY] || data[TOTALCOUNTKEY]) { data[SEARCHAFTERKEY] = undefined; data[TOTALCOUNTKEY] = undefined; diff --git a/lib/updateOrCreate.js b/lib/updateOrCreate.js index cfabd1c..07a875a 100644 --- a/lib/updateOrCreate.js +++ b/lib/updateOrCreate.js @@ -15,7 +15,7 @@ function updateOrCreate(modelName, data, callback) { } const defaults = self.addDefaults(modelName, 'updateOrCreate'); - data.docType = modelName; + data['docType.keyword'] = modelName; if (data[SEARCHAFTERKEY] || data[TOTALCOUNTKEY]) { data[SEARCHAFTERKEY] = undefined; data[TOTALCOUNTKEY] = undefined; From 26350ed3fb2cf7a009149856d197c6fac53d2e69 Mon Sep 17 00:00:00 2001 From: HeeZJee Date: Fri, 2 Jul 2021 01:49:50 +0500 Subject: [PATCH 2/6] fixed filter item query issue for speacial characters --- lib/buildNestedQueries.js | 4 ++-- lib/create.js | 2 +- lib/replaceById.js | 2 +- lib/save.js | 2 +- lib/setupIndex.js | 2 +- lib/updateOrCreate.js | 2 +- 6 files changed, 7 insertions(+), 7 deletions(-) diff --git a/lib/buildNestedQueries.js b/lib/buildNestedQueries.js index f2f74ae..ede19ea 100644 --- a/lib/buildNestedQueries.js +++ b/lib/buildNestedQueries.js @@ -28,11 +28,11 @@ function buildNestedQueries(body, model, idName, where, nestedFields) { const rootPath = body.query; self.buildDeepNestedQueries(true, idName, where, body, rootPath, model, nestedFields); - const docTypeQuery = _.find(rootPath.bool.filter, (v) => v.term && v.term['docType.keyword']); + const docTypeQuery = _.find(rootPath.bool.filter, (v) => v.term && v.term.docType); let addedDocTypeToRootPath = false; if (typeof docTypeQuery !== 'undefined') { addedDocTypeToRootPath = true; - docTypeQuery.term['docType.keyword'] = model; + docTypeQuery.term.docType = model; } else { addedDocTypeToRootPath = true; rootPath.bool.filter.push({ diff --git a/lib/create.js b/lib/create.js index 125a52a..3b06c74 100644 --- a/lib/create.js +++ b/lib/create.js @@ -28,7 +28,7 @@ function create(model, data, done) { if (!document[self.idField]) { method = 'index'; // if there is no/empty id field, we must use the index method to create it (API 5.0) } - document.body['docType.keyword']= model; + document.body.docType = model; if (document.body[SEARCHAFTERKEY] || document.body[TOTALCOUNTKEY]) { document.body[SEARCHAFTERKEY] = undefined; document.body[TOTALCOUNTKEY] = undefined; diff --git a/lib/replaceById.js b/lib/replaceById.js index e75ebad..30e3a43 100644 --- a/lib/replaceById.js +++ b/lib/replaceById.js @@ -20,7 +20,7 @@ function replaceById(modelName, id, data, options, callback) { document[self.idField] = self.getDocumentId(id); document.body = {}; _.assign(document.body, data); - document.body['docType.keyword'] = modelName; + document.body.docType = modelName; if (Object.prototype.hasOwnProperty.call(modelProperties, idName)) { document.body[idName] = id; } diff --git a/lib/save.js b/lib/save.js index fdd22d4..7a64d2b 100644 --- a/lib/save.js +++ b/lib/save.js @@ -18,7 +18,7 @@ function save(model, data, done) { if (id === undefined || id === null) { return done('Document id not setted!', null); } - data['docType.keyword'] = model; + data.docType = model; if (data[SEARCHAFTERKEY] || data[TOTALCOUNTKEY]) { data[SEARCHAFTERKEY] = undefined; data[TOTALCOUNTKEY] = undefined; diff --git a/lib/setupIndex.js b/lib/setupIndex.js index 0c0673e..fd25483 100644 --- a/lib/setupIndex.js +++ b/lib/setupIndex.js @@ -22,7 +22,7 @@ async function setupIndex() { const mapping = { properties: mappingProperties }; - if (!exists) { + if (!exists) { log('ESConnector.prototype.setupIndex', 'create index with mapping for', index); await db.indices.create({ index, diff --git a/lib/updateOrCreate.js b/lib/updateOrCreate.js index 07a875a..cfabd1c 100644 --- a/lib/updateOrCreate.js +++ b/lib/updateOrCreate.js @@ -15,7 +15,7 @@ function updateOrCreate(modelName, data, callback) { } const defaults = self.addDefaults(modelName, 'updateOrCreate'); - data['docType.keyword'] = modelName; + data.docType = modelName; if (data[SEARCHAFTERKEY] || data[TOTALCOUNTKEY]) { data[SEARCHAFTERKEY] = undefined; data[TOTALCOUNTKEY] = undefined; From 2969f798b9e1ec9e5ad0845f2cbd8630ea60a1c9 Mon Sep 17 00:00:00 2001 From: HeeZJee Date: Fri, 2 Jul 2021 02:01:46 +0500 Subject: [PATCH 3/6] fixed filter item query issue for speacial characters --- lib/replaceOrCreate.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/replaceOrCreate.js b/lib/replaceOrCreate.js index 76dd670..0e4e06a 100644 --- a/lib/replaceOrCreate.js +++ b/lib/replaceOrCreate.js @@ -18,7 +18,7 @@ function replaceOrCreate(modelName, data, callback) { document[self.idField] = id; document.body = {}; _.assign(document.body, data); - document.body['docType.keyword'] = modelName; + document.body.docType = modelName; if (document.body[SEARCHAFTERKEY] || document.body[TOTALCOUNTKEY]) { document.body[SEARCHAFTERKEY] = undefined; document.body[TOTALCOUNTKEY] = undefined; From 08599c8b61e86b336939e115f748d52994b9b396 Mon Sep 17 00:00:00 2001 From: ArsalImam <53432361+ArsalImam@users.noreply.github.com> Date: Fri, 2 Jul 2021 17:49:27 +0500 Subject: [PATCH 4/6] Update setupIndex.js Redundant indentation removed --- lib/setupIndex.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/setupIndex.js b/lib/setupIndex.js index fd25483..0c0673e 100644 --- a/lib/setupIndex.js +++ b/lib/setupIndex.js @@ -22,7 +22,7 @@ async function setupIndex() { const mapping = { properties: mappingProperties }; - if (!exists) { + if (!exists) { log('ESConnector.prototype.setupIndex', 'create index with mapping for', index); await db.indices.create({ index, From df5bd6779254637e4fd0915fa360e0e18b1a40e7 Mon Sep 17 00:00:00 2001 From: ArsalImam Date: Fri, 2 Jul 2021 17:52:26 +0500 Subject: [PATCH 5/6] tests updated --- README.md | 2 +- test/es-v6/01.filters.test.js | 22 +++++++++++----------- test/es-v7/01.filters.test.js | 22 +++++++++++----------- 3 files changed, 23 insertions(+), 23 deletions(-) diff --git a/README.md b/README.md index 044daf6..03407b3 100644 --- a/README.md +++ b/README.md @@ -56,7 +56,7 @@ npm install loopback-connector-esv6 --save --save-exact - **This connector will only connect to one index per datasource.** - This package is created to support ElasticSearch v6.x and 7.x only. -- `docType` property is automatically added in mapping properties which is required to differentiate documents stored in index with loopback model data. It stores loopback modelName value. `docType: { type: "keyword", index: true }` +- `docType` property is automatically added in mapping properties which is required to differentiate documents stored in index with loopback model data. It stores loopback modelName value. `'doctype.keyword': { type: "keyword", index: true }` ### Required diff --git a/test/es-v6/01.filters.test.js b/test/es-v6/01.filters.test.js index d13dfbc..10d2938 100644 --- a/test/es-v6/01.filters.test.js +++ b/test/es-v6/01.filters.test.js @@ -65,7 +65,7 @@ describe('Connector', function() { } }, { match: { - docType: modelName + 'doctype.keyword': modelName } }] } @@ -175,7 +175,7 @@ describe('Connector', function() { }, { match: { - docType: modelName + 'doctype.keyword': modelName } } ] @@ -240,7 +240,7 @@ describe('Connector', function() { }, { match: { - docType: modelName + 'doctype.keyword': modelName } } ] @@ -290,7 +290,7 @@ describe('Connector', function() { bool: { must: [{ match: { - docType: modelName + 'doctype.keyword': modelName } }, { bool: { @@ -391,7 +391,7 @@ describe('Connector', function() { }, { match: { - docType: modelName + 'doctype.keyword': modelName } } ] @@ -440,7 +440,7 @@ describe('Connector', function() { bool: { must: [{ match: { - docType: modelName + 'doctype.keyword': modelName } }, { bool: { @@ -519,7 +519,7 @@ describe('Connector', function() { }, { match: { - docType: modelName + 'doctype.keyword': modelName } } ] @@ -575,7 +575,7 @@ describe('Connector', function() { } }, { match: { - docType: modelName + 'doctype.keyword': modelName } }] } @@ -636,7 +636,7 @@ describe('Connector', function() { } }, { match: { - docType: modelName + 'doctype.keyword': modelName } } ] @@ -693,7 +693,7 @@ describe('Connector', function() { } }, { match: { - docType: modelName + 'doctype.keyword': modelName } } ] @@ -766,7 +766,7 @@ describe('Connector', function() { } }, { match: { - docType: modelName + 'doctype.keyword': modelName } } ] diff --git a/test/es-v7/01.filters.test.js b/test/es-v7/01.filters.test.js index d13dfbc..10d2938 100644 --- a/test/es-v7/01.filters.test.js +++ b/test/es-v7/01.filters.test.js @@ -65,7 +65,7 @@ describe('Connector', function() { } }, { match: { - docType: modelName + 'doctype.keyword': modelName } }] } @@ -175,7 +175,7 @@ describe('Connector', function() { }, { match: { - docType: modelName + 'doctype.keyword': modelName } } ] @@ -240,7 +240,7 @@ describe('Connector', function() { }, { match: { - docType: modelName + 'doctype.keyword': modelName } } ] @@ -290,7 +290,7 @@ describe('Connector', function() { bool: { must: [{ match: { - docType: modelName + 'doctype.keyword': modelName } }, { bool: { @@ -391,7 +391,7 @@ describe('Connector', function() { }, { match: { - docType: modelName + 'doctype.keyword': modelName } } ] @@ -440,7 +440,7 @@ describe('Connector', function() { bool: { must: [{ match: { - docType: modelName + 'doctype.keyword': modelName } }, { bool: { @@ -519,7 +519,7 @@ describe('Connector', function() { }, { match: { - docType: modelName + 'doctype.keyword': modelName } } ] @@ -575,7 +575,7 @@ describe('Connector', function() { } }, { match: { - docType: modelName + 'doctype.keyword': modelName } }] } @@ -636,7 +636,7 @@ describe('Connector', function() { } }, { match: { - docType: modelName + 'doctype.keyword': modelName } } ] @@ -693,7 +693,7 @@ describe('Connector', function() { } }, { match: { - docType: modelName + 'doctype.keyword': modelName } } ] @@ -766,7 +766,7 @@ describe('Connector', function() { } }, { match: { - docType: modelName + 'doctype.keyword': modelName } } ] From 9781750afcceb6dad4940aa8543b66bc6d1a7030 Mon Sep 17 00:00:00 2001 From: HeeZJee Date: Fri, 2 Jul 2021 22:14:50 +0500 Subject: [PATCH 6/6] buildFilter model name fixed --- lib/buildFilter.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/buildFilter.js b/lib/buildFilter.js index 9eb4b9b..f0b9176 100644 --- a/lib/buildFilter.js +++ b/lib/buildFilter.js @@ -76,7 +76,7 @@ function buildFilter(modelName, idName, criteria = {}, size = null, offset = nul }, filter: [{ term: { - 'docType.keyword': model + 'docType.keyword': modelName } }] }