fix(elasticSearch): use more precise matching for query filters

Signed-off-by: Phil Kuang <pkuang@factset.com>
This commit is contained in:
Phil Kuang
2022-05-25 16:55:56 -04:00
parent c1511c99a5
commit 281cec1b61
3 changed files with 27 additions and 5 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/plugin-search-backend-module-elasticsearch': patch
---
Use more precise matching for query filters
@@ -153,7 +153,7 @@ describe('ElasticSearchSearchEngine', () => {
},
filter: {
match: {
kind: 'testKind',
'kind.keyword': 'testKind',
},
},
},
@@ -204,7 +204,12 @@ describe('ElasticSearchSearchEngine', () => {
const actualTranslatedQuery = translatorUnderTest({
types: ['indexName'],
term: 'testTerm',
filters: { kind: 'testKind', namespace: 'testNameSpace' },
filters: {
kind: 'testKind',
namespace: 'testNameSpace',
foo: 123,
bar: true,
},
}) as ConcreteElasticSearchQuery;
expect(actualTranslatedQuery).toMatchObject({
@@ -228,12 +233,22 @@ describe('ElasticSearchSearchEngine', () => {
filter: [
{
match: {
kind: 'testKind',
'kind.keyword': 'testKind',
},
},
{
match: {
namespace: 'testNameSpace',
'namespace.keyword': 'testNameSpace',
},
},
{
match: {
foo: '123',
},
},
{
match: {
bar: 'true',
},
},
],
@@ -168,7 +168,9 @@ export class ElasticSearchSearchEngine implements SearchEngine {
.filter(([_, value]) => Boolean(value))
.map(([key, value]: [key: string, value: any]) => {
if (['string', 'number', 'boolean'].includes(typeof value)) {
return esb.matchQuery(key, value.toString());
// Use exact matching for string datatype fields
const keyword = typeof value === 'string' ? `${key}.keyword` : key;
return esb.matchQuery(keyword, value.toString());
}
if (Array.isArray(value)) {
return esb