Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- type ElSearchQuery = {
- query: {
- bool: {
- should: Array<MultiMatch>
- filter: [
- {
- term: {
- serviceLine: string
- }
- }
- ]
- },
- }
- };
- type ServiceLine = { serviceId: string, serviceName: string };
- type ParoTag = string | { id: number, value: string };
- type ProjectSize = '10 hours' | '11-30 hours' | '31-50 hours' | '51+ hours';
- interface ParoElasticSearch {
- first: Array<ParoTag>;
- second: Array<ParoTag>;
- neutral: Array<ParoTag>;
- secondLeast: Array<ParoTag>;
- least: Array<ParoTag>;
- primaryServiceLine: ServiceLine;
- secondaryServiceLine: ServiceLine;
- projectSize: ProjectSize;
- queryObj: ElSearchQuery;
- formattedQuery: ElSearchQuery;
- }
- class ParoElasticSearchQuery implements ParoElasticSearch {
- first: Array<ParoTag>;
- second: Array<ParoTag>;
- neutral: Array<ParoTag>;
- secondLeast: Array<ParoTag>;
- least: Array<ParoTag>;
- primaryServiceLine: ServiceLine;
- secondaryServiceLine: ServiceLine;
- projectSize: ProjectSize;
- queryObj: ElSearchQuery = {
- query: {
- bool: {
- should: [],
- filter: [{
- term: {
- serviceLine: null
- }
- }],
- }
- }
- };
- constructor(private query: ParoElasticSearch) {
- this.first = query.first;
- this.second = query.second;
- this.neutral = query.neutral;
- this.secondLeast = query.secondLeast;
- this.least = query.least;
- this.primaryServiceLine = query.primaryServiceLine;
- this.secondaryServiceLine = query.secondaryServiceLine;
- this.projectSize = query.projectSize;
- }
- private _buildMultiMatch(tag: ParoTag, boost: number): MultiMatch {
- return {
- multi_match: {
- query: (typeof tag === 'string' ? tag : tag.value),
- boost: boost,
- fields: [
- 'strengths',
- 'projects.tags',
- 'projects.company.name',
- 'projects.description'
- ]
- }
- };
- }
- private get _formatQueryObject(): ElSearchQuery {
- (this.first || []).map(tag => this.queryObj.query.bool.should.push(this._buildMultiMatch(tag, 5)));
- (this.second || []).map(tag => this.queryObj.query.bool.should.push(this._buildMultiMatch(tag, 4)));
- (this.neutral || []).map(tag => this.queryObj.query.bool.should.push(this._buildMultiMatch(tag, 3)));
- (this.secondLeast || []).map(tag => this.queryObj.query.bool.should.push(this._buildMultiMatch(tag, 2)));
- (this.least || []).map(tag => this.queryObj.query.bool.should.push(this._buildMultiMatch(tag, 1)));
- if (this.primaryServiceLine) {
- const [ primaryServiceLine ] = this.queryObj.query.bool.filter;
- primaryServiceLine.term.serviceLine = this.primaryServiceLine.serviceName;
- }
- if (this.secondaryServiceLine) {
- this.queryObj.query.bool.should.push(this._buildMultiMatch(this.secondaryServiceLine.serviceName, 5));
- }
- if (this.projectSize) {
- this.queryObj.query.bool.should.push(this._buildMultiMatch(this.projectSize, 5));
- }
- return this.queryObj;
- }
- /**
- * returns the formatted elasticsearch query
- */
- get formattedQuery() {
- return this._formatQueryObject;
- }
- }
Add Comment
Please, Sign In to add comment