Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve full query condition #1293

Merged
merged 26 commits into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
bdca3ec
first version of backwar compatible full query
nitrosx Jun 27, 2024
bd90052
fixed unit tests
nitrosx Jun 27, 2024
88d754c
Merge branch 'master' into fix_full_query_condition
nitrosx Jul 2, 2024
03414a5
build(deps): bump dependabot/fetch-metadata from 2.1.0 to 2.2.0
dependabot[bot] Jul 8, 2024
e4bd7fd
build(deps): bump rimraf from 5.0.7 to 5.0.8
dependabot[bot] Jul 8, 2024
b0ffbbe
build(deps-dev): bump @types/node from 20.14.8 to 20.14.10
dependabot[bot] Jul 8, 2024
e529104
build(deps): bump @nestjs/mongoose from 10.0.6 to 10.0.10
dependabot[bot] Jul 8, 2024
4eb6ee6
build(deps-dev): bump mocha from 10.5.2 to 10.6.0
dependabot[bot] Jul 8, 2024
8158cac
build(deps-dev): bump @typescript-eslint/eslint-plugin
dependabot[bot] Jul 8, 2024
4410298
build(deps): bump mathjs from 13.0.1 to 13.0.2
dependabot[bot] Jul 8, 2024
5f0bc69
build(deps): bump mongoose from 8.4.4 to 8.4.5
dependabot[bot] Jul 8, 2024
5190531
build(deps-dev): bump @nestjs/schematics from 10.1.1 to 10.1.2
dependabot[bot] Jul 8, 2024
e92e58f
build(deps-dev): bump @typescript-eslint/parser from 7.14.1 to 7.15.0
dependabot[bot] Jul 8, 2024
e913869
build(deps-dev): bump @nestjs/cli from 10.3.2 to 10.4.2
dependabot[bot] Jul 8, 2024
0dc264c
build(deps-dev): bump prettier from 3.3.2 to 3.3.3
dependabot[bot] Jul 15, 2024
38f2fc3
build(deps-dev): bump @typescript-eslint/parser from 7.15.0 to 7.16.0
dependabot[bot] Jul 15, 2024
2dde5c8
build(deps-dev): bump @typescript-eslint/eslint-plugin
dependabot[bot] Jul 15, 2024
0b463dd
build(deps): bump mongoose from 8.4.5 to 8.5.1
dependabot[bot] Jul 15, 2024
53f3710
fix: Remove unnecessary properties from datasetMappings and add new m…
Junjiequan Jul 19, 2024
03b8695
fix: Update wrong dataset length assertion in DatasetFilter test
Junjiequan Jul 19, 2024
c17099f
Add test_field_string to RawCorrect3 in DatasetFilter test
Junjiequan Jul 19, 2024
9ed6825
Merge branch 'master' into fix_full_query_condition
Junjiequan Jul 19, 2024
4959954
refactored createIndex function and changed mapping of date type to k…
Junjiequan Jul 21, 2024
3c64305
replaced any types with actual types.
Junjiequan Jul 21, 2024
e0fbd1a
Update indexSetting.ts
Junjiequan Jul 22, 2024
7ecb9f3
Merge branch 'master' into fix_full_query_condition
nitrosx Jul 22, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 0 additions & 19 deletions .github/ISSUE_TEMPLATE/issue-.md

This file was deleted.

1 change: 1 addition & 0 deletions src/common/scientific-relation.enum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ export enum ScientificRelation {
EQUAL_TO_NUMERIC = "EQUAL_TO_NUMERIC",
GREATER_THAN = "GREATER_THAN",
LESS_THAN = "LESS_THAN",
CONTAINS_STRING = "CONTAINS_STRING",
}
39 changes: 36 additions & 3 deletions src/common/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,26 @@ export const convertToRequestedUnit = (
};
};

const buildCondition = (
key: string,
value: string | number,
operator: string,
): Record<string, unknown> => {
const conditions: Record<string, unknown> = { $or: [] };
conditions["$or"] = ["", ".v", ".value"].map((suffix) => {
return {
[`${key}${suffix}`]: { [`${operator}`]: value },
};
});
return conditions;
};

export const mapScientificQuery = (
key: string,
scientific: IScientificFilter[] = [],
): Record<string, unknown> => {
const scientificFilterQuery: Record<string, unknown> = {};
const scientificFilterQueryOr: Record<string, unknown>[] = [];

const keyToFieldMapping: Record<string, string> = {
scientific: "scientificMetadata",
Expand All @@ -112,7 +127,9 @@ export const mapScientificQuery = (

switch (relation) {
case ScientificRelation.EQUAL_TO_STRING: {
scientificFilterQuery[`${matchKeyGeneric}.value`] = { $eq: rhs };
scientificFilterQueryOr.push(
buildCondition(matchKeyGeneric, rhs, "$eq"),
);
break;
}
case ScientificRelation.EQUAL_TO_NUMERIC: {
Expand All @@ -131,7 +148,9 @@ export const mapScientificQuery = (
scientificFilterQuery[matchKeyMeasurement] = { $gt: valueSI };
scientificFilterQuery[matchUnit] = { $eq: unitSI };
} else {
scientificFilterQuery[`${matchKeyGeneric}.value`] = { $gt: rhs };
scientificFilterQueryOr.push(
buildCondition(matchKeyGeneric, rhs, "$gt"),
);
}
break;
}
Expand All @@ -141,12 +160,26 @@ export const mapScientificQuery = (
scientificFilterQuery[matchKeyMeasurement] = { $lt: valueSI };
scientificFilterQuery[matchUnit] = { $eq: unitSI };
} else {
scientificFilterQuery[`${matchKeyGeneric}.value`] = { $lt: rhs };
scientificFilterQueryOr.push(
buildCondition(matchKeyGeneric, rhs, "$lt"),
);
}
break;
}
case ScientificRelation.CONTAINS_STRING: {
scientificFilterQueryOr.push(
buildCondition(matchKeyGeneric, rhs, `/${rhs}/`),
);
break;
}
}
});
if (scientificFilterQueryOr.length == 1) {
scientificFilterQuery["$or"] = scientificFilterQueryOr[0]["$or"];
} else if (scientificFilterQueryOr.length > 1) {
scientificFilterQuery["$and"] = scientificFilterQueryOr;
}

