1

I'm having the hardest time with a php (or mysql) search function. I'd be willing to buy a script for that, but i can't find any.

I have a customer table (firstname, lastname, street, zip, city etc....) and i would like to be able to not just look for one keyword but for 2 IN 2 DIFFERENT columns.

for instance:

Keyword: "John Doe"

So my attempt was.

SELECT ....
   WHERE CONCAT(firstname,lastname) LIKE '%john%'
   AND CONCAT(firstname,lastname LIKE '%doe%'

However: that gives me back all johns and does and Mr. John Doe is somewhere in that list, but not on top, even though it's supposed to be the most relevant result.

I also tried:

....
   WHERE MATCH(firstname,lastname) AGAINST('%john doe%')

And that pretty much gives back the same result.

So the result I'm looking for would be:

1. John Doe (at first position!)
2. John Miller
3. John Smith
4. Harry Doe
5. Jack Doe
etc......

I've been looking for 2 hours and i refuse to believe I'm the first person who ever tried to do that :-)

Any help is appreciated!

Thanks!

3 Answers 3

3

Did you also try something like this

SELECT MATCH(firstname, lastname) AGAINST ('john doe') as Relevance 
FROM table WHERE MATCH(firstname, lastname) AGAINST('john doe' IN
BOOLEAN MODE) 
HAVING Relevance > 0.3
ORDER BY Relevance DESC

see also

http://dev.mysql.com/doc/refman/5.5/en/fulltext-boolean.html

This might be also a solution:

SELECT MATCH(firstname) AGAINST ('john doe') as firstname_relevance, 
    MATCH(lastname) AGAINST ('john doe') as lastname_relevance
    FROM table WHERE MATCH(firstname, lastname) AGAINST('john doe' IN
    BOOLEAN MODE) 
    ORDER BY firstname_relevance+lastname_relevance DESC
Sign up to request clarification or add additional context in comments.

8 Comments

but still john doe isn't on first position. there some other "doe" having a higher relevance. i kinda don't get that
May you post the order in which the rows showed up with relevancy ? I also modified my query to exclude the % symbols as far as i know the wildcard in against queries is *
Ute Schmidt 4.70247888565063 Pau Schmidt 4.70247888565063 Uwe Schmidt 4.70247888565063 Eva Schmidt 4.65077877044678
#1191 - Can't find FULLTEXT index matching the column list my fulltext index contains both rows. is that wrong?
You might need to add two other index also for the alternate solution. One per each column.
|
0

Like this?

SELECT
   firstname, lastname, othercol, MIN(Weighting)
FROM
    (
    SELECT firstname, lastname, othercol, 1 AS Weighting FROM...
    WHERE firstname = 'john' AND lastname  = 'doe'
    UNION ALL
    SELECT firstname, lastname, othercol, 2 AS Weighting FROM...
    WHERE firstname = 'john' OR lastname  = 'doe'
    ) T
GROUP BY
   firstname, lastname, othercol
ORDER BY
   MIN(Weighting) DESC;

2 Comments

yeah, but what if someone enters "doe john"?
@Roman Klare: update your question then. I answered what you asked
0

Boolean mode do not automatically sort rows in order of decreasing relevance so you have to :

SELECT MATCH(firstname,lastname) AGAINST('john doe') as Relevance FROM table WHERE MATCH
MATCH(firstname,lastname) AGAINST('john doe'  IN 
BOOLEAN MODE) ORDER 
BY Relevance DESC

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.