Searching with short words
Started by formmailer, Oct 09 2011 05:59 AM
7 replies to this topic
#1
Posted 09 October 2011 - 05:59 AM
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
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
#2
Posted 09 October 2011 - 06:59 AM
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);
#3
Posted 09 October 2011 - 09:26 AM
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
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
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
#4
Posted 09 October 2011 - 09:43 AM
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:
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.
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.
#5
Posted 09 October 2011 - 10:06 AM
Hi Ryan,
I'll try this later today, but it looks promising.
Thanks for your help!
/Jasper
"by" was a translated example, since my website is in Dutch, so the stopwords don't help me much, I'm afraid.PW already removes stopwords from the query (and I think that "by" is one of them).
That's good to know, might try that one if the rebuilding the query string doesn't provide the results I expected.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.
I'll try this later today, but it looks promising.
Thanks for your help!
/Jasper
#8
Posted 17 May 2012 - 07:32 PM
Yet another super-helpful forum post, solved my problem, thanks guys!
0 user(s) are reading this topic
0 members, 0 guests, 0 anonymous users













