fix(elasticSearch): use more precise matching for query filters
Signed-off-by: Phil Kuang <pkuang@factset.com>
This commit is contained in:
@@ -0,0 +1,5 @@
|
||||
---
|
||||
'@backstage/plugin-search-backend-module-elasticsearch': patch
|
||||
---
|
||||
|
||||
Use more precise matching for query filters
|
||||
+19
-4
@@ -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',
|
||||
},
|
||||
},
|
||||
],
|
||||
|
||||
+3
-1
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user