/    Sign up×
Community /Pin to ProfileBookmark

Complication Name Search

Good Day Coders,

I’m hoping someone can help me.

I’m searching a very large database based on my search string. Here’s my code below. John Jackson comes from a text box and this code has been pulled from previous costs on this forum.

“`
SELECT *, MATCH (`lastname`,`firstname`,`middlenames`,`knownas`,`nickname`) AGAINST (‘John Jackson’ IN NATURAL LANGUAGE MODE) As Score FROM `staff`
WHERE
HAVING Score > 0
ORDER BY Score DESC
“`

I get a score – does anyone know what this score is made from, like how does it get calculated? a lost of scores are in single digits so it’s not a percentage I don’t believe.

I get hundreds of results from my thousands of records. It includes any John or any Jackson even if their other names are unrelated.

I really want to only get strong / close matches.

I can’t just search first and last names as some people go by nickname, of John is a ”know as’ name because he real name is Johnathon.

Other people are known only by their nickname like a staff member known as TinTin, a lot of staff don’t even know his real name.

How can I improve this code?

I had thought about joining names into runtime columns and matching against this?

`
CONCAT_WS(‘ ‘, `
lastname`, `firstname`)
.`

But I’m not sure how efficient that would be as I’d need a large number of combinations and I’m not sure if I can even match there on a WHERE clause??

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@NogDogFeb 16.2022 — The score is based on many factors, and without lots of testing/experimenting (or being much smarter than I am) it's hard to know what a "good" score would be for a given situation.

I don't think doing concat_ws() would help, as you'd then be searching against a non-fulltext data set at that point.

Maybe you could combine a boolean search along with what you're doing, and weight those first:
[code=sql]
SELECT *,
MATCH (lastname,firstname,middlenames,knownas,nickname)
AGAINST ('John Jackson' IN NATURAL LANGUAGE MODE) As Score,
MATCH (lastname,firstname,middlenames,knownas,nickname)
AGAINST ('+John +Jackson' IN BOOLEAN MODE) As all_found
FROM staff,
WHERE MATCH (lastname,firstname,middlenames,knownas,nickname)
AGAINST ('John Jackson' IN NATURAL LANGUAGE MODE) As Score
ORDER BY all_found DESC, Score DESC
[/code]

See https://dev.mysql.com/doc/refman/8.0/en/fulltext-boolean.html for more info on the operators you can use in Boolean mode searches. (E.g.: maybe you'd wan'+John* +Jackson*'.)
Copy linkTweet thisAlerts:
@ginerjmFeb 16.2022 — If this search is triggered by a user filling in a data form perhaps you could offer them the choice of what type of value they are entering. A checkbox to try a search on lastname-firstname or simply the nickname or one of your other search fields. Then build the query based upon the user's help.
×

Success!

Help @singaporeDude spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...