mysql search top match should come first - mysql

in the match - against am getting the correct results, there is no problem but the thing i want is the result combination.
Like for "computer graphics" i am getting results for "+computer +graphics" as "computer" alone results and "computer graphics" results and "graphics" results and etc.
Here i want "computer graphics" results first then the other single word match results. How can i bring those first. Help me some one please

you should order by relevance: search for the same query you use in WHERE, call is RELEVANCE and then order by that field.
SELECT MATCH('...') AGAINST ('...') as Relevance
FROM table WHERE MATCH('...') AGAINST('...' IN
BOOLEAN MODE)
ORDER BY Relevance DESC

This can't be done in MySQL fulltext search without a bit of hoop jumping.
You basically need to run the search twice to get your desired results. First, run a boolean fulltext search using double quotes to enclose the exact phrase being searched for. The double quotes in boolean mode will return exact matches only. Once you have those results, then your normal natural-language search. It's the normal, natural language search that is giving you trouble with partial matches. You'll need to manually combine the two search results.
While MySQL fulltext is decent for simple searching needs, it's not a great search solution. Consider something with more power, like Sphinx, Solr / Lucene, or even something like ElasticSearch.

Assuming we're talking about a full-text index:
... ORDER BY MATCH('computer graphics') AGAINST (some,columns) DESC;

Entries in table
what is computer?
what is graphics on computer?
what is computer graphics?
what is graphics?
QUERY : select *,MATCH(field1,field2) AGAINST ("+computer +graphics" IN BOOLEAN MODE) as results from $table where MATCH(field1,field2) AGAINST ("+computer +graphics" IN BOOLEAN MODE) ORDER BY results ASC
IT RETURNS exact results some where in the middle and others are first.
Like
what is computer graphics?
what is graphics on computer?
what is computer?
what is graphics?
How it can be corrected....

Related

Mysql query show no result with some bad words?

I am working on an adult site, for this site I have created an internal research.
For search I use this query:
SELECT SQL_CALC_FOUND_ROWS
id_photo, title, description, model, data_ins,
MATCH(title, description, model) AGAINST('".trim(strtolower(addslashes($_GET['q'])))."') as score
FROM ".$prefix."photo
WHERE MATCH(title, description, model) AGAINST('".trim(strtolower(addslashes($_GET['q'])))."')
ORDER BY score DESC LIMIT ".$start.", ".$step."
Everything works smoothly and without php or mysql errors, but the client pointed out a strange thing to me.
eg :
searching for the word starting with "c" and ending with "ck" the
query returns no results.
searching for the word starting with "d"
and ending with "ck" the query returns the correct results.
I use something similar to this to verify if there are results:
$photo_query_id = $db->prepare("my query");
$photo_query_id->execute();
if($photo_query_id->rowCount() < 1){
//...
}
The two words are both used hundreds of times in both titles and descriptions, so why does mysql sometimes prefer not to show results?
Is there a list of bad words in some mysql config file that is blocking queries? And in case where do I find it and how do I modify it?
Use a BOOLEAN MODE search or use the InnoDB database engine for your table. When you do a natural language search against a MyISAM full-text index, words that appear in more than 50% of the rows are treated as stopwords.
From the documentation:
The 50% threshold can surprise you when you first try full-text searching to see how it works, and makes InnoDB tables more suited to experimentation with full-text searches. If you create a MyISAM table and insert only one or two rows of text into it, every word in the text occurs in at least 50% of the rows. As a result, no search returns any results until the table contains more rows. Users who need to bypass the 50% limitation can build search indexes on InnoDB tables, or use the boolean search mode explained in Section 12.10.2, “Boolean Full-Text Searches”]2.

MySQL Full-Text search query with MATCH return strange result order

I need to query a table using the following query:
SELECT cards.name, MATCH(`cards.name`) AGAINST("Swiftfoot Boot") AS relevance
FROM cards
WHERE MATCH(`cards.name`) AGAINST("Swiftfoot Boot")
ORDER BY relevance DESC;
Here is my db-fiddle link with schema and records.
If you run the query you can see that the first result with the highest relevance is "Boot Nipper", but I expect to have "Swiftfoot Boots" (note the 's' at the end). Any idea why this strange order result and how to fix it?
Natural Language Full-Text Searches
Every correct word in the collection and in the query is weighted according to its significance in the collection or query. Thus, a word that is present in many documents has a lower weight, because it has lower semantic value in this particular collection. Conversely, if the word is rare, it receives a higher weight. The weights of the words are combined to compute the relevance of the row. This technique works best with large collections.
DEMO

Fulltext match search in natural language mode

I am attempting a fulltext search in mysql. I expect that when I pass in a string, I will receive ranked by relevancy when I use [Natural Language Mode]mysql - fulltext index - what is natural language mode .
Here is how I created the index: CREATE FULLTEXT INDEX item_name ON list_items(name);
When I use LIKE, I receive results, except I want to order them by relevancy. Hence, the fulltext search.
Here is the query I have using LIKE: SELECT name FROM list_items WHERE name LIKE "%carro%";
Which results in Carrots, Carrots, Carrots etc.
Here is the query I have attempting the MATCH search: SELECT name FROM list_items WHERE MATCH(name) AGAINST('carro' IN NATURAL LANGUAGE MODE); Which returns no results.
I am basing my query on the selected answer on this post: Order SQL by strongest LIKE?
And this page: https://www.w3resource.com/mysql/mysql-full-text-search-functions.php
Even when I run the query without Natural Language Mode or even in Boolean Mode, I don't get any results. What am I missing?
You seem to want to use * as a wildcard. For that you need to use "boolean" mode rather than "natural language". So, this might do what you want:
SELECT name
FROM list_items
WHERE MATCH(name) AGAINST('carro*' IN BOOLEAN MODE)
This still produces a relevance ranking, although it might not be exactly the same as natural language mode.
Also note that this will get matches such as "carrouse".
I don't think that MySQL supports synonym lists for full text search, so this is tricky to avoid (although like filtering along with the full text filtering might suffice).

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