#------------------------------------------------------------------------------
# Test With two FTS index on same table + alter/create/drop index + tnx
#------------------------------------------------------------------------------
--source include/have_innodb.inc
--source include/maybe_versioning.inc

--disable_warnings
drop table if exists t1;
--enable_warnings

# Create FTS table
CREATE TABLE t1 (
        id INT UNSIGNED AUTO_INCREMENT NOT NULL PRIMARY KEY,
        a VARCHAR(200),
        b TEXT
        ) ENGINE = InnoDB STATS_PERSISTENT=0;

# Insert rows
INSERT INTO t1 (a,b) VALUES
        ('MySQL Tutorial','DBMS stands for DataBase ...')  ,
        ('How To Use MySQL Well','After you went through a ...'),
        ('Optimizing MySQL','In this tutorial we will show ...');

# Create the 2 FTS index Using Alter on same table
ALTER TABLE t1 ADD FULLTEXT INDEX idx_1 (a);
ALTER TABLE t1 ADD FULLTEXT INDEX idx_2 (b);
EVAL SHOW CREATE TABLE t1;

# check multiple index with transaction
START TRANSACTION;
# Insert rows
INSERT INTO t1 (a,b) VALUES
        ('1001 MySQL Tricks','1. Never run mysqld as root. 2. ...'),
        ('MySQL vs. YourSQL','In the following database comparison ...'),
        ('MySQL Security','When configured properly, MySQL ...');
ROLLBACK;

# Select word "tutorial" in the table
SELECT * FROM t1 WHERE MATCH (a)
        AGAINST ('Tutorial' IN NATURAL LANGUAGE MODE);

# boolean mode
select * from t1 where MATCH(a) AGAINST("+mysql +Tutorial" IN BOOLEAN MODE);
select * from t1 where MATCH(b) AGAINST("+Tutorial" IN BOOLEAN MODE);
select * from t1 where MATCH(b) AGAINST("+stands +(DataBase)" IN BOOLEAN MODE);
select * from t1 where MATCH(b) AGAINST("+DataBase -(comparison)" IN BOOLEAN MODE);
select *, MATCH(a) AGAINST("Optimizing MySQL" IN BOOLEAN MODE) as x from t1;
select *, MATCH(b) AGAINST("collections support" IN BOOLEAN MODE) as x from t1;
select * from t1 where MATCH a AGAINST ("+Optimiz* +Optimiz*" IN BOOLEAN MODE);
select * from t1 where MATCH b AGAINST ('"DBMS stands"' IN BOOLEAN MODE);
select * from t1 where MATCH b AGAINST ('"DBMS STANDS"' IN BOOLEAN MODE);

# query expansion
select * from t1 where MATCH(b) AGAINST ("DataBase" WITH QUERY EXPANSION);
select * from t1 where MATCH(a) AGAINST ("Security" WITH QUERY EXPANSION);

# Drop index 
ALTER TABLE t1 DROP INDEX idx_1;
ALTER TABLE t1 DROP INDEX idx_2;

# Create the FTS index again
ALTER TABLE t1 ADD FULLTEXT INDEX idx_1 (a);
ALTER TABLE t1 ADD FULLTEXT INDEX idx_2 (b);

# Select word "tutorial" in the table
SELECT * FROM t1 WHERE MATCH (a)
        AGAINST ('Tutorial' IN NATURAL LANGUAGE MODE);

# boolean mode
select * from t1 where MATCH(a) AGAINST("+mysql +Tutorial" IN BOOLEAN MODE);
select * from t1 where MATCH(b) AGAINST("+Tutorial" IN BOOLEAN MODE);
select * from t1 where MATCH(b) AGAINST("+stands +(DataBase)" IN BOOLEAN MODE);
select * from t1 where MATCH(b) AGAINST("+DataBase -(comparison)" IN BOOLEAN MODE);
select *, MATCH(a) AGAINST("Optimizing MySQL" IN BOOLEAN MODE) as x from t1;
select *, MATCH(b) AGAINST("collections support" IN BOOLEAN MODE) as x from t1;
select * from t1 where MATCH a AGAINST ("+Optimiz* +Optimiz*" IN BOOLEAN MODE);
select * from t1 where MATCH b AGAINST ('"DBMS stands"' IN BOOLEAN MODE);
select * from t1 where MATCH b AGAINST ('"DBMS STANDS"' IN BOOLEAN MODE);

