Jump to content

Search with AND and EXCLUDE (NOT)


chuckymendoza
 Share

Recommended Posts

Hello together,

currently I’m working on some kind of enhanced search for pages->find in the FRONTEND and I like to ask if someone has a possible solution or hint on how to build it.

My problem is, I like to add search strings with a "+" or "-" in front of word and tell PW what to search and what not based on the input of the user.

Example:
Let say I have the following page tree

Airports
  — NYC, USA
  — Miami, USA
  — Tokyio, Japan
  — Berlin, Germany

Let’s say that all pages have more then one body text field but in one of them is a text that contains the words "Airport" and "Air-condition".

User enter the following text into an input field:
Airport air +USA -nyc

My preferences are:
1) find all Airports (or cities) with the word Airport or air in the title OR in the body text of the pages, also find part of the words like fair or air-condition
2) Filter the results of 1) and find only pages with the word USA inside
3) Filter the results of 2) and show only (exclude) the results with a word like nyc
As the result only Miami, USA will be displayed, because it got USA and NOT nyc and in the body text are the words Airport and air-condition.

For 1) I used the code (see below) from  @Mats suggestion https://processwire.com/talk/topic/10883-search-with-and-string/

It searches part of words in the title and body with OR. But what I’m looking for is, how can I search for pages that have the search term AND the term with a "+" before. It finds me Airport but it should only find Airport if +USA is inside title of body text.
And of course, how it is possible to exclude a word, for example with a "-" before the search term.

In spoken language it would be: :rolleyes:
Find anything with Airport or air. Show only results containing the word USA and exclude anything from the results with the word nyc.
In general, is it possible to exclude something from the search in pages->find and what could be a good start to code such a search function?

Thanks so much in advance!
Thomas

        if($q) {
			$input->whitelist('q', $q);
        	$qs = explode(" ", $q);
        	foreach($qs as $key => $q){
          	$qs[$key] = $sanitizer->selectorValue($q);
        	}
		
    $selector = "template=print, title|body|body_2|body_3%=" . implode("|", $qs) . ", limit=50";
	$matches = $pages->find($selector);
}

 

 

 

Link to comment
Share on other sites

Quote

 

1) find all Airports (or cities) with the word Airport or air in the title OR in the body text of the pages, also find part of the words like fair or air-condition

 

template=airport|city, title|body%=air

u donut need.Airport bcoz u got %=air

Quote

2) Filter the results of 1) and find only pages with the word USA inside

template=airport|city, title|body%=air, title%=USA

Quote

3) Filter the results of 2) and show only (exclude) the results with a word like nyc

template=airport|city, title|body%=air, title%=USA, id!=[title%=nyc]

or.in pw v3 u can do  
template=airport|city, title|body%=air, title%=USA, title!%=nyc

Link to comment
Share on other sites

13 minutes ago, WillyC said:

template=airport|city, title|body%=air, title%=USA, id!=[title%=nyc]

or.in pw v3 u can do  
template=airport|city, title|body%=air, title%=USA, title!%=nyc

Doesn't this also work?

…, !title%=nyc

Link to comment
Share on other sites

Guys, this is wonderful and so simple. I love PW!
!title%=nyc works like a charm!

In my case I needed to exclude not only from the title but also from some more fields, my thoughts, that this would work were wrong:
 

!title|job_body%=word_to_exclude

Instead someone must use:
 

!title%=word_to_exclude, !job_body%=word_to_exclude

It is possible to unify this, or is it the way PW works?

Link to comment
Share on other sites

Quote

!title%=word_to_exclude, !job_body%=word_to_exclude

What you've got above is an AND condition, which I think is what you want. You could also do this if preferred:

id!=[title|job_body%=word_to_exclude]

 

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

×
×
  • Create New...