feat: allow for searching illegal elasticsearch characters

This commit is contained in:
Wieland Schöbl
2020-02-18 09:16:44 +00:00
committed by Rainer Killinger
parent 1bad092185
commit b629d058eb
2 changed files with 51 additions and 9 deletions

View File

@@ -41,6 +41,20 @@ import {
ESTypeFilter,
} from './common';
/**
* Escapes any reserved character that would otherwise not be accepted by Elasticsearch
*
* Elasticsearch as the following reserved characters:
* + - = && || > < ! ( ) { } [ ] ^ " ~ * ? : \ /
* It is possible to use all, with the exception of < and >, of them by escaping them with a \
* https://www.elastic.co/guide/en/elasticsearch/reference/5.6/query-dsl-query-string-query.html
*
* @param str the string to escape the characters from
*/
function escapeESReservedCharacters(str: string): string {
return str.replace(/[+\-=!(){}\[\]^"~*?:\\/]|(&&)|(\|\|)/g, '\\$&');
}
/**
* Builds a boolean filter. Returns an elasticsearch boolean filter
*/
@@ -301,7 +315,7 @@ export function buildQuery(
analyzer: 'search_german',
default_field: 'name',
minimum_should_match: '90%',
query: (typeof params.query !== 'string') ? '*' : params.query,
query: (typeof params.query !== 'string') ? '*' : escapeESReservedCharacters(params.query),
},
};
} else if (elasticsearchConfig.query.queryType === 'query_string') {
@@ -310,7 +324,7 @@ export function buildQuery(
analyzer: 'search_german',
default_field: 'name',
minimum_should_match: elasticsearchConfig.query.minMatch,
query: (typeof params.query !== 'string') ? '*' : params.query,
query: (typeof params.query !== 'string') ? '*' : escapeESReservedCharacters(params.query),
},
};
} else if (elasticsearchConfig.query.queryType === 'dis_max') {
@@ -334,7 +348,7 @@ export function buildQuery(
analyzer: 'search_german',
default_field: 'name',
minimum_should_match: elasticsearchConfig.query.fuzziness,
query: (typeof params.query !== 'string') ? '*' : params.query,
query: (typeof params.query !== 'string') ? '*' : escapeESReservedCharacters(params.query),
},
},
],