banidb-api icon indicating copy to clipboard operation
banidb-api copied to clipboard

#226 Improve Kosh searching results order

Open tsingh777 opened this issue 4 years ago • 7 comments

Followed this example: https://www.codexworld.com/how-to/sort-results-order-by-best-match-using-like-in-mysql/

The order would be 1 - if the query matches the words 2 - if the query matches the beginning of the word 3 - if the query matches the end of the word 4 - if the query matches the definition 5 - if the query matches the beginning of the definition 6 - if the query matches the end of the definition

Also adds a new end point that searches only on words and not definitions.

tsingh777 avatar Jan 03 '21 21:01 tsingh777

Adding an if then in a query is a performance killer and not scalable (in any database).

On Mon, Jan 4, 2021 at 7:52 AM tsingh777 [email protected] wrote:

@tsingh777 commented on this pull request.

In api/controllers/kosh.js https://github.com/KhalisFoundation/banidb-api/pull/227#discussion_r551399908 :

  •                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,
    
  •  match,
    
  •  match,
    
  •  fullMatch,
    
  •  fullMatch,
    
  •  startMatch,
    
  •  startMatch,
    
  •  endMatch,
    
  •  endMatch,
    

Will prioritize #113 https://github.com/KhalisFoundation/banidb-api/issues/113 next so these things can be caught earlier.

— You are receiving this because your review was requested. Reply to this email directly, view it on GitHub https://github.com/KhalisFoundation/banidb-api/pull/227#discussion_r551399908, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAE5OFODGI3QK4XQVJNQ3FTSYHP25ANCNFSM4VSFTE4A .

ManjotS avatar Jan 04 '21 16:01 ManjotS

Adding an if then in a query is a performance killer and not scalable (in any database). On Mon, Jan 4, 2021 at 7:52 AM tsingh777 @.> wrote: @.* commented on this pull request. ------------------------------ In api/controllers/kosh.js <#227 (comment)> : > + 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, + match, + match, + fullMatch, + fullMatch, + startMatch, + startMatch, + endMatch, + endMatch, Will prioritize #113 <#113> next so these things can be caught earlier. — You are receiving this because your review was requested. Reply to this email directly, view it on GitHub <#227 (comment)>, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAE5OFODGI3QK4XQVJNQ3FTSYHP25ANCNFSM4VSFTE4A .

@ManjotS What do you recommend?

tsingh777 avatar Jan 04 '21 23:01 tsingh777

Adding an if then in a query is a performance killer and not scalable (in any database). On Mon, Jan 4, 2021 at 7:52 AM tsingh777 @.> wrote: @.* commented on this pull request. ------------------------------ In api/controllers/kosh.js <#227 (comment)> : > + 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, + match, + match, + fullMatch, + fullMatch, + startMatch, + startMatch, + endMatch, + endMatch, Will prioritize #113 <#113> next so these things can be caught earlier. — You are receiving this because your review was requested. Reply to this email directly, view it on GitHub <#227 (comment)>, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAE5OFODGI3QK4XQVJNQ3FTSYHP25ANCNFSM4VSFTE4A .

there isn't a logical work around - what you could do is either pre-stage that data by setting up intermediary tables or you can reduce the # of conditions you are checking for.

tarunsingh5 avatar Jan 04 '21 23:01 tarunsingh5

What about UNION ?

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 ? 
UNION 
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.WordUni LIKE BINARY ?
UNION 
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 d.DefGurmukhi LIKE ?
UNION 
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 d.DefGurmukhiUni LIKE BINARY ?

tsingh777 avatar Jan 05 '21 01:01 tsingh777

this should work, another thing to make this faster may be to do the where clause prior to the join

tarunsingh5 avatar Jan 12 '21 09:01 tarunsingh5

@tsingh777 whats the status on this?

AkalUstat avatar Apr 11 '21 20:04 AkalUstat

Need to run some performance tests. will update soon.

tsingh777 avatar Apr 18 '21 23:04 tsingh777