how to use Fuzzy look up to find the sentence in SQL? - mysql

search term=['ISBN number on site']
the variable(column): sentence, in MySQL table. It consist many different sentence.
the sentence I want to look for is
"The AutoLink feature comes with Google's latest toolbar and provides links in a webpage to Amazon.com if it finds a book's ISBN number on the site."
However, When I use the following statement:
SELECT * FROM testtable
where Sentence like "%ISBN number on site%" ;
I am not able to get the result. This is because the search term("ISBN number on site") is lack of one word("the") compare with the sentence.
How to change my statement in order to get the sentence I want? thanks.
Assume that We do not change the search term=['ISBN number on site']

This is not a simple question. Your best bet is to use some type of fulltext search. Fulltext search can be configured to have stopwords (words that are omitted from search - like the word the) and can have a minimum word length limit as well (words with less than certain characters long are also omitted from the search.
However, if you simply use
SELECT * FROM testtable
WHERE MATCH (sentence)
AGAINST ('ISBN number on site');
Then MySQL will return not just the record with the value you were looking for, but the records that have some of the words only, and in different order. The one you showed will probably be one of the highest ranking one, but there is no guarantee that it will be highest ranked one.
You may want to use Boolean fulltext search and prepend + to every search word to force MySQL to return those records only that have all the search words present:
SELECT * FROM testtable
WHERE MATCH (sentence)
AGAINST ('+ISBN +number +on +site' IN BOOLEAN MODE);
But, on should either be a stopword (it is on the default stipword lists) or should be shorter that the minimum word length, so should be omitted from the search expression (you will not get back any results):
SELECT * FROM testtable
WHERE MATCH (sentence)
AGAINST ('+ISBN +number +site' IN BOOLEAN MODE);
I know that this requires alteration of the search expression, however this will get you the best results using MySQL's built-in functionality.
The alternative is to use other fulltext search engines, such as sphinx to perform the search for you.

Try:
SELECT * FROM testtable where Sentence like '%ISBN number on%site%' ;
The wildcard can go in the middle of a string too.

Related

Exact Match search from Mysql FullText Search

By Using Full Text Search not searching the exact value for multiple words.
This is the Query
SELECT * FROM csv WHERE match(data) against('"TMN PANTAI"' IN BOOLEAN MODE)
Its showing the result but its searching with "TMN" and "PANTAI" and "TMN PANTAI"
How can i search the exact match using "TMN PANTAI"?
FULL-TEXT doesn't take space into consideration, that is why you cannot use it.
If you still want to take advantage of full-text index, you can shorten up the resultset by filtering using match against query and add an additional LIKE condition. This will be efficient than querying with LIKE on the whole table, since the LIKE will now have lesser records to filter.
SELECT * FROM csv WHERE match(data) against('+TMN +PANTAI' IN BOOLEAN MODE)
AND data like '%TMN PANTAI%'

How to speed up search MySQL? Is fulltext search with special characters possible?

I have strings like the following in my VARCHAR InnoDB table column:
"This is a {{aaaa->bbb->cccc}} and that is a {{dddd}}!"
Now, I'd like to search for e.g. {{xxx->yyy->zzz}}. Brackets are part of the string. Sometimes searched together with another colum, but which only contains an ordinary id and hence don't need to be considered (I guess).
I know I can use LIKE or REGEXP. But these (already tried) ways are too slow. Can I introduce a fulltext index? Or should I add another helping table? Should I replace the special characters {, }, -, > to get words for the fulltext search? Or what else could I do?
The search works with some ten-thousand rows and I assume that I often get about one hundred hits.
This link should give you all the info you need regarding FULLTEXT indexes in MySQL.
MySQL dev site
The section that you will want to pay particular attention to is:
"Full-text searching is performed using MATCH() ... AGAINST syntax. MATCH() takes a comma-separated list that names the columns to be searched. AGAINST takes a string to search for, and an optional modifier that indicates what type of search to perform. The search string must be a string value that is constant during query evaluation. This rules out, for example, a table column because that can differ for each row."
So in short, to answer your question you should see an improvement in query execution times by implementing a full text index on wide VARCHAR columns. Providing you are using a compatible storage engine ( InnoDB or MyISAM)
Also here is an example of how you can query the full text index and also an additional ID field as hinted in your question:
SELECT *
FROM table
WHERE MATCH (fieldlist) AGAINST ('search text here')
AND ( field2= '1234');

How to give more weight for title than other fields in Mysql Full Text Search Index

I am implementing mysql full text search, here I am facing one issue, results displaying but not working as I expect. When I perform search for 'Samsung Galaxy S6' it is coming at second position. I need title has more weight than other fields.
If search string in title found, it will come first, and one more thing when I perform only with "S6" results are strange 0 results showing, how should I do this. Please help.
My Sqlfiddle link
I cannot provide an answer to the weighting of results if the search term was found in the title. I can provide the reason as to why searching for just "S6" returns 0 results, this is a MySQL limitation, where it's minimum word length is 4 characters by default, this can be changed in my.ini, look for the ft_min_word_len (and inversely ft_max_word_len).
This is assuming you're using the MATCH() of course.
Further reading: MySQL Natural Language Full Text Search
In order to apply a different weight for the fields text and title, you have to create an separate index on each column. Then you use both indices with a weight factor.
select *
from mytable
where match(title) against 'word'
or match(text) against 'word'
order by
2 * match(title) against 'word'
+ match(text) against 'word'
desc
Choose an appropriate weighing factor (2 might be good).

Results missing from a mysql FULLTEXT boolean mode search

I'm trying to use a FULLTEXT index in order to facilitate searching for forum posts. It's not working in the way I expect, and I'm trying to understand why not.
For example, I know there is exactly one post which contains the phrase "haha and i got three", so I perform the query
select * from forum_posts where
match(message) against ('"haha and i got three"' in boolean mode);
and as I expect, I find the single post which includes this phrase. Hooray!
But then I perform the related query:
select * from forum_posts where
match(message) against ('"and i got three"' in boolean mode);
and get no results. In fact, simply searching for the word "three":
select * from forum_posts where
match(message) against ('three' in boolean mode);
yields no results either.
What could be going on?
I think you need to learn about stop words and minimum word length.
My default, MySQL ignores stop words in the full text index. Here is a list of them. "And I got three" is all stop words.
In addition, by default, MySQL ignores words with less than for characters. This is controlled by the parameter. This is explained in more detail here.
It sounds like you will want to change the stop word list and change the minimum word length and rebuild the index.

Can MySQL fulltext search return an index(position) instead of a score?

I would like to use the position/index found by the Match...Against fulltext search in mysql to return some text before and after the match in the field. Is this possible? In all the examples I have seen, the Match...Against returns a score in the select instead of a location or position in the text field of which is being searched.
SELECT
random_field,
MATCH ($search_fields)
AGAINST ('".mysql_real_escape_string(trim($keywords))."' IN BOOLEAN MODE)
AS score
FROM indexed_sites
WHERE
MATCH ($search_fields)
AGAINST ('".mysql_real_escape_string($keywords)."' IN BOOLEAN MODE)
ORDER BY score DESC;
This will give me a field and a score...but I would like an index/position instead of (or along side) a score.
Fulltext searching is a scoring function. its not a search for occurrence function. In other words the highest scoring result may not have a starting position for the match. As it may be a combination of weighted results of different matches within the text. if you include query expansion the search for word/s may not even appear in the result!
http://dev.mysql.com/doc/refman/5.0/en/fulltext-query-expansion.html
I hope that makes some sense.
Anyway your best bet is to take the results and then use some text searching function to find the first occurrence of the first matching word. My guess is that would be best suited to a text processing language like perl or a more general language like php or what ever language you are using to run the query.
DC