return scientificFilterQuery;
};

Expand Down
13 changes: 4 additions & 9 deletions src/elastic-search/configuration/datasetFieldMapping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,13 @@ export const datasetMappings: MappingObject = {
creationTime: {
type: "date",
},
endTime: {
type: "date",
},
scientificMetadata: {
type: "nested",
dynamic: true,
properties: {
runNumber: {
properties: {
value: {
type: "long",
},
},
},
},
properties: {},
},
history: {
type: "nested",
Expand Down
33 changes: 28 additions & 5 deletions src/elastic-search/configuration/indexSetting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,25 @@ export const special_character_filter: AnalysisPatternReplaceCharFilter = {

//Dynamic templates
export const dynamic_template: Record<string, MappingDynamicTemplate>[] = [
// NOTE: date as keyword is temporary solution for date format inconsistency issue in the scientificMetadata field
{
string_as_keyword: {
date_as_keyword: {
path_match: "scientificMetadata.*.*",
match_mapping_type: "string",
match_mapping_type: "date",
mapping: {
type: "keyword",
},
},
},
// NOTE: This is a workaround for the issue where the start_time field is not being
// parsed correctly. This is a temporary solution until
// we can find a better way to handle date format.
{
start_time_as_keyword: {
path_match: "scientificMetadata.start_time.*",
match_mapping_type: "long",
mapping: {
type: "keyword",
ignore_above: 256,
},
},
},
Expand All @@ -44,9 +56,20 @@ export const dynamic_template: Record<string, MappingDynamicTemplate>[] = [
},
},
{
date_as_keyword: {
double_as_double: {
path_match: "scientificMetadata.*.*",
match_mapping_type: "date",
match_mapping_type: "double",
mapping: {
type: "double",
coerce: true,
ignore_malformed: true,
},
},
},
{
string_as_keyword: {
path_match: "scientificMetadata.*.*",
match_mapping_type: "string",
mapping: {
type: "keyword",
ignore_above: 256,
Expand Down
36 changes: 16 additions & 20 deletions src/elastic-search/elastic-search.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ import {
import { ConfigService } from "@nestjs/config";
import { sleep } from "src/common/utils";
import {
transformKeysInObject,
initialSyncTransform,
transformFacets,
addValueType,
} from "./helpers/utils";

import { SortFields } from "./providers/fields.enum";
Expand Down Expand Up @@ -86,6 +86,10 @@ export class ElasticSearchService implements OnModuleInit {
const isIndexExists = await this.isIndexExists(this.defaultIndex);
if (!isIndexExists) {
await this.createIndex(this.defaultIndex);
Logger.log(
`New index ${this.defaultIndex}is created `,
"ElasticSearch",
);
}
this.connected = true;
Logger.log("Elasticsearch Connected", "ElasticSearch");
Expand Down Expand Up @@ -141,26 +145,18 @@ export class ElasticSearchService implements OnModuleInit {
index,
body: {
settings: defaultElasticSettings,
mappings: {
dynamic: true,
dynamic_templates: dynamic_template,
numeric_detection: true,
date_detection: true,
dynamic_date_formats: [
"yyyy-MM-dd'T'HH:mm:ss|| yyyy-MM-dd HH:mm:ss||yyyy-MM-dd'T'HH:mm:ss.SSSZ||yyyy-MM-dd'T'HH:mm:ss.SSS'Z'||yyyy-MM-dd'T'HH:mm:ss.SSS",
],
properties: datasetMappings,
},
},
});
await this.esService.indices.close({ index });
await this.esService.indices.putSettings({
index,
body: {
settings: defaultElasticSettings,
},
});
await this.esService.indices.putMapping({
index,
dynamic: true,
body: {
dynamic_templates: dynamic_template,
properties: datasetMappings,
},
});
await this.esService.indices.open({
index,
});
Logger.log(
`Elasticsearch Index Created-> Index: ${index}`,
"Elasticsearch",
Expand Down Expand Up @@ -362,7 +358,7 @@ export class ElasticSearchService implements OnModuleInit {
async updateInsertDocument(data: Partial<DatasetDocument>) {
//NOTE: Replace all keys with lower case, also replace spaces and dot with underscore
delete data._id;
const transformedScientificMetadata = transformKeysInObject(
const transformedScientificMetadata = addValueType(
data.scientificMetadata as Record<string, unknown>,
);

Expand Down
Loading