MySQL FULLTEXT search counting occurences -


i there way logic behind full text search

example table

+----+-------------+ | id | title       | +----+-------------+ | 1  | hello world | | 2  | world world | | 3  | hello       | | 4  | hello hello | +----+-------------+ 

this query

select `title` `example` match(`title`) against('world') 

result

+-------------+ | title       | +-------------+ | world world | | hello world | +-------------+ 

now 'world world' first since has 'world' twice there way can somthing this

+-------------+------------+-------+ | title       | appearance | score | +-------------+------------+-------+ | world world | 2          | 1     | | hello world | 1          | 2     | +-------------+------------+-------+ 

i try using syntax this, instead of attempting count how many times word occurs.

notice uses in boolean mode means when +is before, must occur. https://dev.mysql.com/doc/refman/5.6/en/fulltext-boolean.html

select title, match(`title`) against ('+world +world' in boolean mode) score  `example` match(`title`) against ('+world +world' in boolean mode) order score desc 

it generate relevance score later can sort on.

if need have count, trick.

select title, round (       (char_length(`title`) - char_length(replace(`title`, "world", ""))) / char_length("world")              ) appearance  `example` match(`title`) against ('world') 

notice case sensitive idea wrap title lcase


Comments

Popular posts from this blog

javascript - Google App Script ContentService downloadAsFile not working -

javascript - Function overwritting -

php - Find a regex to take part of Email -