Guest User

Untitled

a guest
Oct 16th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.08 KB | None | 0 0
  1. type ElSearchQuery = {
  2. query: {
  3. bool: {
  4. should: Array<MultiMatch>
  5. filter: [
  6. {
  7. term: {
  8. serviceLine: string
  9. }
  10. }
  11. ]
  12. },
  13. }
  14. };
  15.  
  16. type ServiceLine = { serviceId: string, serviceName: string };
  17.  
  18. type ParoTag = string | { id: number, value: string };
  19.  
  20. type ProjectSize = '10 hours' | '11-30 hours' | '31-50 hours' | '51+ hours';
  21.  
  22. interface ParoElasticSearch {
  23. first: Array<ParoTag>;
  24. second: Array<ParoTag>;
  25. neutral: Array<ParoTag>;
  26. secondLeast: Array<ParoTag>;
  27. least: Array<ParoTag>;
  28. primaryServiceLine: ServiceLine;
  29. secondaryServiceLine: ServiceLine;
  30. projectSize: ProjectSize;
  31. queryObj: ElSearchQuery;
  32. formattedQuery: ElSearchQuery;
  33. }
  34.  
  35. class ParoElasticSearchQuery implements ParoElasticSearch {
  36.  
  37. first: Array<ParoTag>;
  38. second: Array<ParoTag>;
  39. neutral: Array<ParoTag>;
  40. secondLeast: Array<ParoTag>;
  41. least: Array<ParoTag>;
  42. primaryServiceLine: ServiceLine;
  43. secondaryServiceLine: ServiceLine;
  44. projectSize: ProjectSize;
  45. queryObj: ElSearchQuery = {
  46. query: {
  47. bool: {
  48. should: [],
  49. filter: [{
  50. term: {
  51. serviceLine: null
  52. }
  53. }],
  54. }
  55. }
  56. };
  57.  
  58. constructor(private query: ParoElasticSearch) {
  59. this.first = query.first;
  60. this.second = query.second;
  61. this.neutral = query.neutral;
  62. this.secondLeast = query.secondLeast;
  63. this.least = query.least;
  64. this.primaryServiceLine = query.primaryServiceLine;
  65. this.secondaryServiceLine = query.secondaryServiceLine;
  66. this.projectSize = query.projectSize;
  67. }
  68.  
  69. private _buildMultiMatch(tag: ParoTag, boost: number): MultiMatch {
  70. return {
  71. multi_match: {
  72. query: (typeof tag === 'string' ? tag : tag.value),
  73. boost: boost,
  74. fields: [
  75. 'strengths',
  76. 'projects.tags',
  77. 'projects.company.name',
  78. 'projects.description'
  79. ]
  80. }
  81. };
  82. }
  83.  
  84. private get _formatQueryObject(): ElSearchQuery {
  85.  
  86. (this.first || []).map(tag => this.queryObj.query.bool.should.push(this._buildMultiMatch(tag, 5)));
  87. (this.second || []).map(tag => this.queryObj.query.bool.should.push(this._buildMultiMatch(tag, 4)));
  88. (this.neutral || []).map(tag => this.queryObj.query.bool.should.push(this._buildMultiMatch(tag, 3)));
  89. (this.secondLeast || []).map(tag => this.queryObj.query.bool.should.push(this._buildMultiMatch(tag, 2)));
  90. (this.least || []).map(tag => this.queryObj.query.bool.should.push(this._buildMultiMatch(tag, 1)));
  91.  
  92. if (this.primaryServiceLine) {
  93. const [ primaryServiceLine ] = this.queryObj.query.bool.filter;
  94. primaryServiceLine.term.serviceLine = this.primaryServiceLine.serviceName;
  95. }
  96.  
  97. if (this.secondaryServiceLine) {
  98. this.queryObj.query.bool.should.push(this._buildMultiMatch(this.secondaryServiceLine.serviceName, 5));
  99. }
  100.  
  101. if (this.projectSize) {
  102. this.queryObj.query.bool.should.push(this._buildMultiMatch(this.projectSize, 5));
  103. }
  104.  
  105. return this.queryObj;
  106.  
  107. }
  108.  
  109. /**
  110. * returns the formatted elasticsearch query
  111. */
  112. get formattedQuery() {
  113. return this._formatQueryObject;
  114. }
  115.  
  116. }
Add Comment
Please, Sign In to add comment