patternsqlMinor
Calculate MATCH() AGAINST() scores from UNIFIED QUERY not FOR EACH TABLE
Viewed 0 times
eachtablequerymatchagainstcalculatescoresforfromnot
Problem
I am trying to have a score for entire section of SELECT statements
In such case scores are per table + they are not ordered by relevance
But I tried this method, which is working but is not worth of production
Above code is disliked because scores there are per table, they are joined and ordered. A bad approach.
So I tried to
Above statement is perfect for me, but it does not work because
My question is how to proceed to get my engine working.
SELECT *,MATCH(`result`) AGAINST('keyword') as `score` FROM `table1` WHERE MATCH(`result`) AGAINST('keyword')
UNION
SELECT *,MATCH(`content`) AGAINST('keyword') as `score` FROM `table2` WHERE MATCH(`content`) AGAINST('keyword')
UNION
SELECT *,MATCH(`text`) AGAINST('keyword') as `score` FROM `table3` WHERE MATCH(`text`) AGAINST('keyword')In such case scores are per table + they are not ordered by relevance
But I tried this method, which is working but is not worth of production
SELECT * FROM (
SELECT *,MATCH(`result`) AGAINST('keyword') as `score` FROM `table1` WHERE MATCH(`result`) AGAINST('keyword')
UNION
SELECT *,MATCH(`content`) AGAINST('keyword') as `score` FROM `table2` WHERE MATCH(`content`) AGAINST('keyword')
UNION
SELECT *,MATCH(`text`) AGAINST('keyword') as `score` FROM `table3` WHERE MATCH(`text`) AGAINST('keyword')
) as `combined` ORDER BY `score` DESCAbove code is disliked because scores there are per table, they are joined and ordered. A bad approach.
So I tried to
MATCH() AGAINST() for data in TOP LEVEL SELECT as well this. (DIDN'T WORK)SELECT *,MATCH(`data`) AGAINST('keyword') as `good_score` FROM (
SELECT *,`result` as `data`,MATCH(`result`) AGAINST('keyword') as `score` FROM `table1` WHERE MATCH(`result`) AGAINST('keyword')
UNION
SELECT *,`content` as `data`,MATCH(`content`) AGAINST('keyword') as `score` FROM `table2` WHERE MATCH(`content`) AGAINST('keyword')
UNION
SELECT *,`text` as `data`,MATCH(`text`) AGAINST('keyword') as `score` FROM `table3` WHERE MATCH(`text`) AGAINST('keyword')
) as `combined` ORDER BY `good_score` DESCAbove statement is perfect for me, but it does not work because
data column is created on-the-fly and it does not support to have a FULLTEXT INDEX.My question is how to proceed to get my engine working.
- Can you somehow make
dataa FULLTEXT
- Is the
Solution
Perhaps you could record the following from the three(3) tables
Here is the code:
Now you can run a single query against the one table
Give it a Try !!!
- table name
- the column from the table name
- FULLTEXT index on the column
Here is the code:
DROP TABLE IF EXISTS combined_data;
CREATE TABLE combined_data
(
source_table VARCHAR(64),
source_id INT NOT NULL,
data TEXT NOT NULL,
FULLTEXT (data)
) ENGINE=MyISAM;
--
ALTER TABLE combined_data DISABLE KEYS;
--
INSERT INTO combined_data (source_table,source_id,data) VALUES
SELECT 'table1',id,`result` FROM table1 WHERE MATCH(`result`) AGAINST('keyword');
--
INSERT INTO combined_data (source_table,source_id,data) VALUES
SELECT 'table2',id,`content` FROM table1 WHERE MATCH(`content`) AGAINST('keyword');
--
INSERT INTO combined_data (source_table,source_id,data) VALUES
SELECT 'table3',id,`text` FROM table1 WHERE MATCH(`text`) AGAINST('keyword');
--
ALTER TABLE combined_data ENABLE KEYS;Now you can run a single query against the one table
SELECT *,MATCH(`data`) AGAINST('keyword') as `good_score`
FROM combined_data
ORDER BY `good_score` DESC;Give it a Try !!!
Code Snippets
DROP TABLE IF EXISTS combined_data;
CREATE TABLE combined_data
(
source_table VARCHAR(64),
source_id INT NOT NULL,
data TEXT NOT NULL,
FULLTEXT (data)
) ENGINE=MyISAM;
--
ALTER TABLE combined_data DISABLE KEYS;
--
INSERT INTO combined_data (source_table,source_id,data) VALUES
SELECT 'table1',id,`result` FROM table1 WHERE MATCH(`result`) AGAINST('keyword');
--
INSERT INTO combined_data (source_table,source_id,data) VALUES
SELECT 'table2',id,`content` FROM table1 WHERE MATCH(`content`) AGAINST('keyword');
--
INSERT INTO combined_data (source_table,source_id,data) VALUES
SELECT 'table3',id,`text` FROM table1 WHERE MATCH(`text`) AGAINST('keyword');
--
ALTER TABLE combined_data ENABLE KEYS;SELECT *,MATCH(`data`) AGAINST('keyword') as `good_score`
FROM combined_data
ORDER BY `good_score` DESC;Context
StackExchange Database Administrators Q#30602, answer score: 2
Revisions (0)
No revisions yet.