mysql - SQL search for words in any order -
given mockup:
+-----------+-------------+---------------------------+ | item_id | item_name | desc | +-----------+-------------+---------------------------+ | 1 | product 1 | lorem ipsum dolor... | | 2 | product 2 | lorem mauris eu... | | 3 | product 3 | scelerisque sagittis... | | 4 | product 4 | lorem dolor ipsum... | | 5 | product 5 | ipsum dolor lorem... | +-----------+-------------+---------------------------+ and want search of products contain words lorem ipsum in either item_name or desc. additionally, words can appear between lorem , ipsum, , lorem , ipsum can appear in order. basically, search return items 1, 4, , 5
now, know accomplish with:
select * items (item_name 'lorem%ipsum' or desc 'lorem%ipsum') or (item_name 'ipsum%lorem' or desc 'ipsum%lorem') but if search term longer (ie. lorem ipsum dolor sit amet, consectetur adipiscing elit), feel become bit ridiculous number of or added query. there easier/more efficient way handle this?
this sort of search requirement sounds candidate full text search.
full text search (or @ least can be) more of "search engine" search opposed traditional sql like searches. full text searching, order of words being searched not matter, , depending on rdbms full text searching capabilities allow synonym lookup, noise word filtering.
in (i believe) cases, full text searching faster like search. here article on getting started full text search in mysql.
example mysql full text search syntax:
select * items match(item_name) against ('+lorem +ipsum' in boolean mode) full text searching have requirements (which gone detail in links in article). i've not worked mysqls full text search, or i'd list out steps. should enough started though if wanted go in direction.
Comments
Post a Comment