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

#226 Improve Kosh searching results order #227

Open
wants to merge 3 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
86 changes: 83 additions & 3 deletions api/controllers/kosh.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,57 @@ exports.word = async (req, res) => {
}
};

exports.wordSearch = async (req, res) => {
let conn;
const query = `${req.params.query}`;
const match = `%${query}%`;
const fullMatch = `${query}`;
const startMatch = `${query}%`;
const endMatch = `%${query}`;
try {
conn = await req.app.locals.pool.getConnection();
const q = `SELECT w.ID AS id, w.Word AS word, w.WordUni AS wordUni,
d.DefGurmukhi AS definition, d.DefGurmukhiUni AS definitionUni
FROM MahanKoshWords w
LEFT JOIN MahanKoshDefinitions d ON w.Definition = d.ID
WHERE
w.Word LIKE ? OR
w.WordUni LIKE BINARY ?
ORDER BY
CASE
WHEN word LIKE ? THEN 1
WHEN wordUni LIKE ? THEN 1
WHEN word LIKE ? THEN 2
WHEN wordUni LIKE ? THEN 2
WHEN word LIKE ? THEN 3
WHEN wordUni LIKE ? THEN 3
ELSE 4
END`;
const rows = await conn.query(q, [
match,
match,
fullMatch,
fullMatch,
startMatch,
startMatch,
endMatch,
endMatch,
tsingh777 marked this conversation as resolved.
Show resolved Hide resolved
]);
res.json(rows);
} catch (err) {
lib.error(err, res, 500);
} finally {
if (conn) conn.end();
}
};

exports.search = async (req, res) => {
let conn;
const query = `%${req.params.query}%`;
const query = `${req.params.query}`;
const match = `%${query}%`;
const fullMatch = `${query}`;
const startMatch = `${query}%`;
const endMatch = `%${query}`;
try {
conn = await req.app.locals.pool.getConnection();
const q = `SELECT w.ID AS id, w.Word AS word, w.WordUni AS wordUni,
Expand All @@ -59,8 +107,40 @@ exports.search = async (req, res) => {
w.WordUni LIKE BINARY ? OR
d.DefGurmukhi LIKE ? OR
d.DefGurmukhiUni LIKE BINARY ?
ORDER BY w.ID`;
const rows = await conn.query(q, [query, query, query, query]);
ORDER BY
CASE
WHEN word LIKE ? THEN 1
WHEN wordUni LIKE ? THEN 1
WHEN word LIKE ? THEN 2
WHEN wordUni LIKE ? THEN 2
WHEN word LIKE ? THEN 3
WHEN wordUni LIKE ? THEN 3
WHEN definition LIKE ? THEN 4
WHEN definitionUni LIKE ? THEN 4
WHEN definition LIKE ? THEN 5
WHEN definitionUni LIKE ? THEN 5
WHEN definition LIKE ? THEN 6
WHEN definitionUni LIKE ? THEN 6
ELSE 7
END`;
const rows = await conn.query(q, [
match,
match,
match,
match,
fullMatch,
fullMatch,
startMatch,
startMatch,
endMatch,
endMatch,
fullMatch,
fullMatch,
startMatch,
startMatch,
endMatch,
endMatch,
]);
res.json(rows);
} catch (err) {
lib.error(err, res, 500);
Expand Down
2 changes: 2 additions & 0 deletions api/routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ route.get('/kosh/:Letter', limiter.rate100, kosh.letter);

route.get('/kosh/word/:Word', limiter.rate100, kosh.word);

route.get('/kosh/word/search/:query', limiter.rate100, kosh.wordSearch);

route.get('/kosh/search/:query', limiter.rate100, kosh.search);

// Rehat Routes
Expand Down
20 changes: 20 additions & 0 deletions swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,26 @@
}
}
},
"/kosh/word/search/{query}": {
"get": {
"summary": "Kosh Search by word",
"description": "Get words in kosh table by query on words only.",
"parameters": [
{
"name": "query",
"in": "path",
"description": "query to search on",
"required": true,
"type": "string"
}
],
"responses": {
"200": {
"description": "successful operation"
}
}
}
},
"/kosh/search/{query}": {
"get": {
"summary": "Kosh Search",
Expand Down