Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve search relevance #8

Merged
merged 21 commits into from
Jun 7, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
feat: introduce unit tests
  • Loading branch information
sercancicek committed Jun 7, 2024
commit fe316e2b0f03b11c4ca35d1f2d49eda982c20c34
3 changes: 1 addition & 2 deletions FORK.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,7 @@ To help keep things trackable, the following branch naming conventions are used:
value is also computed and stored for the name field. Finally, the results are
sorted first by overallNameWeight and then by overallWeight.




## Changelog entry example

```
Expand Down
84 changes: 2 additions & 82 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 @@ -333,89 +334,8 @@ angular
return objects
}

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
}

service.search = function(q) {
if (q.length == 0) {
if (q.length === 0) {
return _.map(service.project.searchable, function(model) {
return {
model: model,
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,
}
42 changes: 42 additions & 0 deletions src/app/services/project_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);
});

});
});