# query expansion
select * from t1 where MATCH(b) AGAINST ("DataBase" WITH QUERY EXPANSION);
select * from t1 where MATCH(a) AGAINST ("Security" WITH QUERY EXPANSION);

# insert for proximity search 
INSERT INTO t1 (a,b) VALUES ('test query expansion','for database ...');
# Insert into table with similar word of different distances
INSERT INTO t1 (a,b) VALUES
        ('test proximity search, test, proximity and phrase',
         'search, with proximity innodb');

INSERT INTO t1 (a,b) VALUES
        ('test proximity fts search, test, proximity and phrase',
         'search, with proximity innodb');

INSERT INTO t1 (a,b) VALUES
        ('test more of proximity for fts search, test, more proximity and phrase',
         'search, with proximity innodb');

# This should only return the first document
SELECT * FROM t1
        WHERE MATCH (a)
        AGAINST ('"proximity search"@3' IN BOOLEAN MODE);

# This would return no document
SELECT * FROM t1
        WHERE MATCH (a)
        AGAINST ('"proximity search"@2' IN BOOLEAN MODE);

# This give you all three documents
SELECT * FROM t1
        WHERE MATCH (b)
        AGAINST ('"proximity innodb"@4' IN BOOLEAN MODE);

# Similar boundary testing for the words
SELECT * FROM t1
        WHERE MATCH (a)
        AGAINST ('"test proximity"@3' IN BOOLEAN MODE);

# Test with more word The last document will return, please notice there
# is no ordering requirement for proximity search.
SELECT * FROM t1
        WHERE MATCH (a)
        AGAINST ('"more test proximity"@3' IN BOOLEAN MODE);

SELECT * FROM t1
        WHERE MATCH (a)
        AGAINST ('"more test proximity"@2' IN BOOLEAN MODE);

# The phrase search will not require exact word ordering
SELECT * FROM t1
        WHERE MATCH (a)
        AGAINST ('"more fts proximity"@02' IN BOOLEAN MODE);


# Select word "tutorial" in the table - innodb crash
SELECT * FROM t1 WHERE CONCAT(t1.a,t1.b) IN (
SELECT CONCAT(a,b) FROM t1 AS t2 WHERE
MATCH (t2.a) AGAINST ('Tutorial' IN NATURAL LANGUAGE MODE)
) OR t1.id = 3 ;


# Select word "tutorial" in the table  - innodb crash
SELECT * FROM t1 WHERE CONCAT(t1.a,t1.b) IN (
SELECT CONCAT(a,b) FROM t1 AS t2
WHERE MATCH (t2.a) AGAINST ('Tutorial' IN NATURAL LANGUAGE MODE)
AND t2.id != 3) ;

# Select word "tutorial" in the table
SELECT * FROM t1 WHERE id IN (SELECT MIN(id) FROM t1 WHERE
MATCH (b) AGAINST ('Tutorial' IN NATURAL LANGUAGE MODE)) OR id = 3 ;

# Select word except  "tutorial" in the table
SELECT * FROM t1 WHERE id NOT IN (SELECT MIN(id) FROM t1
WHERE MATCH (b) AGAINST ('Tutorial' IN NATURAL LANGUAGE MODE)) ;


# Select word "tutorial" in the table
SELECT * FROM t1 WHERE EXISTS (SELECT t2.id FROM t1 AS t2 WHERE
MATCH (t2.b) AGAINST ('Tutorial' IN NATURAL LANGUAGE MODE)
AND t1.id = t2.id) ;


# Select not word like "tutorial" using subquery
SELECT * FROM t1 WHERE NOT EXISTS (SELECT t2.id FROM t1 AS t2 WHERE
MATCH (t2.a) AGAINST ('Tutorial' IN NATURAL LANGUAGE MODE)
AND t1.id = t2.id) ;


SELECT * FROM t1 WHERE t1.id = (SELECT MAX(t2.id) FROM t1 AS t2 WHERE
MATCH(t2.a) AGAINST ('"proximity search"@3' IN BOOLEAN MODE));
SELECT * FROM t1 WHERE t1.id > (SELECT MIN(t2.id) FROM t1 AS t2 WHERE
MATCH(t2.b) AGAINST ('"proximity innodb"@3' IN BOOLEAN MODE));

DROP TABLE t1;

