I have a search engine in PHP. When a search normally it's OK. Search text is 'company', and in the database there is 'company' in the field...
The problem is when the search text is &company or -company and the data is &company or -company there is no match. why?
problem with the - and & string...
Try putting your search terms in quotes. This should help mysql know you mean those characters literally in fulltext search:
SELECT * FROM tablename MATCH (company) AGAINST ('"&company"' IN BOOLEAN MODE)
SELECT * FROM tablename MATCH (company) AGAINST ('"-company"' IN BOOLEAN MODE)
If you are using FullText Search then the & and - are reserved characters. I could not find any nice solution to this problem. What I did is just remove the special character and run the full text search. For example if they are looking for At&t I run a search for "AT" "T", but if you have noise words At and A are in there and you will not get any results.
Another solution is to detect when they are requesting a special character and run a LIKE '%&Company%' search instead of a full text search, but this will affect the performance of the query.
Related
I have a query,
e.g.
name column have "Rodrigue Dattatray Desilva".
I want to write a query in such a way that,
If I search for 'gtl' and match anywhere in string it should show the result.
I know in PHP I can apply the patch like '%g%t%l%'.
But I want to know MySql way.
Note: I can search for anything, I am just giving above an example.
EDIT:
create table Test(id integer, title varchar(100));
insert into Test(id, title) values(1, "Rodrigue Dattatray Desilva");
select * from Test where title like '%g%t%l%';
Consider the above case. Where "gtl" is string I am trying to search in the title but search string can be anything.
gtl is string where it exists in the current title but not in sequence.
The easy answer is that you need an extra wildcard:
select * from Test where title like '%g%t%l%';
The query you posted does not have a wild card after the 'l', so would only match if the phrase ended with 'l'.
The more complicated answer is that you can also use regular expressions, which give you more power over the search.
The even more complicated answer is that performance of these string matching queries tends to be poor - the wild cards mean that indexes are usually ineffective. If you have a large number of rows in your table, full-text searching is much faster.
You can do the same in Mysql too.
You can use the keyword like in MySql.
% - The percent sign represents zero, one, or multiple characters
_ - The underscore represents a single character
I am doing a search function in php, and I am allowing BOOLEAN search, but when I enter text containing chars like #, the query fails.
For example, when I search for #everyone, it throws an error.
I tried to solve this by adding doublequotes, but it doesn't work as expected, since for the search #everyone, it works but returns rows containing everyone and #everone.
I would like to know how we can search for words containing special chars in mysql full text search
Here's my query (simplified) :
SELECT * FROM messages WHERE MATCH(body) AGAINST ('#everyone' IN BOOLEAN MODE)
By default, MySQL does not treat '#' as a valid character for a word. If you want to treat '#' it, then review the documentation on the subject.
After you have made the changes, then you will need to re-build your index.
This may be a newbie question, as I'm not an expert in SQL. However, couldn't find the answer using Google.
I have a table called record_fields which contains the majority of my system's content, which I want to search in. The content cell is defined as LONGTEXT as it can include extremely long input.
Originally, I used (simplifying the query a bit for clarity sake):
SELECT * FROM record_fields WHERE LOWER(content) LIKE LOWER('%{$keyword}%')
Execution time aside, this query has one major issue. If I search for the term "post" it will return all content which has words like "poster", "posting" and others. I wanted to add a FULLTEXT search.
Now the query looks like this (again, simplified):
SELECT * FROM record_fields WHERE MATCH (content) AGAINST ('{$keyword}')
However, this is still problematic. With MATCH, if my system's users search for the words "Bank of America", for example, all records that either have the word "Bank" and "America" will be returned.
TL;DR - my question is this:
how do I use MATCH to search for exact phrases with space in them?
Any help would be highly appreciated, thanks in advance!
%{keyword}% matches all text sub-strings that include your keyword anywhere in the string. MATCH usually takes all keywords in the match string as individual search terms, and matches against each. You can use boolean mode and use a + symbol before each required keyword. Take a look at the MySQL reference for this.
Edited the answer to reflect Idan's response in not getting the results from the suggested %keyword solution.
You can use Match Against With Boolean Mode and you can put your input string inside '"{$keyword}"'.
Check last example in below link
https://dev.mysql.com/doc/refman/5.5/en/fulltext-boolean.html
SELECT * FROM record_fields WHERE MATCH (content) AGAINST ('"{$keyword}"' IN BOOLEAN MODE )
I've an trouble when using full text search keyword has space to match the column with name has no space. Ex: The column name is StackOverFlow, the keyword is Stack Over Flow, I tried to using this query but didn't work:
SELECT * FROM table
WHERE MATCH(name) AGAINST('%Stack%Over%Flow%').
Can anyone help to from this? Thank you.
I'm afraid you can't do it like this. You'll need to remove spaces in php or create some kind of a mapping dictionary for similar search terms.
I have following match against query which searches records from database table based on search phrase.
SELECT * FROM My_Table WHERE MATCH (catchall) AGAINST ('"horse"' IN BOOLEAN MODE)
This query works properly. When search phrase contains special characters like '(' etc It just skips such special characters.
If i search for "(horse)" it gives me same result as it gives for "horse".
SELECT * FROM My_Table WHERE MATCH (catchall) AGAINST ('"(horse)"' IN BOOLEAN MODE)
Does it mean match against query doesn't work with special characters or am i missing something. Please suggest. Thanks.
I tried by removing IN BOOLEAN MODE from the query but it didn't work.
from the documentation:
Parentheses group words into subexpressions. Parenthesized groups can be nested.
if you want to treat prenthes as "word chars", there are two possibilitys:
If you want to change the set of characters that are considered word
characters, you can do so in two ways. Suppose that you want to treat
the hyphen character ('-') as a word character. Use either of these
methods:
Modify the MySQL source: In myisam/ftdefs.h, see the true_word_char()
and misc_word_char() macros. Add '-' to one of those macros and
recompile MySQL.
Modify a character set file: This requires no recompilation. The
true_word_char() macro uses a “character type” table to distinguish
letters and numbers from other characters. . You can edit the
contents in one of the character set XML files to specify
that '-' is a “letter.” Then use the given character set for your
FULLTEXT indexes.
After making the modification, you must rebuild the indexes for each
table that contains any FULLTEXT indexes.
a third way would be to not use MATCH ... AGAINST at all and use LIKE instead - but this might get complicated (if you want to use the other operators of ful-text-searches such as +/-) and slow down your query.