Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

Expand Down
13 changes: 11 additions & 2 deletions lib/convert.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
}


Expand Down
12 changes: 6 additions & 6 deletions test/unit/convert.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -1457,17 +1457,17 @@ 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;
assert.isArray(filter.bool.must);
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
);
});
Expand Down