Search XML feed's description against a keyword from database - mysql

I'm working on a project where I use XML feeds to get input. I have to filter the items which title and description that matches specific keywords. If an item contains smart phone in title or description, I have to add that item in database under the category "Smart phone".
The query I use here is
$title = $item=>title;
$desc = $item->description;
SELECT cid FROM tbl_keyword WHERE MATCH(keyword) AGAINST ('".$title." ".$desc."' IN
BOOLEAN MODE);
Query returns value but it gets other rows from database like smart watch,smart toys.
I want to know, how to include space based search.
Query have to match the exact keyword.
table looks like
id cid keyword
1 6 smart phone
2 6 iphone
3 7 smart watch
When i get a title as "Smart phones are not essential", query should return only the cid 6.
How to implement it.?

As I know , you can't just search the whole sentence(Smart phones are not essential) against the db to get that exact result .
Two ways to do this :
1.*(Recommended)*You can just break the sentence with space ("Smart", "Phone", "are", "not", "essential")and then apply the following query
select * from tbl_keyword where keyword like "%smart%" or keyword like "%phones%" or keyword like "%are%" or keyword like "%not%" or keyword like "%essential%"
This query will output list of possible entries from the database. From this you will need to compare the result with your query sentence using your programming language.
2.This way will output the entry directly from the database (but this may rule out some of the important entries at worst case).
Breakdown the sentence into single word like this ("Smart", "Phones", "are", "not", "essential")
And then break this sentence with two words like this ("Smart Phones", "Phone are", "are not", "not essential","essential Smart","Smart are","Smart not","Phone not")
And use both of this to retrieve entries from the database (This process will just narrow down the filter)
select * from tbl_keyword where (keyword like "%smart%" or keyword like "%phone%" or keyword like "%are%" or keyword like "%not%" or keyword like "%essential%") and (keyword like "%smart phones%" or keyword like "%phones are%" or keyword like "%are not%" or keyword like "%not essentials%" or keyword like "%essentials smart%")
Hope this will help you ...

Related

Using a SQL wildcard ignoring non-alphanumeric characters in a column

I have a two-column table:
product name
cars ["bmw", "mazda", "ford", "dodge"]
fruit ["lemon", "orange", "lime", "apple"]
I'm using a wildcard to search the product's name column. My question is, is there a way to search a column only by alphanumeric characters and ignore the " and [ ]?
For example, if the user searched bmw the query would be: LIKE '%bmw%' and it would return cars, however if the user searches bmw" and the query is: LIKE '%bmw"% or they enter dodge"] and the query would be LIKE '%dodge"]%" it would want it to not return any results.
My current query:
SELECT product, name FROM `test1` WHERE name LIKE '%bmw%'
It doesn't need to be a wildcard basically, I am after the query only providing the product if the exact name is used but because of the format of the name column it's giving me problems.
You might want to clean the data before it's being added to the query.
e.g.You can use regex to replace unwanted characters / only allow certain characters in a text.
Then you can add that "cleaned" data as a parameter to that query.
As suggested JSON_SEARCH() is what I needed to achieve my desired outcome.
See: https://database.guide/json_search-find-the-path-to-a-string-in-a-json-document-in-mysql/
Specifically Example 5 – Wildcards.

Selecting rows with more than 1 LIKE

