Home Database Mysql Tutorial How to Search for Multiple Keywords Across Multiple Columns with Relevance-Based Ordering in Laravel?

How to Search for Multiple Keywords Across Multiple Columns with Relevance-Based Ordering in Laravel?

Nov 06, 2024 pm 02:09 PM

How to Search for Multiple Keywords Across Multiple Columns with Relevance-Based Ordering in Laravel?

Laravel: Searching Multiple Keywords Against Multiple Columns with Relevance-Based Ordering

In Laravel, implementing a search that considers multiple keywords against multiple columns while maintaining relevance can be challenging. This article addresses such a problem, where the search results are ordered based on the frequency of the keywords' occurrence in the specified columns.

Scenario:

The search bar requires three keywords as input, and the search criteria is:

  1. Rows containing all three keywords in both columns (meta_name and meta_description) come first.
  2. Rows containing only the first two keywords in the mentioned columns come second.
  3. Rows containing only the first keyword in the mentioned columns come third.

Database Query:

To achieve the desired result, the database query should combine multiple OR conditions and utilize the whereNotIn function to exclude rows that have already been fetched in previous conditions.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

<code class="php">$word1 = 'word1';

$word2 = 'word2';

$word3 = 'word3';

 

$all = DB::table('posts')

    -&gt;where('meta_name', 'like', "%{$word1}%")

    -&gt;where('meta_name', 'like', "%{$word2}%")

    -&gt;where('meta_name', 'like', "%{$word3}%")

    -&gt;orWhere(function($query) use ($word1, $word2, $word3) {

        $query-&gt;where('meta_description', 'like', "%{$word1}%")

              -&gt;where('meta_description', 'like', "%{$word2}%")

              -&gt;where('meta_description', 'like', "%{$word3}%");

    });

 

$twoWords = DB::table('posts')

    -&gt;where('meta_name', 'like', "%{$word1}%")

    -&gt;where('meta_name', 'like', "%{$word2}%")

    -&gt;orWhere(function($query) use ($word1, $word2) {

        $query-&gt;where('meta_description', 'like', "%{$word1}%")

              -&gt;where('meta_description', 'like', "%{$word2}%");

    })

    -&gt;whereNotIn('id', $all-&gt;pluck('id'));

 

$oneWord = DB::table('posts')

    -&gt;where('meta_name', 'like', "%{$word1}%")

    -&gt;orWhere('meta_description', 'like', "%{$word1}%")

    -&gt;whereNotIn('id', $all-&gt;pluck('id'))

    -&gt;whereNotIn('id', $twoWords-&gt;pluck('id'));</code>

Copy after login

Union and Ordering:

Finally, the $all, $twoWords, and $oneWord results are merged using the union function to obtain the ordered search results.

1

2

3

4

5

<code class="php">$posts = $all-&gt;union($twoWords)-&gt;union($oneWord)-&gt;get(); // check this first

 

# or

 

$posts = $all-&gt;union($twoWords)-&gt;union($oneWord)-&gt;skip($start)-&gt;take($this-&gt;rowperpage)-&gt;get();</code>

Copy after login

This approach ensures that the search results are ordered as per the specified criteria, starting with rows containing all three keywords in the specified columns.

The above is the detailed content of How to Search for Multiple Keywords Across Multiple Columns with Relevance-Based Ordering in Laravel?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Reduce the use of MySQL memory in Docker Reduce the use of MySQL memory in Docker Mar 04, 2025 pm 03:52 PM

Reduce the use of MySQL memory in Docker

How do you alter a table in MySQL using the ALTER TABLE statement? How do you alter a table in MySQL using the ALTER TABLE statement? Mar 19, 2025 pm 03:51 PM

How do you alter a table in MySQL using the ALTER TABLE statement?

How to solve the problem of mysql cannot open shared library How to solve the problem of mysql cannot open shared library Mar 04, 2025 pm 04:01 PM

How to solve the problem of mysql cannot open shared library

What is SQLite? Comprehensive overview What is SQLite? Comprehensive overview Mar 04, 2025 pm 03:55 PM

What is SQLite? Comprehensive overview

Run MySQl in Linux (with/without podman container with phpmyadmin) Run MySQl in Linux (with/without podman container with phpmyadmin) Mar 04, 2025 pm 03:54 PM

Run MySQl in Linux (with/without podman container with phpmyadmin)

Running multiple MySQL versions on MacOS: A step-by-step guide Running multiple MySQL versions on MacOS: A step-by-step guide Mar 04, 2025 pm 03:49 PM

Running multiple MySQL versions on MacOS: A step-by-step guide

What are some popular MySQL GUI tools (e.g., MySQL Workbench, phpMyAdmin)? What are some popular MySQL GUI tools (e.g., MySQL Workbench, phpMyAdmin)? Mar 21, 2025 pm 06:28 PM

What are some popular MySQL GUI tools (e.g., MySQL Workbench, phpMyAdmin)?

How do I configure SSL/TLS encryption for MySQL connections? How do I configure SSL/TLS encryption for MySQL connections? Mar 18, 2025 pm 12:01 PM

How do I configure SSL/TLS encryption for MySQL connections?

See all articles