formmailer Posted October 9, 2011 Share Posted October 9, 2011 Hi! I when searching my site (using the default search page) I can't search using words shorter than 4 characters. This is not a big issue when searching for one word (I think it's completely fine that words like "the", "and", "or" and "it" don't generate search results), but I do want to provide results for searches like "by car" and "by train". I know a Full Text Search setting in MySQL (ft_min_word_len=2) resolves the issue, as it will provide results for two and three letter words as well. But this isn't really what I want. Is there another, better way to make the search work with queries containing short words? Like ignoring the short words and just search for the other words instead? All suggestions are appreciated. /Jasper 1 Link to comment Share on other sites More sharing options...
Adam Kiss Posted October 9, 2011 Share Posted October 9, 2011 You can just delete shorter words from query, check whether rebuilt query (without short words) has any length and then search for this: <?php //i assume $query containing search string $query = 'this is one word longg wordd'; $queryArray = explode(' ', $query); array_walk($queryArray, create_function( '$v,$i,&$queryArray', 'if(mb_strlen($v)<=3){unset($queryArray[$i]);}' ), &$queryArray); $query = implode(' ',$queryArray); //now you have shortened search string in your $query, so you can find() with it $result = $pages->find('title|body|search_field~='.$query); 1 Link to comment Share on other sites More sharing options...
formmailer Posted October 9, 2011 Author Share Posted October 9, 2011 Hi Adam, This works fine, but it gives me a PHP warning: Deprecated: Call-time pass-by-reference has been deprecated which is caused by the ampersand in &$queryArray . In most cases removing the & solves the trick, but in this case it also breaks the function. I tried quite a few suggestions on how to deal with this warning, but so far nothing worked. Do you have any suggestions. /Jasper Link to comment Share on other sites More sharing options...
ryan Posted October 9, 2011 Share Posted October 9, 2011 PW already removes stopwords from the query (and I think that "by" is one of them). Take a look at the %= operator as that works the same as *= but has no stopword or length limitations (at the expense of being slower). But using Adam's example (which is a good one), you could recode that array_walk part like this: foreach($queryArray as $key => $value) { if(strlen($value < 4)) unset($queryArray[$key]); } I've never used array_walk before, and dont know what the error you got is, but I think the above would do the same thing. 1 Link to comment Share on other sites More sharing options...
formmailer Posted October 9, 2011 Author Share Posted October 9, 2011 Hi Ryan, PW already removes stopwords from the query (and I think that "by" is one of them). "by" was a translated example, since my website is in Dutch, so the stopwords don't help me much, I'm afraid. Take a look at the %= operator as that works the same as *= but has no stopword or length limitations (at the expense of being slower). That's good to know, might try that one if the rebuilding the query string doesn't provide the results I expected. But using Adam's example (which is a good one), you could recode that array_walk part like this: foreach($queryArray as $key => $value) { if(strlen($value < 4)) unset($queryArray[$key]); } I've never used array_walk before, and dont know what the error you got is, but I think the above would do the same thing. I'll try this later today, but it looks promising. Thanks for your help! /Jasper Link to comment Share on other sites More sharing options...
ryan Posted October 9, 2011 Share Posted October 9, 2011 Minor typo in my prev example, it should be if(strlen($value) < 4) unset(... Link to comment Share on other sites More sharing options...
formmailer Posted October 9, 2011 Author Share Posted October 9, 2011 Minor typo in my prev example, it should be if(strlen($value) < 4) unset(... I noticed that one... It works like a charm. Thanks! Link to comment Share on other sites More sharing options...
alan Posted May 18, 2012 Share Posted May 18, 2012 Yet another super-helpful forum post, solved my problem, thanks guys! Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now