My problem here is that, I want to create a simple search engine.
I've got it to work by using 2 queries. The first one searches for the full search term and the second one searches for all of the matches word by word.
For example if You search for "The long lasting i don't know", then it will search for "The long lasting i don't know", then for "The", then "long", and so on.
But I want to use pagination and It would mess up the results, so I need it in 1 query.
Is it achieveable with the IN operator?
You could try using REGEXP as said by #jazkat at Mysql Like multiple values
Your query would be:
SELECT * FROM table WHERE column REGEXP "The long lasting i don't know|The|long|lasting|i|don't|know"
More about REGEXP here: http://www.tutorialspoint.com/mysql/mysql-regexps.htm
You have to use individual like with ORs as follows
select * from tablename where field like "%The long lasting i don't know%" or field like "%The%" or field like "%long%" or field like "%lasting%" or field like "%i%" or field like "%don't%" or field like "%know%"
refer this link Using SQL LIKE and IN together
My answer may not be optimal on the performance side, but here's the idea I got.
You could search only with the word by word (column like '%The%' or column like '%long% etc.). You would surely get the results containing the whole sentence.
Then, you can order it with a case
Order By Case When Column Like "The long lasting i don't know%" Then 1 Other 2 End

How to match keywords with SQL query?

I am building a website that accepts user input keywords and output data that matches. For instance if the user specifies keyword as 'Restaurant pizza' then my database should output such record.
My current table has a column called category and five columns named from keyword1 to keyword5 which contains their specialized area, i.e. 'pizza', 'chicken' or 'bbq' etc.
But I have no idea how to write the SQL query since user may input keywords in any order: category first or with specialized area first.
so such query will surely return no result (given that user input 'Restaurant pizza' for query):
SELECT *
FROM message
WHERE category LIKE 'Restaurant pizza'
OR keyword1 LIKE 'Restaurant pizza'
OR keyword2 LIKE 'Restaurant pizza'
I guess it would be a bad idea splitting input keyword into words then running every word in the WHERE clause to database. but I really do not know how to achieve my goal.
In addition, would you please give me some advice on how to build index in this scenario?
You should create FULLTEXT index on category and keywords columns, then when querying data explode query string by delimeters (space character) and then create query something like:
SELECT * FROM items
WHERE
MATCH (category,keyword1,keyword2,keyword3,keyword4,keyword5)
AGAINST ('pizza')
AND
MATCH (category,keyword1,keyword2,keyword3,keyword4,keyword5)
AGAINST ('restaurant');
You may try this:
SELECT *
FROM message
WHERE `category` LIKE '%Restaurant%'
AND (`keyword1` IN ('Pizza','chicken','bbq')
OR `keyword2` IN ('Pizza','chicken','bbq'))

mysql query to match sentence against keywords in a field

I have a mysql table with a list of keywords such as:
id | keywords
---+--------------------------------
1 | apple, oranges, pears
2 | peaches, pineapples, tangerines
I'm trying to figure out how to query this table using an input string of:
John liked to eat apples
Is there a mysql query type that can query a field with a sentence and return results (in my example, record #1)?
One way to do it could be to convert apple, oranges, pears to apple|oranges|pears and use RLIKE (ie regular expression) to match against it.
For example, 'John liked to eat apples' matches the regex 'apple|orange|pears'.
First, to convert 'apple, oranges, pears' to the regex form, replace all ', ' by '|' using REPLACE. Then use RLIKE to select the keyword entries that match:
SELECT *
FROM keywords_table
WHERE 'John liked to eat apples' RLIKE REPLACE(keywords,', ','|');
However this does depend on your comma-separation being consistent (i.e. if there is one row that looks like apples,oranges this won't work as the REPLACE replaces a comma followed by a space (as per your example rows).
I also don't think it'll scale up very well.
And, if you have a sentence like 'John liked to eat pineapples', it would match both of the rows above (as it does have 'apple' in it). You could then try to add word boundaries to the regex (i.e. WHERE $sentence RLIKE '[[:<:]](apple|oranges|pears)[[:>:]]'), but this would screw up matching when you have plurals ('apples' wouldn't match '[wordboundary]apple[wordboundary]').
Hopefully this isn't more abstract than what you need but maybe good way of doing it.
I haven't tested this but I think it would work. If you can use PHP you can use str_replace to turn the spaces into keyword LIKE '%apple%'
$sentence = "John liked to eat apples";
$sqlversion = str_replace(" ","%' OR Keyword like '%",$sentence );
$finalsql = "%".$sqlversion."%";
the above will echo:
%John%' OR Keyword like '%liked%' OR Keyword like '%to%' OR Keyword like '%eat%' OR Keyword like '%apples%
Then just combine with your SQl statement
SQL ="SELECT *
FROM keywords_table
WHERE Keyword like" . $finalsql;
Storing comma delimited data is... less than ideal.
If you broke up the string "John liked to eat apples" into individual words, you could use the FIND_IN_SET operator:
WHERE FIND_IN_SET('apple', t.keywords) > 0
The performance wouldn't be great - this operation is better suited to Full Text Search.
I'm not aware of any direct solution to that type of query. But Full Text Search is a possibility. If you have a full-text index on the field of interest then a search with OR between each word in the sentence (although I think the OR operator is implied) would find that record ... but it might also find more than you want too.
I really don't think what you are looking for is completely possible but you can look into Full Text Search or SOUNDEX. SOUNDEX, for example, can do something like:
WHERE SOUNDEX(sentence) = SOUNDEX('%'+keywords+'%');
I have never tried it in this context but you should and let me know how it works out.

Use REGEXP in MySQL to match keywords for search engine in random order

I'm trying to use a regular expression to match a user entered search string to a title of an entry in my MySQL database.
For example I have the following rows in a table in my databse:
id title
1 IM2 - Article 3 Funky Business
2 IM2 - Article 4 There's no Business That's not Show Business
3 IM2 - There's no Business That's not Show Business
4 CO4 - Life's a business
When a user searches for "IM Article Business", the following query will be executed (spaces are replaced by "(.*)" using str_replace):
SELECT * FROM mytable WHERE title REGEXP 'IM(.*)Article(.*)Business'
This will return the first 2 rows.
Now, I want it to show the same results when a user uses the same words, but in another order, for example: "Business IM Article". The results MUST contain all words entered, only the order of how the words are entered shouldn't matter.
I couldn't figure out how to do it in any way and hoped regular expressions would be the answer. I've never used them before, so does anybody know how to do this?
Thanks,
Pascal
This isn't something regular expressions are great at. Fortunately, it's something SQL is pretty good at. (I'm going to not use mysql's regexp keyword, which I didn't even knew existed, and instead use the SQL standard "%" glob matching.)
select * from mytable where title like '%IM%' and title like '%Article%' and title like '%Business%'
Now title has to contain all three strings, but you haven't specified an order. Exactly what you want.