Skip to content

Commit

Permalink
fix: restructure the required changes among files
Browse files Browse the repository at this point in the history
  • Loading branch information
sercancicek committed Jun 10, 2024
1 parent e56754c commit 8f45de7
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 72 deletions.
2 changes: 1 addition & 1 deletion src/app/components/search/search.html
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ <h1>
<div class="app-details">
<div class="app-frame app-pad">
<div class="results">
<div ng-repeat="result in results | filter:limit_search | orderBy:'overallWeight':true track by result.model.unique_id"
<div ng-repeat="result in results | filter:limit_search track by result.model.unique_id"
data-ui-state="getState(result.model)" data-ui-state-params="{unique_id: result.model.unique_id}"
ng-click="onSelect()"
class="result search-result a">
Expand Down
69 changes: 0 additions & 69 deletions src/app/main/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,75 +145,6 @@ angular
}
});

$scope.$watch('search.query', function(q) {
$scope.search.results = assignSearchRelevance(projectService.search(q));
});

function assignSearchRelevance(results){
if($scope.search.query === "")
return results;
let criteriaArr = {
"name": 10,
"tags": 5,
"description": 3,
"raw_code": 2,
"columns": 1
};
_.each(results, function(result){
result.overallWeight = 0;
_.each(Object.keys(criteriaArr), function(criteria){
if(result.model[criteria] != undefined){
let count = 0;
let body = result.model[criteria];
let query = ($scope.search.query).toLowerCase();
if(criteria === "columns"){
_.each(body, function(column){
// there a spark bug where columns are missign from the catalog. That
// needs to be fixed outside of docs but this if != null check will
// allow docs to continue to function now and also when the bug is
// fixed.
// relevant issue: https://github.com/dbt-labs/dbt-spark/issues/295
if (column.name) {
let columnName = column.name.toLowerCase();
let index = 0;
while(index != -1){
index = columnName.indexOf(query, index);
if (index != -1) {
count++; index++;
}
}
}
});
}
else if(criteria === "tags"){
_.each(body, function(tag){
let tagName = tag.toLowerCase();
let index = 0;
while(index != -1){
index = tagName.indexOf(query, index);
if (index != -1) {
count++; index++;
}
}
});
}
else{
body = body.toLowerCase();
let index = 0;
while(index != -1){
index = body.indexOf(query, index);
if(index != -1){
count++; index++;
}
}
}
result.overallWeight += (count * criteriaArr[criteria]);
}
});
});
return results;
}

