up
2
up
mozzapp 1764011229 [Programming] 4 comments
How could this algorithm be improved to return better results for entire phrases? ```php $terms = explode(" ", trim($query)); $conditions = []; foreach ($terms as $term) { $term = trim($term); if ($term !== "") { $term = sanitizeString($term); $conditions[] = "title LIKE '%$term%'"; } } $where = count($conditions) > 0 ? implode(" OR ", $conditions) : "1"; $new = queryMysql("SELECT * FROM post WHERE $where ORDER BY id DESC LIMIT $result_for_pages OFFSET $offset"); ``` The issue is that the code snippet has not been very accurate in searching for words within a given sentence, thus causing certain searches to be skipped.
up
3
up
skutlbot 1764035181
Hmm, "it's complicated" ... but I assume searching titles for EACH %$term% is going to catch a lot of junk ( you may need some whitespace n that line "title LIKE '% $term %'" ( but that would run into issues if "term" is the start of end of the title... I suppose you could append leading/trailing spaces to the title (so this post would be " Problems with the search algorithm ") . similarly - if I search for "search algorithm" - you are finding all the titles with %search% and %algorithm% ... which will get "too many" results (versus searches "title LIKE '% search algorithm %' " ' --beskuttle/skutlbot
up
0
up
amargo85 1764084812
spaces...! Yes, it makes sense, but I don't know if it solves the problem.
up
2
up
scutlbot 1764104457
its a lot more than JUST SPACES - the code shows if you put 2 terms in the search it will do an "OR" of the terms, which is (definately) LESS of what I would be searching for ( but I also understand that doing an implied "AND" of those 2 terms may be dependent on the order they appear... if I search for this post using "algorithm search" -- your OR should turn it up (in both $where clauses) - but you are still going to get a lot of other posts (like anything which mentions "algorithm" or "search" ... . but " $term%" like "the" will match all those "them", "there", "theme", "thesis", "theater", etc ... . --beskuttle/skutlbot
up
0
up
amargo85 1764159105
Okay. Now I understand!