mirror of
https://gitlab.com/openstapps/openstapps.git
synced 2026-01-20 16:42:56 +00:00
feat: allow for searching illegal elasticsearch characters
This commit is contained in:
committed by
Rainer Killinger
parent
1bad092185
commit
b629d058eb
@@ -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),
|
||||
},
|
||||
},
|
||||
],
|
||||
|
||||
Reference in New Issue
Block a user