I have a problem with the query:
Select * From table Where MATCH(col1) AGAINST('mystring' IN BOOLEAN MODE) OR MATCH(col2) AGAINST('mystring' IN BOOLEAN MODE)
because it is not using the fulltext index, as the query:
Select * From table Where MATCH(col1) AGAINST('mystring' IN BOOLEAN MODE) AND MATCH(col2) AGAINST('mystring' IN BOOLEAN MODE)
does.
Both col1 and col2 has a fulltext index, but when using OR instead of AND, mysql performs a full table search instead of using one of the indexes.
How can I select records with (at least) one of the 2 columns matching 'mystring' using indexes?
Just try to select each set by isolated query (with only one filter) and than intersect results with UNION.
Another form:
SELECT * FROM table WHERE MATCH (col1, col2) AGAINST ('mystring' IN BOOLEAN MODE)
Related
I am trying to make a query where I select all the rows that do not contain a specific word, for this I have a fulltext type index in this column, try the following bolt works:
SELECT *
FROM products
WHERE MATCH(title) AGAINST(' -Dolo' IN BOOLEAN MODE)
So how can I perform this search?
If I have understood you correctly you want to find all the rows from the table that do not contain a word'Dolo'.
Well you can use NOT operator for that.
SELECT *
FROM products
WHERE NOT MATCH(title) AGAINST('Dolo');
Here is a DEMO.
Also, you can use it like this(because as the OP has asked: "if the whole word is "dolorem", would this query work?"):
SELECT title as Title
, MATCH(title) AGAINST('Dolo*' IN BOOLEAN MODE) as Score
FROM products
WHERE MATCH(title) AGAINST('Dolo*' IN BOOLEAN MODE) = 0;
* is a wildcard.
Other signs are described here: https://dev.mysql.com/doc/refman/8.0/en/fulltext-boolean.html
Here is the DEMO for the second example.
The MySQL version is 5.1.40
The table is MyISAM(only changed the table to MyISAM)
The type of table_column(name) is varchar
When I apply the fullText search, it doesn't work.
Below sql only return data 'eeee', will not return such as 'eeeefff' or 'ffeeee'
select name from test where match(name) against('eeee' in boolean mode);
MySQL's full text search lets you search for keywords beginning, but not ending, in a certain substring. So, to find all words beginning with eeee we can try:
SELECT name
FROM test
WHERE MATCH(name) AGAINST('eeee*' IN BOOLEAN MODE);
I added a FULLTEXT index to a table like so:
ALTER TABLE TEST ADD FULLTEXT(name, descrip, notes);
The TEST table has 100 rows. I updated the descrip column with 'branch' in one row and 'Branches' in another row.
Then I run
SELECT * FROM TEST WHERE MATCH(name, descrip, notes) AGAINST ('branch' IN BOOLEAN MODE);
The result returned only rows that contain 'branch'. 'Branches' is not included in the result, even though it is comprised of the word that was searched. How to include 'Branches'? I want to avoid using LIKE.
In boolean mode you can use a wildcard:
AGAINST ('branch*' IN BOOLEAN MODE);
Here goes your query. You need to use wildcard.
SELECT * FROM TEST WHERE MATCH(name, descrip, notes) AGAINST ('branch*' IN BOOLEAN MODE);
Relevance score of MATCH..AGAINST is not working.
Created one dummy table which has 2 rows.
Dummy Table
Row1=> 'Leela Hayat Marriot'
Row2=> 'Americas Best Value'
Query1:
SELECT MATCH (col1) AGAINST ('Leela* Hayat*' IN BOOLEAN MODE) AS relevance
FROM table1
WHERE MATCH (col1) AGAINST ('Leela* Hayat*' IN BOOLEAN MODE);
Result:
relevance
2
Query2:
SELECT MATCH (col1) AGAINST ('Americas* Best*' IN BOOLEAN MODE) AS relevance
FROM table1
WHERE MATCH (col1) AGAINST ('Americas* Best*' IN BOOLEAN MODE);
Result:
relevance
1
Query1 is working fine but why is query 2 not working?
Why am I getting relevance 1 instead of 2 in Query2 as Americas and Best both are present in column.
Thanks
http://dev.mysql.com/doc/refman/5.7/en/fulltext-stopwords.html
'BEST' is listed in stopword list.
http://dev.mysql.com/doc/refman/5.7/en/server-system-variables.html#sysvar_ft_stopword_file
ft_stopword_file:
The file from which to read the list of stopwords for full-text searches on MyISAM tables. The server looks for the file in the data directory unless an absolute path name is given to specify a different directory. All the words from the file are used; comments are not honored. By default, a built-in list of stopwords is used (as defined in the storage/myisam/ft_static.c file). Setting this variable to the empty string ('') disables stopword filtering.
I disabled stopwords list and now query 2 is working fine.
Thanks for help.
I can search for rows with both foo and bar in the col1/col2 using match against:
SELECT col1, col2
FROM some_table
WHERE MATCH (col1,col2)
AGAINST ('+foo +bar' IN BOOLEAN MODE);
But suppose I want to search for the exact phrase "foo.bar" (with a full-stop in the middle). On the docs page for Boolean Full-Text Searches, it doesn't mention full-stop being an operator, so I thought I could use:
AGAINST ('+foo.bar' IN BOOLEAN MODE);
However, this returns the same results as:
AGAINST ('+foo.couldBeAnything' IN BOOLEAN MODE);
AGAINST ('+foo' IN BOOLEAN MODE);
AGAINST ('+foo.*' IN BOOLEAN MODE); #Note you would expect this to look for instances of foo. followed by something, rather than just the same as foo
.
Why isn't this working as I expect? and how can I match against for foo.bar?
I don't have a fulltext table readily available to test this out, but I believe this should work:
SELECT col1, col2
FROM some_table
WHERE MATCH (col1,col2)
AGAINST ('+\"foo.bar\"' IN BOOLEAN MODE);
To make this work, you need to suround your literal by a double quote: "bar.foo",
because the point is probably equivalent to or operator.