Prioritize display of full-text search results for "best matches" for exact matches, including join tables
P粉002572690
P粉002572690 2024-03-29 12:30:39
0
1
411

What do I have to change in the query to get an exact match in the first place?

For example, anyone enters happy songs in the search form, now he will get this output..

Happy Child

Happy days

Happy people

Happy Song

Happy Station

...

This is my query..

SELECT  `artist`, `song`, `genre`, `pass`, `id`
        FROM  `songs`
        JOIN  `artists` USING (`pass`)
        WHERE  MATCH(`artist`) AGAINST("happy song")
          OR  MATCH(`song`, `genre`) AGAINST("happy song")
        ORDER BY  `song` ASC
        LIMIT  0,30
') or die(mysql_error($_connect));```

Thank you!

EDIT 03.07.22
Changed query to:<br>
```$query = mysqli_query($_connect, '
    SELECT  `artist`, `song`, `genre`, `pass`, `id`
        FROM  `songs`
        JOIN  `artists` USING (`pass`)
        WHERE  MATCH(`artist`) AGAINST("happy song")
          OR  MATCH(`song`, `genre`) AGAINST("happy song")
        ORDER BY  `song` = "happy song" DESC
') or die(mysql_error($_connect));```

The output is the same, result with only "song" in the text are above in the result when searched for "happy song"

**EDIT 14.7.22**
**Table Artists:**

CREATE TABLE `artists` (
  `artist` varchar(40) NOT NULL,
  `pass` int(10) UNSIGNED NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 PACK_KEYS=1;

ALTER TABLE `artists`
  ADD PRIMARY KEY (`pass`),
  ADD UNIQUE KEY `uni_que` (`artist`) USING BTREE;
ALTER TABLE `artists` ADD FULLTEXT KEY `full_txt` (`artist`);
COMMIT;

**Table Songs:**

    CREATE TABLE `songs` (
      `song` varchar(80) NOT NULL,
      `tags` varchar(40) NOT NULL,
      `style` varchar(20) NOT NULL,
      `pass` bigint(20) UNSIGNED NOT NULL,
      `id` bigint(20) UNSIGNED NOT NULL
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 PACK_KEYS=1;
    
    ALTER TABLE `songs`
      ADD PRIMARY KEY (`id`);
    ALTER TABLE `songs` ADD FULLTEXT KEY `song` (`song`,`tags`,`style`);
    COMMIT;

P粉002572690
P粉002572690

reply all(1)
P粉116654495

You are currently requesting ORDER BY song ASC Sort alphabetically

Change to ORDER BY MATCH(...) AGAINST(...) DESC

This might put "A Song About a Happy Child" at the top of the list. In other words, if you need to "sing" first, we need to work harder:

SELECT ...
    FROM ...
    WHERE ...
    ORDER BY song = "happy song" DESC,
             MATCH(...) AGAINST (...)

(Since you have OR and multiple columns involved, the query can get quite confusing.)

If you don't want a song with only one word, see operator, or

MATCH(song) AGAINST('+"happy song"')

Note the intentional use of quotation marks.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template