Jump to content

Searching with short words


formmailer
 Share

Recommended Posts

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

  • Like 1
Link to comment
Share on other sites

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);
  • Like 1
Link to comment
Share on other sites

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

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.

  • Like 1
Link to comment
Share on other sites

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

  • 7 months later...

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...