Skip to content

Commit

Permalink
revert: ES changes
Browse files Browse the repository at this point in the history
  • Loading branch information
f-necas committed Oct 3, 2023
1 parent 3608bb0 commit fa6bc61
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 172 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 '@geonetwork-ui/api/metadata-converter'
import { EsSearchParams } from '../types/elasticsearch.model'

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

expect(query).toEqual({
bool: {
filter: [],
Expand All @@ -130,8 +131,8 @@ describe('ElasticsearchService', () => {
},
},
{
ids: {
values: ['record-1', 'record-2', 'record-3'],
query_string: {
query: '(Org:"world")',
},
},
],
Expand Down Expand Up @@ -179,8 +180,8 @@ describe('ElasticsearchService', () => {
},
},
{
match: {
Org: '"world"',
query_string: {
query: '(Org:"world")',
},
},
{
Expand All @@ -203,9 +204,6 @@ describe('ElasticsearchService', () => {
Org: {
world: true,
},
name: {
john: true,
},
any: 'hello',
},
{},
Expand Down Expand Up @@ -235,107 +233,9 @@ describe('ElasticsearchService', () => {
query: 'hello',
},
},
{
match: {
Org: '"world"',
},
},
{
match: {
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'],
},
},
{
match: {
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',
},
},
{
match: {
Org: 'world AND world2',
query: '(Org:"world")',
},
},
{
Expand Down Expand Up @@ -387,7 +287,9 @@ describe('ElasticsearchService', () => {
it('adds boosting of 7 for intersecting with it and boosting of 10 on geoms within', () => {
const query = service['buildPayloadQuery'](
{
Org: 'world',
Org: {
world: true,
},
any: 'hello',
},
{},
Expand Down Expand Up @@ -418,8 +320,8 @@ describe('ElasticsearchService', () => {
},
},
{
match: {
Org: 'world',
query_string: {
query: '(Org:"world")',
},
},
],
Expand Down Expand Up @@ -719,13 +621,17 @@ describe('ElasticsearchService', () => {
})
).toStrictEqual({
myFilters: {
filter1: {
match: {
field1: '100',
filters: {
filter1: {
match: {
field1: '100',
},
},
filter2: {
match: {
field2: { value1: true, value3: true },
},
},
},
filter2: {
match: { field2: '"value1" AND "value3"' },
},
},
myHistogram: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ import {
Aggregation,
AggregationParams,
AggregationsParams,
FieldFilter,
FieldFilters,
FilterAggregationParams,
SortByField,
} from '@geonetwork-ui/common/domain/search'
import { METADATA_LANGUAGE } from '../../metadata-language'
Expand Down Expand Up @@ -82,13 +81,6 @@ export class ElasticsearchService {
addMapping(node.field)
}
}
if ('match' in node && typeof node.match === 'object') {
for (const key in node.match) {
if (key in this.runtimeFields) {
addMapping(key)
}
}
}
for (const runtimeField in this.runtimeFields) {
if (
runtimeField in node &&
Expand Down Expand Up @@ -191,6 +183,7 @@ export class ElasticsearchService {
uuids?: string[],
geometry?: Geometry
) {
const queryFilters = this.stateFiltersToQueryString(fieldSearchFilters)
const must = [this.queryFilterOnValues('isTemplate', 'n')] as Record<
string,
unknown
Expand All @@ -217,9 +210,12 @@ export class ElasticsearchService {
},
})
}
if (Object.keys(fieldSearchFilters).length > 0) {
const filters = this.searchFiltersToESFilters(fieldSearchFilters)
must.push(...filters)
if (queryFilters) {
must.push({
query_string: {
query: queryFilters,
},
})
}
if (uuids) {
must.push({
Expand Down Expand Up @@ -419,49 +415,22 @@ export class ElasticsearchService {
)
}

private searchFiltersToESFilters(
filters: FieldFilters
): Record<string, unknown>[] {
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(' AND ')
}
const matchArray = Object.keys(filters)
.filter(
(fieldname) =>
filters[fieldname] && JSON.stringify(filters[fieldname]) !== '{}'
)
.map((fieldname) => ({
match: {
[fieldname]: makeQuery(filters[fieldname]),
},
}))
return matchArray
}

buildAggregationsPayload(aggregations: AggregationsParams): any {
const mapFilterAggregation = (filterAgg: FilterAggregationParams) => ({
match: filterAgg,
})
const mapToESAggregation = (aggregation: AggregationParams) => {
switch (aggregation.type) {
case 'filters': {
return Object.keys(aggregation.filters).reduce(
(prev, curr) => ({
...prev,
[curr]: this.searchFiltersToESFilters(
aggregation.filters[curr] as FieldFilters
)[0],
}),
{}
)
}
case 'filters':
return {
filters: Object.keys(aggregation.filters).reduce(
(prev, curr) => ({
...prev,
[curr]: mapFilterAggregation(aggregation.filters[curr]),
}),
{}
),
}
case 'terms':
return {
terms: {
Expand Down

0 comments on commit fa6bc61

Please sign in to comment.