RGS
RGS

Reputation: 4253

improve my search engine in php - mysql

For example, if I have this:

Barack Hussein Obama is the 44th and current President of the United States.

I want to search for Barack Obama, my search will not bring any results.

php

$q=$_POST['search'];
$q= '%' . $q . '%';

$stmt = $mysqli->prepare("

select id, titulo, fotos from posts where titulo like ? order by id desc LIMIT 150

");
$stmt->bind_param('s', $q);

mysql:

select id, titulo, fotos from posts where titulo like '%barack obama%' order by id desc LIMIT 150

what can I do to search for barack obama or barack president and have results?

Upvotes: 1

Views: 138

Answers (1)

georoot
georoot

Reputation: 3617

What you want is not a simple sql query. Let me try to make this as simple as possible. I am guessing you don't have an idea about natural language processing Here is a nice library for the same. Now you have a a search query and a lot of content to search from.

First you find the subject of the query. For example a query like Who is barack should not search for who and is but only for barack. This is going to return you a couple of keywords that you can use.

Next you need to find them in your document and rank the results according to priority. A lot of factors can play affect in the same, for the sake of simplicity just make a heatmap of keyword in the document. Like the document with more barack will have a higher priority.

In case of multiple keywords like barack and president i guess the simplest way that doesn't go into machine learning would be to take individual union of them.

Now search is not something that you can do on request. This is a continuous process and i would recommend to have a cron job that constantly parses though each document and extracts keywords from them. Save those keywords against the document so that you can match them according to query later.

Lastly this is just a simple method for the same and there are a lot more complicated methods for the same. You can , if you want collect user data in some way to judge if the result was usefull and use it next time to put it on a higher search rank. Here is something that i found interesting and i guess will give you a better understanding of the process.

Easy and won't work well

If you want something easier you can just explode the search term and run sql query against all of them and finally give higher priority to content that returned in more number of searches.

Upvotes: 1

Related Questions