Support forums > old

Search Problem OC 3.5.3

(1/9) > >>

Nex23:
I have again a problem with my search.
My searches do not return any item.

The website is : http://xoor.ro

What can I do ? The install is fresh and installed with Softaculous .

Nex23:
Ok , I've figured out that the search problem appears after I change the default language to another language . What may be the problem ?

teseo:
Hi,

Yes, searches are made over current selected language. Any ad with title/description marked as written in other language won't be taken into consideration.

***UPDATED with 5th version*** (February, 14 2017)

If you're interested in your searches covering all enabled languages, 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.

5th version (Alerts compatible -no duplicate ads, Multilanguage on User Alerts, solved problem with page language changing)


--- Code: ---<?php
function cust_refined_pattern_search($params) {
    if (@$params['sPattern'] != '') {
        $mSearch = Search::newInstance();
        
        $mSearch->addLocale('%');
        $mSearch->addGroupBy(DB_TABLE_PREFIX.'t_item.pk_i_id');       
    }
}

osc_add_hook('search_conditions', 'cust_refined_pattern_search');

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

        $aAlerts = Alerts::newInstance()->findByUser( Session::newInstance()->_get('userId'), false );
        $user = User::newInstance()->findByPrimaryKey( Session::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(0, 3);

            $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');

?>
--- End code ---

4th version (Alerts compatible -no duplicate ads, Multilanguage on User Alerts, problem with page language changing, other bugs)


--- Code: ---<?php
function cust_pattern_search_all_locales($params) {
    
    if ($params['sPattern'] != '') {
        $mSearch =  Search::newInstance();
        $mSearch->addLocale('%');
        $mSearch->addGroupBy("pk_i_id"); 
    }
}

osc_add_hook('search_conditions', 'cust_pattern_search_all_locales');

function cust_alerts_locale_hack_start() {

    if (osc_get_osclass_section() == "alerts") {
        View::newInstance()->_exportVariableToView('aUserLocale', Session::newInstance()->_get('userLocale'));
        Session::newInstance()->_set('userLocale', '%');
    }
}

osc_add_hook('init', 'cust_alerts_locale_hack_start');

function cust_alerts_locale_hack_end($conditions) {

    if (osc_get_osclass_section() == "alerts") {
        Session::newInstance()->_set('userLocale', View::newInstance()->_get('aUserLocale'));
    }

    return $conditions;
}

osc_add_filter('sql_search_item_conditions', 'cust_alerts_locale_hack_end');
?>

--- End code ---

3rd version (Alerts compatible -no duplicate ads, No multilanguage on User Alerts)


--- Code: ---<?php
function cust_pattern_search_all_locales($params) {
    $mSearch =  Search::newInstance();

    if ($params['sPattern'] != '') {
        $mSearch->addConditions("d.fk_c_locale_code LIKE '%'");
        $mSearch->addGroupBy("pk_i_id");
    }
}

osc_add_hook('search_conditions', 'cust_pattern_search_all_locales');

function cust_clean_duplicate_ads_user_alerts() {
    
    $clean_alerts = View::newInstance()->_get('alerts');

    if ($clean_alerts) {
        foreach ($clean_alerts as $key => $alert) {

            $outputArray = array();
            $keysArray = array();
            foreach ($clean_alerts[$key]['items'] as $innerArray) {
                if (!in_array($innerArray['pk_i_id'], $keysArray)) {
                    $keysArray[] = $innerArray['pk_i_id'];
                    $outputArray[] = $innerArray;
                }
            }

            $clean_alerts[$key]['items'] = $outputArray;
        }

    View::newInstance()->_exportVariableToView('alerts', $clean_alerts);
    }  
}

osc_add_hook('before_html', 'cust_clean_duplicate_ads_user_alerts');
?>
--- End code ---

2nd version (Alerts compatible -but including duplicate ads under certain circumstances)


--- Code: ---<?php
function cust_pattern_search_all_locales($params) {
    $mSearch =  Search::newInstance();

    if ($params['sPattern'] != '') {
        $mSearch->addConditions("d.fk_c_locale_code LIKE '%'");
        $mSearch->addGroupBy("pk_i_id");
    }
}

osc_add_hook('search_conditions', 'cust_pattern_search_all_locales');
?>
--- End code ---


1st version (Alerts uncompatible)


--- Code: ---<?php
function cust_pattern_search_all_locales($params) {
    $mSearch =  Search::newInstance();
    
    if ($params['sPattern'] != '') {
        $mSearch->addLocale('%');
        $mSearch->addGroupBy("pk_i_id");
    }
}

osc_add_hook('search_conditions', 'cust_pattern_search_all_locales');
?>
--- End code ---

Regards

Nex23:
Yep , that worked for me . Thanks a lot !

There is another problem with the search algorithm .
Let's say I have: Blue T-Shirt and Black T-Shirt .
If I search after "T-Shirt" I will receive both results , if I search after "Black T-Shirt" I will receive again both results BUT they will be ordered by "Newest first" or by price , not by relevance !
And by this I mean if the Blue one is the newest added if I search for Black T-Shirt , the Blue one will come up first . How can I change that ? The blue one has two matches while the black one only one !!

teseo:
***UPDATED with 10th version*** :o :) (February, 14 2017)

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.

