Advertisement:

Author Topic: [SOLVED] Show only ads that contain all keywords given in search  (Read 909 times)

Syed

  • Sr. Member
  • ****
  • Posts: 254
[SOLVED] Show only ads that contain all keywords given in search
« on: February 27, 2017, 08:39:23 am »
Hi,

I want to improve search results so that when someone searches for "samsung galaxy edge" search result must show only those ads that contain all these keywords and sorted by time.
I have already gone through following topic and used 4th version, it shows related results but not ordered by time.
http://forums.osclass.org/3-5-x/search-problem-oc-3-5-3/

Please guide me to the right direction.

Regards,
« Last Edit: February 28, 2017, 03:22:04 pm by Syed »

teseo

  • Hero Member
  • *****
  • Posts: 6169
Hi,

That would be possible (altough I wouldn't recommend it, people might include irrelevant keywords, like "looking for samsung galaxy edge", that couldn't give any results). Anyway, are you interested also in "search all languages" feature? ??? If you only have one language, the code would be a lot simpler...

Regards

Syed

  • Sr. Member
  • ****
  • Posts: 254
thanks teseo for response,

Currently I have only one language but I have plan to add more, then I will be interested in "search all languages"

Regards,

teseo

  • Hero Member
  • *****
  • Posts: 6169
Add this at the very bottom of your theme functions.php:
Notes:
1.- Take care not to leave blank lines after this.
2.- If your theme functions.php doesn't end with ?> skip first line of my code.

Code: [Select]
<?php
function cust_pattern_search_all_keywords_only($params) {
    if (@
$params['sPattern'] != '') {
        
$mSearch Search::newInstance();
        
$query_elements = (array) json_decode($mSearch->toJson());
        
$pattern $query_elements['sPattern'];

        foreach (
explode(' '$pattern) as $word) {
            
$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'10);

function 
cust_alerts_user_dashboard() {
    if (
Params::getParam('page') == "user" && Params::getParam('action') == "alerts") {
        
$webUser = new CWebUser;

        
$aAlerts Alerts::newInstance()->findByUserSession::newInstance()->_get('userId'), false );
        
$user User::newInstance()->findByPrimaryKeySession::newInstance()->_get('userId'));
        foreach(
$aAlerts as $k => $a) {
            
$array_conditions   = (array)json_decode($a['s_search']);

            
$search = new Search();
            
$search->setJsonAlert($array_conditions);
            if (
osc_version() > 361$search->notFromUser(Session::newInstance()->_get('userId'));
            
$search->addLocale('%');
            
$search->addGroupBy(DB_TABLE_PREFIX.'t_item.pk_i_id');
            
$search->limit(03);

            
$aAlerts[$k]['items'] = $search->doSearch();
        }

        
$webUser->_exportVariableToView('alerts'$aAlerts);
        
View::newInstance()->_reset('alerts');
        
$webUser->_exportVariableToView('user'$user);
    }
}

osc_add_hook('before_html''cust_alerts_user_dashboard');
?>


Regards

Syed

  • Sr. Member
  • ****
  • Posts: 254
Thank you very much teseo, it works fine.

1. Do it search all languages?

2. Do it work for search alerts subscribed by users?

3. Is it possible to add some more relevant search results at the end of these search results.

4. Is it good to use following wildcard code with it (currently searching for duck egg do not include results containing duck eggs)

Code: [Select]
<?php
function cust_search_keyword_wildcard($params) {

    if (
$params['sPattern']) {
        
$mSearch =  Search::newInstance();
        
$query_elements = (array) json_decode($mSearch->toJson());
    
        
$query_elements['sPattern'] = str_replace(' ''* '$query_elements['sPattern']) . '*';
    
        
$mSearch->setJsonAlert($query_elements);
    }
}

osc_add_hook('search_conditions''cust_search_keyword_wildcard'10);  
?>


Regards,

teseo

  • Hero Member
  • *****
  • Posts: 6169
1 & 2: Yes

4.- In the function cust_pattern_search_all_keywords_only($params), replace this line:

Code: [Select]
$query_elements['sPattern'] = str_replace($word, '+' . $word, $query_elements['sPattern']);
with:

Code: [Select]
$query_elements['sPattern'] = str_replace($word, '+' . $word . '*', $query_elements['sPattern']);
3.- Many things are possible, but they would need much time to develop, I'm just trying here to provide generic procedures useful to many people.

You're welcome, please add [SOLVED] to the title of this thread.

Regards
« Last Edit: February 28, 2017, 01:26:28 pm by teseo »

Syed

  • Sr. Member
  • ****
  • Posts: 254
Re: [SOLVED] Show only ads that contain all keywords given in search
« Reply #6 on: February 28, 2017, 03:24:07 pm »
Thank you teseo

You are very much helpful.

Regards,

fallemmis

  • Newbie
  • *
  • Posts: 2
Re: [SOLVED] Show only ads that contain all keywords given in search
« Reply #7 on: July 13, 2019, 05:30:36 pm »
Teseo!! i don`t know who are you or where are you from but i reallllyyyy love you man!!!

BaghDad

  • Newbie
  • *
  • Posts: 2
Re: [SOLVED] Show only ads that contain all keywords given in search
« Reply #8 on: July 22, 2019, 02:22:42 pm »
Hi,

I want to improve search results so that when someone searches for "samsung galaxy edge" search result must show only those ads that contain all these keywords and sorted by time.
I have already gone through following topic and used 4th version, it shows related results but not ordered by time.
https://time4buying.com/best-power-scrubbers

Please guide me to the right direction.

Regards,

That would be possible (altough I wouldn't recommend it, people might include irrelevant keywords, like "looking for samsung galaxy edge", that couldn't give any results). Anyway, are you interested also in "search all languages" feature? ??? If you only have one language, the code would be a lot simpler...