*

Destanst

  • *
  • 3 posts
Is it possible to make search match all keywords instead of any?
« on: February 20, 2025, 02:18:10 PM »
Hi,
So basically I have an issue where the search field uses [keyword] OR [keyword] in the search instead of [keyword] AND [keyword] (if that makes any sense to you)
For example, when searching 'first floor' it displays anything that has 'first' or 'floor' in the post.

I did some research on this topic and found a solution that worked for the other person, they implemented a code into theme's 'functions.php' file, however it did nothing to the search, it is still the same.

The question being, is it possible to match all used keywords and display the block only if all the keywords are present?

Thank you in advance!

Quote
function cust_pattern_search_all_keywords_only($params) {
  if (@$params['sPattern'] != '') {
      $mSearch = Search::newInstance();
      $query_elements = json_decode($mSearch->toJson(), true);
      $pattern = $query_elements['sPattern'];

      foreach (explode(' ', $pattern) as $word) {
          //$query_elements['sPattern'] = str_replace($word, '+' . $word . '*', $query_elements['sPattern']); //search with wildcard egg => eggs
          $query_elements['sPattern'] = str_replace($word, '+' . $word, $query_elements['sPattern']);
      }

      $mSearch->addLocale('%');
      $mSearch->addGroupBy(DB_TABLE_PREFIX.'t_item.pk_i_id');

      $mSearch->setJsonAlert($query_elements);
  }
}

osc_add_hook('search_conditions', 'cust_pattern_search_all_keywords_only', 5);

*

MB Themes

Re: Is it possible to make search match all keywords instead of any?
« Reply #1 on: February 21, 2025, 02:50:33 PM »
Try to use Full text or Enhanced full text search.
Alternative is to set query be ... like '%word1%word2%word3%word4%....%wordXY%'

But it means words must go in selected order.
  To get fast support, we need following details: Detail description, URL to reproduce problem, Screenshots

*

Destanst

  • *
  • 3 posts
Re: Is it possible to make search match all keywords instead of any?
« Reply #2 on: February 24, 2025, 01:56:09 PM »
I do use Enhanced Full-Text Search already, yet the issue remains.
I did try to modify search scripts, but it didn't do much.

If you know any other way, please share it with me.

Thanks in advance!

*

MB Themes

Re: Is it possible to make search match all keywords instead of any?
« Reply #3 on: March 01, 2025, 01:03:50 PM »
You could use filters/hooks to modiffy conditions - but I think problem will be to create correct condition.
  To get fast support, we need following details: Detail description, URL to reproduce problem, Screenshots