10th version (Alerts compatible, Multilanguage on User Alerts), no Sorting by relevance on See By Price, Compatibility with other possible custom JOINS, ignore duplicates, fixed problem with page language changing and other bugs


--- Code: ---<?php
function cust_refined_pattern_search($params) {
    if (@$params['sPattern'] != '') {
        $mSearch = Search::newInstance();
        $query_elements = (array) json_decode($mSearch->toJson());
        
        $mSearch->addLocale('%');
        $mSearch->addGroupBy(DB_TABLE_PREFIX.'t_item.pk_i_id');
        if (@$params['sOrder'] != 'i_price') {
            $mSearch->addJoinTable(count($query_elements['tables_join']), sprintf("(SELECT fk_i_item_id, MATCH(s_title, s_description) AGAINST('%s') AS relevance FROM %st_item_description) des", $params['sPattern'], DB_TABLE_PREFIX), 'des.fk_i_item_id = '.DB_TABLE_PREFIX.'t_item.pk_i_id', 'INNER' );
            $mSearch->order("des.relevance DESC, dt_pub_date", "DESC");
        }        
    }
}

osc_add_hook('search_conditions', 'cust_refined_pattern_search');

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

        $aAlerts = Alerts::newInstance()->findByUser( Session::newInstance()->_get('userId'), false );
        $user = User::newInstance()->findByPrimaryKey( Session::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(0, 3);

            $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');
?>
--- End code ---

9th version (Alerts compatible, Multilanguage on User Alerts), no Sorting by relevance on See By (another little fix), Compatibility with other possible custom JOINS, Discard duplicatesproblem with page language changing, other bugs


--- Code: ---<?php
function cust_refined_pattern_search($params) {
    
    if ($params['sPattern'] != '' && !Session::newInstance()->_get('seeBy')) {
        $mSearch = Search::newInstance();
        $query_elements = (array) json_decode($mSearch->toJson());
    
        $mSearch->addJoinTable(count($query_elements['tables_join']), sprintf("(SELECT DISTINCT(fk_i_item_id), MATCH(s_title, s_description) AGAINST('%s') AS relevance FROM %st_item_description ) des", $params['sPattern'], DB_TABLE_PREFIX), 'des.fk_i_item_id = '.DB_TABLE_PREFIX.'t_item.pk_i_id', 'INNER' );
        $mSearch->addLocale('%');
        $mSearch->addGroupBy(DB_TABLE_PREFIX.'t_item.pk_i_id');
        $mSearch->order("des.relevance DESC, dt_pub_date", "DESC"); 
    }

    Session::newInstance()->_drop('seeBy');
}

osc_add_hook('search_conditions', 'cust_refined_pattern_search');

function cust_alerts_locale_hack_start() {

    if (osc_get_osclass_section() == "alerts") {
        View::newInstance()->_exportVariableToView('aUserLocale', Session::newInstance()->_get('userLocale'));
        Session::newInstance()->_set('userLocale', '%');
    }

    // Detect See By link     
    if (Session::newInstance()->_get('seeBy') == '')  Session::newInstance()->_set('seeBy', (Params::getParam('seeBy') ? 1 : 0));
    Params::unsetParam('seeBy');
}

osc_add_hook('init', 'cust_alerts_locale_hack_start');

function cust_alerts_locale_hack_end($conditions) {

    if (osc_get_osclass_section() == "alerts") {
        Session::newInstance()->_set('userLocale', View::newInstance()->_get('aUserLocale'));
    }

    return $conditions; 
}

osc_add_filter('sql_search_item_conditions', 'cust_alerts_locale_hack_end');

function cust_seeBy_link_add_identifier() { ?>
    <script type="text/javascript">
        $(".see_by a").on('click', function(e) {
            $(this).prop('href', $(this).prop('href') + '?seeBy=1');
            return true;
        })
    </script>
<?php  }

osc_add_hook('footer', 'cust_seeBy_link_add_identifier');
?>

--- End code ---

8th version (Alerts compatible - no duplicate ads, Multilanguage on User Alerts), no Sorting by relevance on See By (another little fix), Compatibility with other possible custom JOINS


--- Code: ---<?php
function cust_refined_pattern_search($params) {
    
    if ($params['sPattern'] != '' && !Session::newInstance()->_get('seeBy')) {
        $mSearch = Search::newInstance();
        $query_elements = (array) json_decode($mSearch->toJson());
    
        $mSearch->addJoinTable(count($query_elements['tables_join']), sprintf("(SELECT DISTINCT(fk_i_item_id), MATCH(s_title, s_description) AGAINST('%s') AS relevance FROM %st_item_description ) des", $params['sPattern'], DB_TABLE_PREFIX), 'des.fk_i_item_id = '.DB_TABLE_PREFIX.'t_item.pk_i_id', 'INNER' );
        $mSearch->addLocale('%');
        $mSearch->order("des.relevance DESC, dt_pub_date", "DESC"); 
    }

    Session::newInstance()->_drop('seeBy');
}

osc_add_hook('search_conditions', 'cust_refined_pattern_search');

function cust_alerts_locale_hack_start() {

    if (osc_get_osclass_section() == "alerts") {
        View::newInstance()->_exportVariableToView('aUserLocale', Session::newInstance()->_get('userLocale'));
        Session::newInstance()->_set('userLocale', '%');
    }

    // Detect See By link     
    if (Session::newInstance()->_get('seeBy') == '')  Session::newInstance()->_set('seeBy', (Params::getParam('seeBy') ? 1 : 0));
    Params::unsetParam('seeBy');
}

osc_add_hook('init', 'cust_alerts_locale_hack_start');

function cust_alerts_locale_hack_end($conditions) {

    if (osc_get_osclass_section() == "alerts") {
        Session::newInstance()->_set('userLocale', View::newInstance()->_get('aUserLocale'));
    }

    return $conditions; 
}

osc_add_filter('sql_search_item_conditions', 'cust_alerts_locale_hack_end');

function cust_seeBy_link_add_identifier() { ?>
    <script type="text/javascript">
        $(".see_by a").on('click', function(e) {
            $(this).prop('href', $(this).prop('href') + '?seeBy=1');
            return true;
        })
    </script>
<?php  }

osc_add_hook('footer', 'cust_seeBy_link_add_identifier');
?>

--- End code ---

7th version (Alerts compatible - no duplicate ads, Multilanguage on User Alerts), no Sorting by relevance on See By (little fix), Compatibility with other possible custom JOINS


--- Code: ---<?php
function cust_refined_pattern_search($params) {
    
    if ($params['sPattern'] != '' && !Session::newInstance()->_get('seeBy')) {
        $mSearch = Search::newInstance();
        $query_elements = (array) json_decode($mSearch->toJson());
    
        $mSearch->addJoinTable(count($query_elements['tables_join']), sprintf("(SELECT DISTINCT(fk_i_item_id), MATCH(s_title, s_description) AGAINST('%s') AS relevance FROM %st_item_description ) des", $params['sPattern'], DB_TABLE_PREFIX), 'des.fk_i_item_id = '.DB_TABLE_PREFIX.'t_item.pk_i_id', 'INNER' );
        $mSearch->addLocale('%');
        $mSearch->order("des.relevance DESC, dt_pub_date", "DESC"); 
    }

    Session::newInstance()->_drop('seeBy');
}

osc_add_hook('search_conditions', 'cust_refined_pattern_search');

function cust_alerts_locale_hack_start() {

    if (osc_get_osclass_section() == "alerts") {
        View::newInstance()->_exportVariableToView('aUserLocale', Session::newInstance()->_get('userLocale'));
        Session::newInstance()->_set('userLocale', '%');
    }

    // Detect See By link     
    if (Session::newInstance()->_get('seeBy') == '')  Session::newInstance()->_set('seeBy', (Params::getParam('seeBy') ? 1 : 0));
    Params::unsetParam('seeBy');
}

osc_add_hook('init', 'cust_alerts_locale_hack_start');

function cust_alerts_locale_hack_end($conditions) {

    if (osc_get_osclass_section() == "alerts") {
        Session::newInstance()->_set('userLocale', View::newInstance()->_get('aUserLocale'));
    }

    return $conditions; 
}

osc_add_filter('sql_search_item_conditions', 'cust_alerts_locale_hack_end');

function cust_seeBy_link_add_identifier() { ?>
    <script type="text/javascript">
        $(".see_by a").on('click', function(e) {
            e.preventDefault();
            window.location = $(this).prop('href') + '?seeBy=1';
        })
    </script>
<?php  }

osc_add_hook('footer', 'cust_seeBy_link_add_identifier');
?>

--- End code ---

6th version (Alerts compatible - no duplicate ads, Multilanguage on User Alerts), no Sorting by relevance on See By (little bug)


--- Code: ---<?php
function cust_refined_pattern_search($params) {
    
    if ($params['sPattern'] != '' && !Session::newInstance()->_get('seeBy')) {
        $mSearch = Search::newInstance();
        $mSearch->addJoinTable( null, sprintf("(SELECT DISTINCT(fk_i_item_id), MATCH(s_title, s_description) AGAINST('%s') AS relevance FROM %st_item_description ) des", $params['sPattern'], DB_TABLE_PREFIX), 'des.fk_i_item_id = '.DB_TABLE_PREFIX.'t_item.pk_i_id', 'INNER' );
        $mSearch->addLocale('%');
        $mSearch->order("des.relevance DESC, dt_pub_date", "DESC"); 
    }

    Session::newInstance()->_drop('seeBy');
}

osc_add_hook('search_conditions', 'cust_refined_pattern_search');

function cust_alerts_locale_hack_start() {

    if (osc_get_osclass_section() == "alerts") {
        View::newInstance()->_exportVariableToView('aUserLocale', Session::newInstance()->_get('userLocale'));
        Session::newInstance()->_set('userLocale', '%');
    }

    // Detect See By link     
    if (Session::newInstance()->_get('seeBy') == '')  Session::newInstance()->_set('seeBy', (Params::getParam('seeBy') ? 1 : 0));
    Params::unsetParam('seeBy');
}

osc_add_hook('init', 'cust_alerts_locale_hack_start');

function cust_alerts_locale_hack_end($conditions) {

    if (osc_get_osclass_section() == "alerts") {
        Session::newInstance()->_set('userLocale', View::newInstance()->_get('aUserLocale'));
    }

    return $conditions; 
}

osc_add_filter('sql_search_item_conditions', 'cust_alerts_locale_hack_end');

function cust_seeBy_link_add_identifier() { ?>
    <script type="text/javascript">
        $(".see_by a").on('click', function(e) {
            e.preventDefault();
            window.location = $(this).prop('href') + '/?seeBy=1';
        })
    </script>
<?php  }

osc_add_hook('footer', 'cust_seeBy_link_add_identifier');
?>

--- End code ---

5th version (Alerts compatible - no duplicate ads, Multilanguage on User Alerts), no Sorting by relevance on See By (incomplete)


--- Code: ---<?php
function cust_refined_pattern_search($params) {

    if ($params['sPattern'] != '' && !Params::existParam('sOrder')) {
        $mSearch = Search::newInstance();
        $mSearch->addJoinTable( null, sprintf("(SELECT DISTINCT(fk_i_item_id), MATCH(s_title, s_description) AGAINST('%s') AS relevance FROM %st_item_description ) des", $params['sPattern'], DB_TABLE_PREFIX), 'des.fk_i_item_id = '.DB_TABLE_PREFIX.'t_item.pk_i_id', 'INNER' );
        $mSearch->addLocale('%');
        $mSearch->order("des.relevance DESC, dt_pub_date", "DESC");
    }
}

osc_add_hook('search_conditions', 'cust_refined_pattern_search');

function cust_alerts_locale_hack_start() {
    
    if (osc_get_osclass_section() == "alerts") {
        View::newInstance()->_exportVariableToView('aUserLocale', Session::newInstance()->_get('userLocale'));
        Session::newInstance()->_set('userLocale', '%');
    }
}

osc_add_hook('init', 'cust_alerts_locale_hack_start'); 

function cust_alerts_locale_hack_end($conditions) {

    if (osc_get_osclass_section() == "alerts") {
        Session::newInstance()->_set('userLocale', View::newInstance()->_get('aUserLocale'));
    }
    
    return $conditions;
}

osc_add_filter('sql_search_item_conditions', 'cust_alerts_locale_hack_end');
?>

--- End code ---

Well, first, I've realized that my previous solution to include all languages in the search could retrieve same ad several times, so I've added now a GROUP BY to prevent that happening.

And regarding your request for including all languages plus relevance ordering, here it's the solution (of course, use one or another):

4th version (Alerts compatible -no duplicate ads, Multilanguage on User Alerts)


--- Code: ---<?php
function cust_refined_pattern_search($params) {

    if ($params['sPattern'] != '') {
        $mSearch = Search::newInstance();
        $mSearch->addJoinTable( null, sprintf("(SELECT DISTINCT(fk_i_item_id), MATCH(s_title, s_description) AGAINST('%s') AS relevance FROM %st_item_description ) des", $params['sPattern'], DB_TABLE_PREFIX), 'des.fk_i_item_id = '.DB_TABLE_PREFIX.'t_item.pk_i_id', 'INNER' );
        $mSearch->addLocale('%');
        $mSearch->order("des.relevance DESC, dt_pub_date", "DESC");
    }
}

osc_add_hook('search_conditions', 'cust_refined_pattern_search');

function cust_alerts_locale_hack_start() {
    
    if (osc_get_osclass_section() == "alerts") {
        View::newInstance()->_exportVariableToView('aUserLocale', Session::newInstance()->_get('userLocale'));
        Session::newInstance()->_set('userLocale', '%');
    }
}

osc_add_hook('init', 'cust_alerts_locale_hack_start'); 

function cust_alerts_locale_hack_end($conditions) {

    if (osc_get_osclass_section() == "alerts") {
        Session::newInstance()->_set('userLocale', View::newInstance()->_get('aUserLocale'));
    }
    
    return $conditions;
}

osc_add_filter('sql_search_item_conditions', 'cust_alerts_locale_hack_end');
?>

--- End code ---

3rd version (Alerts compatible -no duplicate ads, No multilanguage on User Alerts)


--- Code: ---<?php
function cust_refined_pattern_search($params) {

    $mSearch =  Search::newInstance();

    if ($params['sPattern'] != '') {
        $mSearch->addJoinTable( null, sprintf("(SELECT DISTINCT(fk_i_item_id), MATCH(s_title, s_description) AGAINST('%s') AS relevance FROM %st_item_description ) des", $params['sPattern'], DB_TABLE_PREFIX), 'des.fk_i_item_id = '.DB_TABLE_PREFIX.'t_item.pk_i_id', 'INNER' );
        $mSearch->addLocale('%');
        $mSearch->addGroupBy("pk_i_id");
        $mSearch->order("des.relevance DESC, dt_pub_date", "DESC");
    }
}

osc_add_hook('search_conditions', 'cust_refined_pattern_search');

function cust_clean_duplicate_ads_user_alerts() {
    
    $clean_alerts = View::newInstance()->_get('alerts');

    if ($clean_alerts) {
        foreach ($clean_alerts as $key => $alert) {

            $outputArray = array();
            $keysArray = array();
            foreach ($clean_alerts[$key]['items'] as $innerArray) {
                if (!in_array($innerArray['pk_i_id'], $keysArray)) {
                    $keysArray[] = $innerArray['pk_i_id'];
                    $outputArray[] = $innerArray;
                }
            }

            $clean_alerts[$key]['items'] = $outputArray;
        }

    View::newInstance()->_exportVariableToView('alerts', $clean_alerts);
    }  
}

osc_add_hook('before_html', 'cust_clean_duplicate_ads_user_alerts');
?>
--- End code ---


2nd version (Alerts compatible -but including duplicate ads under certain circumstances)


--- Code: ---<?php
function cust_refined_pattern_search($params) {

    $mSearch =  Search::newInstance();

    if ($params['sPattern'] != '') {
        $mSearch->addJoinTable( null, sprintf("(SELECT DISTINCT(fk_i_item_id), MATCH(s_title, s_description) AGAINST('%s') AS relevance FROM %st_item_description ) des", $params['sPattern'], DB_TABLE_PREFIX), 'des.fk_i_item_id = '.DB_TABLE_PREFIX.'t_item.pk_i_id', 'INNER' );
        $mSearch->addLocale('%');
        $mSearch->order("des.relevance DESC, dt_pub_date", "DESC");
    }
}

osc_add_hook('search_conditions', 'cust_refined_pattern_search');
?>
--- End code ---

1st version (Alerts uncompatible)


--- Code: ---<?php
function cust_refined_pattern_search($params) {
    $mSearch =  Search::newInstance();
    
    if ($params['sPattern'] != '') {
        $mSearch->addField( sprintf("MATCH(d.s_title, d.s_description) AGAINST('%s') AS relevance", $params['sPattern']) );
        $mSearch->addLocale('%');
        $mSearch->order("relevance DESC, dt_pub_date", "DESC");
        $mSearch->addGroupBy("pk_i_id");
    }
}

osc_add_hook('search_conditions', 'cust_refined_pattern_search');
?>
--- End code ---

Regards

Navigation

[0] Message Index

[#] Next page

Go to full version