← Back to MySQL Course | Chapter 19: Advanced & Reference | Lesson 3 of 5

Full-Text Search

Full-text search एक search bar जैसा है जो लंबे text के अंदर words समझता है, वे pages ढूँढते हुए जो आपके type किए का mention करते हैं, सिर्फ exact matches नहीं।
Syntax
sql
FULLTEXT (column_name)

SELECT * FROM table_name
WHERE MATCH(column_name) AGAINST('search text' [IN BOOLEAN MODE]);

एक FULLTEXT Index क्या है?

एक regular index सिर्फ exact values match करता है, लेकिन एक FULLTEXT index किसी text column के अंदर words analyze करता है ताकि आप एक term search कर सकें जो एक लंबे paragraph में कहीं भी दिखे, सिर्फ field की शुरुआत में नहीं।

उदाहरण: What is a FULLTEXT Index?

sql
CREATE TABLE articles (id INT, body TEXT, FULLTEXT (body)) ENGINE=InnoDB;
INSERT INTO articles VALUES (1, 'MySQL supports full text search on text columns');
SELECT * FROM articles WHERE MATCH(body) AGAINST('search');

⚠️ This is MySQL-specific syntax. It cannot run in the browser editor. Practice this on your local MySQL installation.

NATURAL LANGUAGE Mode इस्तेमाल करना

NATURAL LANGUAGE mode एक typical search engine जैसा behave करता है — यह आपके search phrase को interpret करता है, matching words वाली rows ढूँढता है, और automatically results को rank करता है इसके आधार पर कि हर match कितना relevant है।

उदाहरण: Using NATURAL LANGUAGE Mode

sql
CREATE TABLE articles (id INT, body TEXT, FULLTEXT (body)) ENGINE=InnoDB;
INSERT INTO articles VALUES (1, 'MySQL full text search ranks results by relevance');
SELECT *, MATCH(body) AGAINST('full text search' IN NATURAL LANGUAGE MODE) AS relevance
FROM articles;

⚠️ This is MySQL-specific syntax. It cannot run in the browser editor. Practice this on your local MySQL installation.

BOOLEAN Mode इस्तेमाल करना

BOOLEAN mode special operators इस्तेमाल करके explicit control देता है: एक word को plus sign से prefix करना इसे appear करने की माँग करता है, जबकि एक minus sign उस word वाली rows को पूरी तरह exclude करता है।

उदाहरण: Using BOOLEAN Mode

sql
CREATE TABLE articles (id INT, body TEXT, FULLTEXT (body)) ENGINE=InnoDB;
INSERT INTO articles VALUES (1, 'MySQL search'), (2, 'PostgreSQL search');
SELECT * FROM articles WHERE MATCH(body) AGAINST('+search -PostgreSQL' IN BOOLEAN MODE);

⚠️ This is MySQL-specific syntax. It cannot run in the browser editor. Practice this on your local MySQL installation.

Query Expansion Mode

Query expansion mode छोटी, vague searches के लिए designed है — यह आपकी search एक बार चलाता है, top matches से commonly associated words निकालता है, फिर उन extra terms को शामिल करके search फिर चलाता है results broaden करने के लिए।

उदाहरण: Query Expansion Mode

sql
CREATE TABLE articles (id INT, body TEXT, FULLTEXT (body)) ENGINE=InnoDB;
INSERT INTO articles VALUES (1, 'database indexing basics');
SELECT * FROM articles WHERE MATCH(body) AGAINST('database' WITH QUERY EXPANSION);

⚠️ This is MySQL-specific syntax. It cannot run in the browser editor. Practice this on your local MySQL installation.

Performance और FULLTEXT Best Practices

FULLTEXT indexes real overhead add करते हैं, इसलिए इन्हें सिर्फ उन columns पर apply करना उचित है जिन्हें genuinely text search चाहिए, और आप अपनी query results में सीधे हर row को MySQL द्वारा assign किया गया relevance score inspect कर सकते हैं।

उदाहरण: Performance and FULLTEXT Best Practices

sql
CREATE TABLE articles (id INT, body TEXT, FULLTEXT (body)) ENGINE=InnoDB;
INSERT INTO articles VALUES (1, 'MySQL full text search');
SELECT id, MATCH(body) AGAINST('MySQL') AS relevance_score FROM articles;

⚠️ This is MySQL-specific syntax. It cannot run in the browser editor. Practice this on your local MySQL installation.

Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. बड़े data पर search के लिए एक FULLTEXT index के बजाय LIKE '%word%' इस्तेमाल करना।
  2. एक ऐसे column पर MATCH(col) इस्तेमाल करना जो एक FULLTEXT index में नहीं है, जो एक error देता है।
  3. बहुत छोटे words या common stopwords search करना, जिन्हें index ignore करता है।
🔒

Chapter Quiz — Complete all 5 topics to unlock

0/5 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.