Skip to content

Commit

Permalink
feat(es): use simpler logic for generating a query string from filters
Browse files Browse the repository at this point in the history
  • Loading branch information
jahow committed Oct 5, 2023
1 parent e92361e commit eb627f1
Show file tree
Hide file tree
Showing 2 changed files with 155 additions and 30 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ElasticsearchService } from './elasticsearch.service'
import { ES_FIXTURE_AGGS_RESPONSE } from '@geonetwork-ui/common/fixtures'
import { EsSearchParams } from '../types/elasticsearch.model'
import { EsSearchParams } from '@geonetwork-ui/api/metadata-converter'

describe('ElasticsearchService', () => {
let service: ElasticsearchService
Expand Down Expand Up @@ -96,16 +96,15 @@ describe('ElasticsearchService', () => {
})
})
describe('#buildPayloadQuery', () => {
it('add any and other fields query_strings', () => {
it('should not add fields query_strings if fieldsSearchFilters Object is empty', () => {
const query = service['buildPayloadQuery'](
{
Org: {
world: true,
},
any: 'hello',
},
{}
{},
['record-1', 'record-2', 'record-3']
)

expect(query).toEqual({
bool: {
filter: [],
Expand All @@ -131,8 +130,8 @@ describe('ElasticsearchService', () => {
},
},
{
query_string: {
query: '(Org:"world")',
ids: {
values: ['record-1', 'record-2', 'record-3'],
},
},
],
Expand Down Expand Up @@ -181,7 +180,7 @@ describe('ElasticsearchService', () => {
},
{
query_string: {
query: '(Org:"world")',
query: 'Org:("world")',
},
},
{
Expand All @@ -203,6 +202,10 @@ describe('ElasticsearchService', () => {
{
Org: {
world: true,
world2: true,
},
name: {
john: true,
},
any: 'hello',
},
Expand Down Expand Up @@ -235,7 +238,100 @@ describe('ElasticsearchService', () => {
},
{
query_string: {
query: '(Org:"world")',
query: 'Org:("world" OR "world2") AND name:("john")',
},
},
{
ids: {
values: [],
},
},
],
must_not: {
terms: {
resourceType: ['service', 'map', 'map/static', 'mapDigital'],
},
},
},
})
})
it('handle negative and empty filters', () => {
const query = service['buildPayloadQuery'](
{
Org: {
world: false,
},
name: {},
message: '',
},
{},
[]
)
expect(query).toEqual({
bool: {
filter: [],
should: [],
must: [
{
terms: {
isTemplate: ['n'],
},
},
{
query_string: {
query: 'Org:(-"world")',
},
},
{
ids: {
values: [],
},
},
],
must_not: {
terms: {
resourceType: ['service', 'map', 'map/static', 'mapDigital'],
},
},
},
})
})
it('handle filters expressed as queries', () => {
const query = service['buildPayloadQuery'](
{
Org: 'world AND world2',
any: 'hello',
},
{},
[]
)
expect(query).toEqual({
bool: {
filter: [],
should: [],
must: [
{
terms: {
isTemplate: ['n'],
},
},
{
query_string: {
default_operator: 'AND',
fields: [
'resourceTitleObject.langfre^5',
'tag.langfre^4',
'resourceAbstractObject.langfre^3',
'lineageObject.langfre^2',
'any.langfre',
'uuid',
],
query: 'hello',
},
},
{
query_string: {
query: 'Org:(world AND world2)',
},
},
{
Expand Down Expand Up @@ -287,9 +383,7 @@ describe('ElasticsearchService', () => {
it('adds boosting of 7 for intersecting with it and boosting of 10 on geoms within', () => {
const query = service['buildPayloadQuery'](
{
Org: {
world: true,
},
Org: 'world',
any: 'hello',
},
{},
Expand Down Expand Up @@ -321,7 +415,7 @@ describe('ElasticsearchService', () => {
},
{
query_string: {
query: '(Org:"world")',
query: 'Org:(world)',
},
},
],
Expand Down Expand Up @@ -611,6 +705,7 @@ describe('ElasticsearchService', () => {
filters: {
filter1: { field1: '100' },
filter2: { field2: { value1: true, value3: true } },
filter3: 'my own query',
},
},
myHistogram: {
Expand All @@ -623,14 +718,13 @@ describe('ElasticsearchService', () => {
myFilters: {
filters: {
filter1: {
match: {
field1: '100',
},
query_string: { query: 'field1:(100)' },
},
filter2: {
match: {
field2: { value1: true, value3: true },
},
query_string: { query: 'field2:("value1" OR "value3")' },
},
filter3: {
query_string: { query: 'my own query' },
},
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import {
Aggregation,
AggregationParams,
AggregationsParams,
FieldFilter,
FieldFilters,
FilterAggregationParams,
SortByField,
} from '@geonetwork-ui/common/domain/search'
Expand Down Expand Up @@ -177,13 +179,35 @@ export class ElasticsearchService {
})
}

private filtersToQueryString(filters: FieldFilters): string {
const makeQuery = (filter: FieldFilter): string => {
if (typeof filter === 'string') {
return filter
}
return Object.keys(filter)
.map((key) => {
if (filter[key] === true) {
return `"${key}"`
}
return `-"${key}"`
})
.join(' OR ')
}
return Object.keys(filters)
.filter(
(fieldname) =>
filters[fieldname] && JSON.stringify(filters[fieldname]) !== '{}'
)
.map((fieldname) => `${fieldname}:(${makeQuery(filters[fieldname])})`)
.join(' AND ')
}

private buildPayloadQuery(
{ any, ...fieldSearchFilters }: SearchFilters,
configFilters: SearchFilters,
uuids?: string[],
geometry?: Geometry
) {
const queryFilters = this.stateFiltersToQueryString(fieldSearchFilters)
const must = [this.queryFilterOnValues('isTemplate', 'n')] as Record<
string,
unknown
Expand All @@ -210,6 +234,7 @@ export class ElasticsearchService {
},
})
}
const queryFilters = this.filtersToQueryString(fieldSearchFilters)
if (queryFilters) {
must.push({
query_string: {
Expand Down Expand Up @@ -348,6 +373,7 @@ export class ElasticsearchService {
* }
* }
*/
// FIXME: this is not used anymore
stateFiltersToQueryString(facetsState) {
const query = []
for (const indexKey in facetsState) {
Expand All @@ -365,6 +391,7 @@ export class ElasticsearchService {
return this.combineQueryGroups(query)
}

// FIXME: this is not used anymore
private parseStateNode(nodeName, node, indexKey) {
let queryString = ''
if (node && typeof node === 'object') {
Expand Down Expand Up @@ -416,20 +443,24 @@ export class ElasticsearchService {
}

buildAggregationsPayload(aggregations: AggregationsParams): any {
const mapFilterAggregation = (filterAgg: FilterAggregationParams) => ({
match: filterAgg,
})
const mapToESAggregation = (aggregation: AggregationParams) => {
switch (aggregation.type) {
case 'filters':
return {
filters: Object.keys(aggregation.filters).reduce(
(prev, curr) => ({
filters: Object.keys(aggregation.filters).reduce((prev, curr) => {
const filter = aggregation.filters[curr]
return {
...prev,
[curr]: mapFilterAggregation(aggregation.filters[curr]),
}),
{}
),
[curr]: {
query_string: {
query:
typeof filter === 'string'
? filter
: this.filtersToQueryString(filter),
},
},
}
}, {}),
}
case 'terms':
return {
Expand Down

0 comments on commit eb627f1

Please sign in to comment.