search

Home  >  Q&A  >  body text

Prioritize display of full-text search results for "best matches" for exact matches, including join tables

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粉002572690P粉002572690238 days ago404

reply all(1)I'll reply

  • P粉116654495

    P粉1166544952024-03-30 00:57:29

    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.

    reply
    0
  • Cancelreply