Skip to content

Instantly share code, notes, and snippets.

@iamtracy
Created October 16, 2018 20:12
Show Gist options
  • Save iamtracy/b20329e1990d47d5fd097fb7754d5095 to your computer and use it in GitHub Desktop.
Save iamtracy/b20329e1990d47d5fd097fb7754d5095 to your computer and use it in GitHub Desktop.
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;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment