Mysql Select Query with match and against issue - php

This is my code to perform a search
SELECT d.deal_id,
d.deal_title,
d.friendly_url
FROM wp_deals AS d
WHERE MATCH (d.deal_title) AGAINST ('Historic China eight day tour' IN BOOLEAN MODE)
GROUP BY d.deal_id
It works fine.and give 14 results.
one of them is
Great Sandy Straits two night Natural Encounters Tour for two with sunset cruise & more. Up to $1,071 off!
But when I search for "with" or "more" it becomes
SELECT d.deal_id,
d.deal_title,
d.friendly_url
FROM wp_deals AS d
WHERE MATCH (d.deal_title) AGAINST ('more' IN BOOLEAN MODE)
GROUP BY d.deal_id
SELECT d.deal_id,
d.deal_title,
d.friendly_url
FROM wp_deals AS d
WHERE MATCH (d.deal_title) AGAINST ('with' IN BOOLEAN MODE)
GROUP BY d.deal_id
and does not give any result although with and more both are their. I am not an expert with this type of search query.
But
When i search with "tour" that is also their it works fine.whats going on their.
could not understand as "tour","with" and "more" all contains four letters and also their in title.
You People are Genius..
What you suggest so it will workout.
Thanks in advance.

In Boolean full-text searches the stopword list applies and common words such as “some” or “then”... are stopwords and do not match if present in the search string : http://dev.mysql.com/doc/refman/5.5/en//fulltext-boolean.html
Stopword list : http://dev.mysql.com/doc/refman/5.0/en/fulltext-stopwords.html
The best known solutions are disable the list or add / remove values ​​from it : How to reset stop words in MYSQL?
Also :
SELECT d.deal_id,
d.deal_title,
d.friendly_url
FROM
wp_deals AS d
WHERE MATCH (d.deal_title) AGAINST ('+more*' IN BOOLEAN MODE)
GROUP BY d.deal_id

Related

Can you combine a simple MYSQL Statement with a FullText Statement

I've built an internal DB / Search Engine for art creatives. I'm trying to create a search criterion where you can query one column in the database and also search several columns in the database for a phrase search using FullText Search. The example of a search query might be: November {and} Black Friday. November would search for creatives matching the created_for column and the black friday would search headline, subheadline and additional_text columns with a fulltext search. Any ideas of how to accomplish this would be really helpful!
SELECT
(SELECT * FROM headlines WHERE created_for = '$searchString' AND image_slug <> '')
(SELECT *, MATCH(headline) AGAINST('$fullText' IN BOOLEAN MODE) AS MultiScore, MATCH(subheadline, additional_text) AGAINST('$fullText' IN BOOLEAN MODE) AS MultiSecondScore
FROM `headlines`
WHERE MATCH(headline, subheadline, additional_text) AGAINST('$fullText' IN BOOLEAN MODE))
I've tried adding a UNION statement before the second Select statement, but I get an error message saying the columns don't match. Not sure what I've got wrong here, but thanks in advance for your help!
Use AND in the WHERE clause.
SELECT *, MATCH(headline) AGAINST('$fullText' IN BOOLEAN MODE) AS MultiScore, MATCH(subheadline, additional_text) AGAINST('$fullText' IN BOOLEAN MODE) AS MultiSecondScore
FROM headlines
WHERE created_for = '$searchString'
AND image_slug <> ''
AND MATCH(headline, subheadline, additional_text) AGAINST('$fullText' IN BOOLEAN MODE)
UNION would get results that match either of the criteria, not both of them. And when you use UNION, both subqueries have to return the same number of columns -- you would have to add extra columns to the first query to match the MultiScore and MultiSecondScore columns of the first query.

MySQL: Help to search each word in multiple columns as case insensitive

I have a table that contains: product, cost, comment.
Id product cost comment
1 Tires Rex 10 Fast movement good quality
2 Bone Maxx Centri 110 Clean and soft movement
3 Engine damaged 20 Damaged
The outcome I want: User can searches multiple words and the query has to find the items with those words as case insensitive.
For example, user searches: Buy clean tires for cars
The query output has to show products with Id 1 and 2.
Why?
Because the word Clean matches the comment in product with id 2
Because the word tires matches the product with id 1
I tried with:
SELECT * FROM `inventory` WHERE MATCH (product) AGAINST ('Buy* clean* tires* for* cars*' IN BOOLEAN MODE);
But that only works for 1 column and is case-sensitive.
I want it searches in multiples columns as case-insensitive.
Any help will be appreciated!
Use the following query, it's tested and returns first 2 rows:
SELECT * FROM `inventory` WHERE MATCH (product, comment) AGAINST ('Buy* clean* tires* for* cars*' IN BOOLEAN MODE);
I just updated my system using yum update and now is working fine.
Maybe that was the problem.
The problem now is that, is case sensitive. If I search "Clean" it works but if I search "clean" it does not.

MATCH (somefeald) AGAINST ('+".$_GET['search']."' IN BOOLEAN MODE) not work

Why does this SQL query return a blank result?
select
webs.title,webs.disc,webs.logo,webs.id
from
webs
JOIN rel ON rel.webid=webs.id
JOIN catagory on catagory.id=rel.catid
where
webs.app!=0
AND MATCH (webs.title,webs.city,webs.state,webs.url,catagory.catname) AGAINST ('+".$_GET['search']."' IN BOOLEAN MODE)
This is a query for multiples searches in multiple tables and fields.
I also want to order the result by the best matching word.
If you want to use "MATCH() ... AGAINST", first create fulltext index on the field which you want in match.

MySQL fulltext search Boolean mode confusion

I'm getting a bit confused when trying to set up a search utilizing fulltext search in boolean mode. Here is the query I'm using:
$query = "SELECT *,
MATCH(title) AGAINST('$q' IN BOOLEAN MODE) AS score
FROM results
WHERE MATCH(title) AGAINST('$q' IN BOOLEAN MODE)
ORDER BY score DESC";
When I run a search for +divorce+refinance, the results returned are:
1) Divorce: Paying Off Spouse = Rate/Term Refinance
2) Divorce - What to Look Out For Regarding Divorced Borrowers
Am I right in thinking that the second result should not be appearing, as it does not have both words? If not, how can I create that functionality?
Maybe I am mistaken, but if you search this string +divorce+refinance you get a weird result. If you want to search both words, your should search for +divorce +refinance (with a space between).
I tested it and it returns only one row:
Divorce: Paying Off Spouse = Rate/Term Refinance
Your problem relates to the create a prioritized boolean query and for this type of query one has to go in depth of Boolean search and to now how the Boolean search is performed. In simple words let me explain you why the second number result of result is shown.
Once should first understand what does Boolean means in programming?
It means either condition is true or false i,e 0 to 1.
Now let me explain for the Boolean search is performed? You have given two words. Let us search the row by row in Boolean mode. Search engine start and searches the row by row now where ever the First word is found, it makes the record true and give score as 1 to the rows in which the first word is found and also prepare the numbers of words found in the row.
Now it moves the next word and do the same process gives the record True and makes a list of records wherever the word is found and also prepare the number of words found in the row.
Now there are two rows of results are available and they are clubbed and with the priority is given to the words with the maximum number of words and row here is the main problem lies.
Example
First >>> total nos. >> Second >> total nos. >>> Final >> row
Word >>> Results >> Word >>>> of words > > > Results >>no >>Answer
1 >>>>>>>> 2 >>>>>>>>1>>>>>>>>>1>>>>>>>>1.33>>>> 1 >>> 1.33
0 >>>>>>>> 0 >>>>>>>>2>>>>>>>>>2>>>>>>>>1.25>>>> 2 >>> 1.25
0 >>>>>>>> 0 >>>>>>>>1>>>>>>>>>0>>>>>>>>1.25>>>> 3 >>> 1
While clubbing two results lists when true added with false then result is true, as if you add 1 + 0 = 1 and the results are should with value more than 1. So, while scoring the relevancy to the words found it is always found that the search engine shows the results where it found any word.
Scoring relevancy queries are done in two types either ignore the scores which are equal to one and only do calculations on the records who's score is greater than 1. Second is to make such a query that it never shows the records equal to one. As in your case you can so the below things also to get the correct results for two words:
SELECT *, ( (1.3 * (MATCH(title) AGAINST ('+term +term2' IN BOOLEAN MODE))) + (0.6 * (MATCH(text) AGAINST ('+term +term2' IN BOOLEAN MODE))) ) AS score FROM results WHERE ( MATCH(title, text) AGAINST ('+term +term2' IN BOOLEAN MODE) ) HAVING relevance > 0 ORDER BY relevance DESC;
I know that using the word HAVING make the query little slow but there is no other solution available. Hope this solves your query.

PHP search on keywords

I have a table in a database with records containing keywords as well as other data. What would be a logical way to create a search function that would allow people to search based on keywords, and order results based on the number of matched keywords?
Mysql provide FULLTEXT search options. Check this link mysql full text search. These search results will be sorted according to best match. it also has support for boolean mode and NATURAL LANGUAGE MODE(default). You need to add FULLTEXT index on search column.
Here is the query that will work for you.
SELECT *, MATCH (ab,cd) AGAINST ('sample text' IN BOOLEAN MODE) AS relevancy
FROM table_name
WHERE MATCH (ab,cd) AGAINST ('sample text' IN BOOLEAN MODE)
ORDER BY relevancy DESC;

Categories