Skip to content

Commit

Permalink
elasticsearch variable naming improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
Junjiequan committed May 21, 2024
1 parent c4daf42 commit 20844f1
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/elastic-search/providers/fields.enum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export enum FilterFields {
Mode = "mode",
}

export enum QueryFields {
export enum MustFields {
DatasetName = "datasetName",
Description = "description",
}
Expand Down
14 changes: 9 additions & 5 deletions src/elastic-search/providers/query-builder.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
} from "../interfaces/es-common.type";
import {
FilterFields,
QueryFields,
MustFields,
FacetFields,
ShouldFields,
} from "./fields.enum";
Expand All @@ -22,7 +22,7 @@ import { convertToElasticSearchQuery } from "../helpers/utils";
@Injectable()
export class SearchQueryService {
readonly filterFields = [...Object.values(FilterFields)];
readonly queryFields = [...Object.values(QueryFields)];
readonly mustFields = [...Object.values(MustFields)];
readonly shouldFields = [...Object.values(ShouldFields)];
readonly facetFields = [...Object.values(FacetFields)];
readonly textQuerySplitMethod = /[ ,]+/;
Expand All @@ -33,9 +33,13 @@ export class SearchQueryService {

const filter = this.buildFilterFields(fields);
const should = this.buildShouldFields(fields);
const query = this.buildTextQuery(fields);
const must = this.buildTextQuery(fields);

return this.constructFinalQuery(filter, should, query);
// NOTE: The final query flow is as follows:
// step 1. Build filter fields conditions must match all filter fields
// step 2. Build should fields conditions must match at least one should field
// step 3. Build text query conditions must match all text query fields
return this.constructFinalQuery(filter, should, must);
} catch (err) {
Logger.error("Elastic search build search query failed");
throw err;
Expand Down Expand Up @@ -100,7 +104,7 @@ export class SearchQueryService {
private buildWildcardQueries(text: string): QueryDslQueryContainer[] {
const terms = this.splitSearchText(text);
return terms.flatMap((term) =>
this.queryFields.map((fieldName) => ({
this.mustFields.map((fieldName) => ({
wildcard: { [fieldName]: { value: `*${term}*` } },
})),
);
Expand Down

0 comments on commit 20844f1

Please sign in to comment.