fish man
fish man

Reputation: 2710

mysql fulltext union words

I have a mysql fulltext question. Refrence on: http://dev.mysql.com/doc/refman/5.0/en/fulltext-boolean.html

SELECT * FROM table WHERE (MATCH (aindex,bindex) AGAINST ('(+apple +juice)' IN BOOLEAN MODE))

This return 14 items

SELECT * FROM table WHERE (MATCH (aindex,bindex) AGAINST ('(+apple +pie)' IN BOOLEAN MODE))

This return 36 items

SELECT * FROM table WHERE (MATCH (aindex,bindex) AGAINST ('(+apple +pie)|(+apple +juice)' IN BOOLEAN MODE))

This return 1910 items, obviously, my query has problem. How to search words exactly contains (+apple +pie) and (+apple +juice)? I think it should be return 14+36=50items

And in my purpose, I need a union search for

(+apple +pie) (+apple +juice) (+apple +macintosh) (+apple +turnover) (+apple +strudel) (+pear +pie) (+pear +juice) (+pear +macintosh) (+pear +turnover) (+pear +strudel)

it should be return nearly 520 items. Thanks.

Upvotes: 0

Views: 523

Answers (1)

Ilion
Ilion

Reputation: 6882

This should be what you want:

SELECT 
  * 
FROM 
  table 
WHERE 
  (MATCH (aindex,bindex) AGAINST ('+apple +(pie juice)' IN BOOLEAN MODE))

ETA: Missed the pear clauses my first read through. To get all the ones you listed I think this should work:

SELECT 
  * 
FROM 
  table 
WHERE 
  (MATCH (aindex,bindex) AGAINST ('+((apple +(pie juice macintosh turnover strudel)) (pear +(pie juice macintosh turnover strudel)))' IN BOOLEAN MODE));

That should translate into "must contain apple and one of pie, juice, macintosh, turnover, or strudel OR pear and one of pie, juice, macintosh, turnover, or strudel.

If that doesn't work I would do this:

SELECT 
  * 
FROM 
  table 
WHERE 
  (MATCH (aindex,bindex) AGAINST ('+apple +(pie juice macintosh turnover strudel)' IN BOOLEAN MODE))
UNION
SELECT 
  * 
FROM 
  table 
WHERE 
  (MATCH (aindex,bindex) AGAINST ('+pear +(pie juice macintosh turnover strudel)' IN BOOLEAN MODE));

Upvotes: 2

Related Questions