From 9ed6d43ff2e571363ce967a415a9ed22d16daba8 Mon Sep 17 00:00:00 2001 From: "C.Jay Martin" Date: Mon, 8 Jan 2018 21:53:45 -0500 Subject: [PATCH 1/2] Modified _like (and implicitly _nlike) to match Elastic Search's format --- lib/convert.js | 13 +++++++++++-- test/unit/convert.test.js | 12 ++++++------ 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/lib/convert.js b/lib/convert.js index 62bd0cf..fb912fc 100644 --- a/lib/convert.js +++ b/lib/convert.js @@ -242,12 +242,21 @@ class Builder { throw new errors.ConvertError(); } - const regex = like.toRegexString(); + let regex = like.toRegexString(); + // LIKE clauses in elastic do NOT + if (regex.startsWith('^')) { + regex = regex.slice(1); + } + if (regex.endsWith('$')) { + regex = regex.slice(0, -1); + } + + const svo = {}; svo[this._target(clause.subject)] = regex; - return { regex: svo }; + return { regexp: svo }; } diff --git a/test/unit/convert.test.js b/test/unit/convert.test.js index 84bbe76..a09a88c 100644 --- a/test/unit/convert.test.js +++ b/test/unit/convert.test.js @@ -1427,15 +1427,15 @@ describe('#convert', function() { }); it('adds regex for target like string', function() { - const regex = '^.*Hello World.{1}$'; + const regex = '.*Hello World.{1}'; const parseResult = spleen.parse('/foo/bar like "*Hello World_"'); const { value } = parseResult; const result = convert(value); const { filter } = result.value; assert.isArray(filter.bool.must); assert.lengthOf(filter.bool.must, 1); - assert.isObject(filter.bool.must[0].regex); - assert.strictEqual(filter.bool.must[0].regex['foo.bar'], regex); + assert.isObject(filter.bool.must[0].regexp); + assert.strictEqual(filter.bool.must[0].regexp['foo.bar'], regex); }); it('throws if like object not Like', function() { @@ -1457,7 +1457,7 @@ describe('#convert', function() { }); it('adds must_not regex for target nlike string', function() { - const regex = '^.*Hello World.{1}$'; + const regex = '.*Hello World.{1}'; const { value } = spleen.parse('/foo/bar nlike "*Hello World_"'); const result = convert(value); const { filter } = result.value; @@ -1465,9 +1465,9 @@ describe('#convert', function() { assert.lengthOf(filter.bool.must, 1); assert.isObject(filter.bool.must[0].bool); assert.isObject(filter.bool.must[0].bool.must_not); - assert.isObject(filter.bool.must[0].bool.must_not.regex); + assert.isObject(filter.bool.must[0].bool.must_not.regexp); assert.strictEqual( - filter.bool.must[0].bool.must_not.regex['foo.bar'], + filter.bool.must[0].bool.must_not.regexp['foo.bar'], regex ); }); From f31ce3de58ac9304a912673ba17de29f0e07bc35 Mon Sep 17 00:00:00 2001 From: "C.Jay Martin" Date: Mon, 8 Jan 2018 22:17:32 -0500 Subject: [PATCH 2/2] Missed committing my README.md piece --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index f6ae01c..878b289 100644 --- a/README.md +++ b/README.md @@ -149,8 +149,8 @@ Under the hood, Elasticsearch is utilizing the [Apache Lucene](https://lucene.ap | `nbetween` | `{ "bool": { "must_not": { "range": { "key": { "gte": "value1", "lte": "value2" } } } } }` | | `in` | `{ "terms": { "key": ["value1", "value2", "valueN"] } }` | | `nin` | `{ "bool": { "must_not": { "terms": { "key": ["value1", "value2", "valueN"] } } } }` | -| `like` | `{ "regex": { "key": "like value converted to regex" } }` | -| `nlike` | `{ "bool": { "must_not": { "regex": { "key": "like value converted to regex" } } } }` | +| `like` | `{ "regexp": { "key": "like value converted to regex" } }` | +| `nlike` | `{ "bool": { "must_not": { "regexp": { "key": "like value converted to regex" } } } }` | ### Pattern Matching Conversion to Regex @@ -161,7 +161,7 @@ Elasticsearch can perform pattern matching usig regular expressions. The `splee | `*` | `.*` | | `_` | `.{1}` | -All `like` statements converted to regex begin with `^` and `$`. For example, the `like` pattern `*Hello World_` is converted into the regex `^.*Hello World.{1}$`. +All `like` statements converted to regex begin with `^` and `$`. For example, the `like` pattern `*Hello World_` is converted into the regex `.*Hello World.{1}`. ### Range Comparisons