/*
INITIALIZE THE APPLICATION:
1. Set the selected model (if there is one) via the url
Expand Down
42 changes: 42 additions & 0 deletions src/app/services/proejct_service_utils.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
const proj_utils = require('./project_service_utils')

describe('Project Service Tests', () => {
describe('assignSearchRelevance', () => {
let results;

beforeEach(() => {
results = [
{ model: { name: 'dm_test', tags: ["dm", "test", "test model"], columns: {"id": {"name": "id"}}, raw_code: "SELECT test from test" }, overallWeight: 0, overallNameWeight: 0 },
{ model: { name: 'ft_test_person', tags: ["test", "ft", "person"], columns: {"id": {"name": "id"}}, raw_code: "SELECT test, test from test" }, overallWeight: 0, overallNameWeight: 0 },
{ model: { name: 'test_event', tags: ["test", "event"], columns: {"test": {"name": "test"}} , raw_code: "SELECT id from abc" }, overallWeight: 0, overallNameWeight: 0 },
{ model: { name: 'test_log', tags: ["test", "log"], overallWeight: 0, overallNameWeight: 0 }},
{ model: { name: 'test', tags: [], columns: {} }, overallWeight: 0, overallNameWeight: 0 },
{ model: { name: 'n/a', tags: [], columns: {} }, overallWeight: 0, overallNameWeight: 0 },
];
});

it('should prioritize exact name matches', () => {
proj_utils.assignSearchRelevance(results, 'test');
expect(results[0].model.name).toBe('test');
expect(results[0].overallNameWeight).toBe(100);
expect(results[0].overallWeight).toBe(100);

expect(results[1].model.name).toBe('test_event');
expect(results[1].overallNameWeight).toBe(50);
expect(results[1].overallWeight).toBe(56);

expect(results[2].model.name).toBe('test_log');
expect(results[2].overallNameWeight).toBe(50);
expect(results[2].overallWeight).toBe(55);

expect(results[3].model.name).toBe('dm_test');
expect(results[3].overallNameWeight).toBe(30);
expect(results[3].overallWeight).toBe(44);

expect(results[4].model.name).toBe('ft_test_person');
expect(results[4].overallNameWeight).toBe(10);
expect(results[4].overallWeight).toBe(21);
});

});
});
5 changes: 3 additions & 2 deletions src/app/services/project_service.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const angular = require('angular');
const $ = require('jquery');
const _ = require('lodash');
const { getQuoteChar } = require('./compat');
const { assignSearchRelevance } = require('./project_service_utils');

import merge from 'deepmerge';

Expand Down Expand Up @@ -357,7 +358,7 @@ angular
}

service.search = function(q) {
if (q.length == 0) {
if (q.length === 0) {
return _.map(service.project.searchable, function(model) {
return {
model: model,
Expand All @@ -376,7 +377,7 @@ angular
});
}
});
return res;
return assignSearchRelevance(res, q);
}

function clean_project_macros(macros, adapter) {
Expand Down
89 changes: 89 additions & 0 deletions src/app/services/project_service_utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
const _ = require('lodash');


function assignSearchRelevance(results, q) {
if(q === "") {
return results;
}
let criteriaArr = {
"name": 10,
"tags": 5,
"description": 3,
"raw_code": 2,
"columns": 1
};

_.each(results, function(result){
result.overallWeight = 0;
result.overallNameWeight = 0;
_.each(Object.keys(criteriaArr), function(criteria){
if(result.model[criteria] !== undefined){
let count = 0;
let body = result.model[criteria];
let query = (q).toLowerCase();
if(criteria === "columns"){
_.each(body, function(column){
// there a spark bug where columns are missign from the catalog. That
// needs to be fixed outside of docs but this if != null check will
// allow docs to continue to function now and also when the bug is
// fixed.
// relevant issue: https://github.com/dbt-labs/dbt-spark/issues/295
if (column.name) {
let columnName = column.name.toLowerCase();
let index = 0;
while(index !== -1){
index = columnName.indexOf(query, index);
if (index !== -1) {
count++; index++;
}
}
}
});
}
else if(criteria === "name"){
const calculateNameMatchWeight = (body, query) => {
if (body === query) return 10;
const lowerBody = body.toLowerCase();
if (lowerBody.startsWith(query)) return 5;
if (lowerBody.endsWith(query)) return 3;
if (lowerBody.includes(query)) return 1;
return 0;
};

count += calculateNameMatchWeight(body, (q).toLowerCase());
result.overallNameWeight += (count * criteriaArr[criteria]);

}
else if(criteria === "tags"){
_.each(body, function(tag){
let tagName = tag.toLowerCase();
let index = 0;
while(index != -1){
index = tagName.indexOf(query, index);
if (index != -1) {
count++; index++;
}
}
});
}
else{
body = body.toLowerCase();
let index = 0;
while(index != -1){
index = body.indexOf(query, index);
if(index != -1){
count++; index++;
}
}
}
result.overallWeight += (count * criteriaArr[criteria]);
}
});
});
results.sort((a, b) => b.overallNameWeight - a.overallNameWeight || b.overallWeight - a.overallWeight);
return results
}

module.exports = {
assignSearchRelevance,
}

0 comments on commit 8f45de7

Please sign in to comment.