Skip to content

Commit

Permalink
Merge 7f6e894 into e2c7c13
Browse files Browse the repository at this point in the history
  • Loading branch information
Nataniel López committed Dec 2, 2019
2 parents e2c7c13 + 7f6e894 commit 4c01348
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 24 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Fixed
- `ElasticSearchFilters` field datatypes will be filtered by `raw` only when exists in the model `sortableFields` otherwise will use `keyword` instead.

## [1.1.0] - 2019-09-12
### Added
- `ElasticSearchConfigError`
Expand Down
33 changes: 28 additions & 5 deletions lib/elasticsearch-filters.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,39 @@ const ElasticSearchError = require('./elasticsearch-error');
*/
class ElasticSearchFilters {

static get model() {
return this._model;
}

static set model(model) {
this._model = model;
}

/**
* Search the received field into the model and returns the respective ES fieldType
* @param {String} field the field name
* @returns {String} ES fieldType
*/
static _getFieldType(field) {

if(this.model.constructor.sortableFields[field])
return 'raw';

return 'keyword';
}

/**
* Get the elasticsearch filters query from the received filters
* @param {Object} filters filters
* @returns {Object} elasticsearch filters
*/
static getFilters(filters) {
static getFilters(model, filters) {

if(!filters || typeof filters !== 'object' || Array.isArray(filters))
throw new ElasticSearchError('Invalid filters', ElasticSearchError.codes.INVALID_FILTERS);

this.model = model;

filters = this._prepareFilters(filters);

const parsedFilters = {};
Expand Down Expand Up @@ -70,8 +93,8 @@ class ElasticSearchFilters {
static _formatByOperator(parsedFilters, operator, fields) {

const operators = {
eq: this._formatEq,
ne: this._formatNe,
eq: this._formatEq.bind(this),
ne: this._formatNe.bind(this),
in: this._formatIn,
nin: this._formatNin,
gt: this._formatGt,
Expand Down Expand Up @@ -99,7 +122,7 @@ class ElasticSearchFilters {

parsedFilters.bool = parsedFilters.bool || {};
parsedFilters.bool.must = parsedFilters.bool.must || [];
parsedFilters.bool.must.push({ term: { [`${field}.raw`]: value } });
parsedFilters.bool.must.push({ term: { [`${field}.${this._getFieldType(field)}`]: value } });
}

/**
Expand All @@ -112,7 +135,7 @@ class ElasticSearchFilters {

parsedFilters.bool = parsedFilters.bool || {};
parsedFilters.bool.must_not = parsedFilters.bool.must_not || [];
parsedFilters.bool.must_not.push({ term: { [`${field}.raw`]: value } });
parsedFilters.bool.must_not.push({ term: { [`${field}.${this._getFieldType(field)}`]: value } });
}

/**
Expand Down
6 changes: 3 additions & 3 deletions lib/elasticsearch.js
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,7 @@ class ElasticSearch {

const limit = params.limit || this.config.limit;
const page = params.page || 1;
const filters = params.filters ? ElasticSearchFilters.getFilters(params.filters) : {};
const filters = params.filters ? ElasticSearchFilters.getFilters(model, params.filters) : {};
const order = params.order ? this._parseSortingParams(params.order) : {};

try {
Expand Down Expand Up @@ -526,7 +526,7 @@ class ElasticSearch {
refresh: true,
type: '_doc',
body: {
...ElasticSearchFilters.getFilters(filters),
...ElasticSearchFilters.getFilters(model, filters),
...this._parseValuesForUpdate(values)
}
});
Expand Down Expand Up @@ -614,7 +614,7 @@ class ElasticSearch {
type: '_doc',
refresh: true,
body: {
...ElasticSearchFilters.getFilters(filters)
...ElasticSearchFilters.getFilters(model, filters)
}
});

Expand Down
49 changes: 33 additions & 16 deletions tests/elasticsearch-filters-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,33 @@ const ElasticSearchFilters = require('./../lib/elasticsearch-filters');

const ElasticSearchError = require('./../lib/elasticsearch-error');

class Model {

static get table() {
return 'myTable';
}

static get sortableFields() {
return {
id: true,
value: true,
othervalue: { type: 'integer' }
};
}
}

const model = new Model();

describe('ElasticSearchFilters', () => {

describe('getFilters()', () => {

it('should return the elasticsearch query (term only)', () => {

const termFilters = {
field: 'value',
id: 'value',
$eq: {
field: 'value'
id: 'value'
},
$ne: {
field: 'value'
Expand All @@ -28,14 +45,14 @@ describe('ElasticSearchFilters', () => {
}
};

assert.deepStrictEqual(ElasticSearchFilters.getFilters(termFilters), {
assert.deepStrictEqual(ElasticSearchFilters.getFilters(model, termFilters), {
query: {
bool: {
must: [
{ term: { 'field.raw': 'value' } }, { terms: { field: ['value'] } }
{ term: { 'id.raw': 'value' } }, { terms: { field: ['value'] } }
],
must_not: [
{ term: { 'field.raw': 'value' } }, { terms: { field: ['value'] } }
{ term: { 'field.keyword': 'value' } }, { terms: { field: ['value'] } }
]
}
}
Expand All @@ -59,7 +76,7 @@ describe('ElasticSearchFilters', () => {
}
};

assert.deepStrictEqual(ElasticSearchFilters.getFilters(rangeFilters), {
assert.deepStrictEqual(ElasticSearchFilters.getFilters(model, rangeFilters), {
query: {
range: {
id: {
Expand Down Expand Up @@ -96,7 +113,7 @@ describe('ElasticSearchFilters', () => {
}
};

assert.deepStrictEqual(ElasticSearchFilters.getFilters(mixedFilters), {
assert.deepStrictEqual(ElasticSearchFilters.getFilters(model, mixedFilters), {
query: {
bool: {
must: [
Expand All @@ -112,7 +129,7 @@ describe('ElasticSearchFilters', () => {
}
],
must_not: [
{ term: { 'field.raw': 'value' } }, { terms: { field: ['value'] } }
{ term: { 'field.keyword': 'value' } }, { terms: { field: ['value'] } }
]
}
}
Expand All @@ -121,7 +138,7 @@ describe('ElasticSearchFilters', () => {

it('should throw \'Invalid filters\' when the $in terms values aren\'t an array', async () => {

assert.throws(() => ElasticSearchFilters.getFilters({
assert.throws(() => ElasticSearchFilters.getFilters(model, {
$in: 'not-array'
}), {
name: 'ElasticSearchError',
Expand All @@ -132,7 +149,7 @@ describe('ElasticSearchFilters', () => {

it('should throw \'Invalid filters\' when the $nin terms values aren\'t an array', async () => {

assert.throws(() => ElasticSearchFilters.getFilters({
assert.throws(() => ElasticSearchFilters.getFilters(model, {
$nin: 'not-array'
}), {
name: 'ElasticSearchError',
Expand All @@ -145,7 +162,7 @@ describe('ElasticSearchFilters', () => {

it('should throw \'Invalid filter operator\' when any of the received operator not exists', async () => {

assert.throws(() => ElasticSearchFilters.getFilters({
assert.throws(() => ElasticSearchFilters.getFilters(model, {
$eq: { field: 'value' },
$eqq: { field: 'value' }
}), {
Expand All @@ -156,21 +173,21 @@ describe('ElasticSearchFilters', () => {
});

it('should throw \'Invalid filters\' when the filters not exists', async () => {
assert.throws(() => ElasticSearchFilters.getFilters(), {
assert.throws(() => ElasticSearchFilters.getFilters(model), {
name: 'ElasticSearchError',
code: ElasticSearchError.codes.INVALID_FILTERS
});
});

it('should throw \'Invalid filters\' when the filters is not an object', async () => {
assert.throws(() => ElasticSearchFilters.getFilters('filters'), {
assert.throws(() => ElasticSearchFilters.getFilters(model, 'filters'), {
name: 'ElasticSearchError',
code: ElasticSearchError.codes.INVALID_FILTERS
});
});

it('should throw \'Invalid filters\' when the filters is an array', async () => {
assert.throws(() => ElasticSearchFilters.getFilters(['filters']), {
assert.throws(() => ElasticSearchFilters.getFilters(model, ['filters']), {
name: 'ElasticSearchError',
code: ElasticSearchError.codes.INVALID_FILTERS
});
Expand All @@ -182,7 +199,7 @@ describe('ElasticSearchFilters', () => {
/* eslint-disable no-underscore-dangle */

[
ElasticSearchFilters._formatEq,
ElasticSearchFilters._formatEq.bind(ElasticSearchFilters),
ElasticSearchFilters._formatIn

].forEach(formatter => {
Expand All @@ -207,7 +224,7 @@ describe('ElasticSearchFilters', () => {
});

[
ElasticSearchFilters._formatNe,
ElasticSearchFilters._formatNe.bind(ElasticSearchFilters),
ElasticSearchFilters._formatNin

].forEach(formatter => {
Expand Down

0 comments on commit 4c01348

Please sign in to comment.