Jump to content

Search for partial words in any position/order


Robin S
 Share

Recommended Posts

The selector operator ~= matches words in any position, but it doesn't match partial words (e.g. search for "crab" does not match "crabapple").

The selector operator %= matches partial words, but if there are two or more words in the search query then the words need to be next to each other and in that order (i.e. search for "crab red" does not match "red crabapple").

Is there a selector operator or technique that allows matches for partial words in any position/order? Also, the search should match all words rather than any word.

My aim is for a search such that searching for "crab red" in the title field...

  1. matches a page with title "red shiny crabapple"
  2. does not match a page with title "crab" (all words must be present)
Link to comment
Share on other sites

Great, thanks.

I used explode to get the individual words from the search query and then a foreach to build the selector.

$selector = '';
if($input->get->general_search) {
	$value = $sanitizer->selectorValue($input->get->general_search);
	$terms = explode(" ", $value);
	foreach($terms as $term) {
		$selector .= "title|author|source|keywords|bibliography_type%=$term, ";
	}
	$input->whitelist('general_search', $value); 
}
  • Like 4
Link to comment
Share on other sites

  • 3 weeks later...

Hi,

thanks for giving the example you used, Cerulean!

I realised an unwanted behaviour and don't know how to solve this: If you search for "test", also "latest" or "contest" will be among the results. So I'd like to make sure the search term ("test") is prepended or followed by a space:

        $selector .= "title%=' $term', ";
        $selector .= "title%='$term ', ";

However, this does not have any effect on the SQL query and the results. (Same if you put "title%= $term, " without the ' ' quotation marks.)

Any ideas?

Link to comment
Share on other sites

  • 3 weeks later...

Oh well, I just read my last post again. Looks I got really confused with word beginnings and endings, that one doesn't really make sense. But somehow you got exactly what I wanted to say (and didn't manage to)!

Sarah, in your case, the selector ~= should work

Also, if you're not wanting to match partial words you don't need to do the explode/foreach.

The ~= operator will match multiple whole words in any order.

Excellent, thanks a lot!

Link to comment
Share on other sites

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...