Jump to content


Photo

Searching with short words


  • Please log in to reply
7 replies to this topic

#1 formmailer

formmailer

    Sr. Member

  • Members
  • PipPipPipPip
  • 245 posts
  • 28

  • LocationHudiksvall, Sweden (but originally from The Netherlands)

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

#2 adamkiss

adamkiss

    Master of the universe

  • Moderators
  • 1,078 posts
  • 289

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 formmailer

formmailer

    Sr. Member

  • Members
  • PipPipPipPip
  • 245 posts
  • 28

  • LocationHudiksvall, Sweden (but originally from The Netherlands)

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
&$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 ryan

ryan

    Hero Member

  • Administrators
  • 5,771 posts
  • 3112

  • LocationAtlanta, GA

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:

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 formmailer

formmailer

    Sr. Member

  • Members
  • PipPipPipPip
  • 245 posts
  • 28

  • LocationHudiksvall, Sweden (but originally from The Netherlands)

Posted 09 October 2011 - 10:06 AM

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

#6 ryan

ryan

    Hero Member

  • Administrators
  • 5,771 posts
  • 3112

  • LocationAtlanta, GA

Posted 09 October 2011 - 10:24 AM

Minor typo in my prev example, it should be if(strlen($value) < 4) unset(...

#7 formmailer

formmailer

    Sr. Member

  • Members
  • PipPipPipPip
  • 245 posts
  • 28

  • LocationHudiksvall, Sweden (but originally from The Netherlands)

Posted 09 October 2011 - 01:25 PM

Minor typo in my prev example, it should be if(strlen($value) < 4) unset(...

I noticed that one... :-)

It works like a charm. Thanks!

#8 alanfluff

alanfluff

    Sr. Member

  • Members
  • PipPipPipPip
  • 405 posts
  • 118

  • LocationOttawa, Canada

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