Advertisement:

Author Topic: Index Car Attibutes for Search Queries  (Read 2898 times)

richieboymx

  • Full Member
  • ***
  • Posts: 110
Index Car Attibutes for Search Queries
« on: November 28, 2014, 10:00:29 pm »
Hello to all the community,

As the title says... is it possible to index the car attributes of an ad for search queries? This way, if an ad has an attribute that matches the user query, it can be displayed on the search results along with the other ads that match in title/description.

Thanks a lot!

Ricardo A.

teseo

  • Hero Member
  • *****
  • Posts: 6169
Re: Index Car Attibutes for Search Queries
« Reply #1 on: November 28, 2014, 11:02:25 pm »
Hi,

There is a little trick here:

http://forums.osclass.org/bender/how-to-add-fields-from-another-table-in-search-form

The code:

http://forums.osclass.org/bender/how-to-add-fields-from-another-table-in-search-form/msg103808/#msg103808

This only contemplates "Make" though. This plugin has a lot of fields, and a part of them wouldn't mean anything when searching, so I could adapt this to your needs, but you have to select first which fields should be added to the search and let me know.

Regards

richieboymx

  • Full Member
  • ***
  • Posts: 110
Re: Index Car Attibutes for Search Queries
« Reply #2 on: November 29, 2014, 03:52:09 am »
Hi teseo,

I've read the full post and you're trick is pretty amazing and exactly what I need. I would edit the code myself and save you the time and effort of helping a stranger, but I honestly couldn't figure out your code (no expert, but learned a bit by playing with OSClass) hahaha.

I have in fact modified the Cars Plugin to match my needs and modified some of the fields and sql tables, so if I had to choose, I would go with the stock Make, Model and Years tables, and perhaps try later to add one of my customized tables to your code on my own.

Thanks in advance, and very much appreciated your help my friend!

Ricardo A.


teseo

  • Hero Member
  • *****
  • Posts: 6169
Re: Index Car Attibutes for Search Queries
« Reply #3 on: November 29, 2014, 09:17:08 pm »
***INCOMPLETE METHOD, read the rest of this thread***

Hi again,

Here it is:

1.- Add this at the very bottom of your theme functions.php (take care not to leave blank lines after this):

Code: [Select]
<?php
function cust_item_description($locale "") {
    if (
$locale == ""$locale osc_current_user_locale();
    
$desc osc_item_field("s_description"$locale);
    if(
$desc=='') {
        
$desc osc_item_field("s_description"osc_language());
        if(
$desc=='') {
            
$aLocales osc_get_locales();
            foreach(
$aLocales as $locale) {
                
$desc osc_item_field("s_description", @$locale['pk_c_code']);
                if(
$desc!='') {
                    break;
                }
            }
        }
    }

    return 
preg_replace('~\\\\n\\\\0x07\\\\0x12.*$~m'''$desc);
}

function 
cust_clean_edit_description($item) {

    
$locale osc_current_user_locale();
    
$desc osc_item_field("s_description"$locale);
    if(
$desc=='') {
        
$desc osc_item_field("s_description"osc_language());
        if(
$desc=='') {
            
$aLocales osc_get_locales();
            foreach(
$aLocales as $locale) {
                
$desc osc_item_field("s_description", @$locale['pk_c_code']);
                if(
$desc!='') {
                    
$current_locale $locale['pk_c_code'];
                    break;
                }
            }
        }
    } else  
$current_locale $locale;

    
$item['locale'][$current_locale]['s_description'] = preg_replace('~\\\\n\\\\0x07\\\\0x12.*$~m'''$desc);
    
View::newInstance()->_exportVariableToView('item'$item);
}

osc_add_hook('before_item_edit''cust_clean_edit_description');

function 
cust_add_ca_to_description($item) {

    
$conn getConnection();
    
$car_data $conn->osc_dbFetchResult("SELECT a.*, mk.s_name AS make, mo.s_name AS model, vt.s_name AS vehicle_type
FROM %st_item_car_attr a
INNER JOIN %st_item_car_make_attr mk ON mk.pk_i_id = a.fk_i_make_id
INNER JOIN %st_item_car_model_attr mo ON mo.pk_i_id = a.fk_i_model_id
INNER JOIN %st_item_car_vehicle_type_attr vt ON vt.pk_i_id = a.fk_vehicle_type_id
WHERE a.fk_i_item_id = '%d'"
DB_TABLE_PREFIXDB_TABLE_PREFIXDB_TABLE_PREFIXDB_TABLE_PREFIX$item['pk_i_id']);

    
$title = array();
    
$description = array();

    foreach (
$item['locale'] as $key => $title_desc) {
        
$title[$key] = $title_desc['s_title'];
        
$description[$key] = $title_desc['s_description'] .= '\n\0x07\0x12 '$car_data['make'] . ' ' $car_data['model'] . ' ' $car_data['i_year'] . ' ' $car_data['vehicle_type'];
    }

    
$mItem = new ItemActions(false);
    
$mItem->insertItemLocales('EDIT'$title$description$item['pk_i_id']);

}

osc_add_hook('posted_item''cust_add_ca_to_description');
osc_add_hook('edited_item''cust_add_ca_to_description');
?>

(Explanation: cust_add_ca_to_description($item) function is run after a new ad is posted and it appends to the description/s of that ad a unique identifier, 2 standard control characters -bell and form feed, virtually impossible they appear together in a normal message- plus the content of selected Car Attributes fields corresponding to that ad).

2.- Replace all occurrences of osc_item_description() in your theme and plugins scripts with cust_item_description(), this will ensure that users never see the appended Car Attributes fields.

.............

Now, if you want to add more fields, these are the field names you can use:

Code: [Select]
    'fk_i_item_id' => string (2) "95"

    'i_year' => NULL

    'i_doors' => string (1) "0"

    'i_seats' => string (1) "0"

    'i_mileage' => NULL

    'i_engine_size' => NULL

    'fk_i_make_id' => string (1) "7"

    'fk_i_model_id' => string (2) "58"

    'i_num_airbags' => string (1) "0"

    'e_transmission' => string (6) "MANUAL"

    'e_fuel' => string (8) "GASOLINE"

    'e_seller' => string (6) "DEALER"

    'b_warranty' => string (1) "0"

    'b_new' => string (1) "0"

    'i_power' => NULL

    'e_power_unit' => string (2) "KW"

    'i_gears' => string (1) "0"

    'fk_vehicle_type_id' => string (1) "1"

    'make' => string (8) "Cadillac"

    'model' => string (7) "Deville"

    'vehicle_type' => string (11) "Convertible"

For instance, to add "Vehicle type":

Quote
$description[$key] = $title_desc['s_description'] .= '\n\0x07\0x12 '. $car_data['make'] . ' ' . $car_data['model'] . ' ' . $car_data['i_year'] . ' ' . $car_data['vehicle_type'];

Regards
« Last Edit: December 04, 2014, 12:18:20 am by teseo »

richieboymx

  • Full Member
  • ***
  • Posts: 110
Re: Index Car Attibutes for Search Queries
« Reply #4 on: December 01, 2014, 08:26:08 pm »
Thanks a lot Teseo, this is gold.

And by plugins, you mean absolutely all plugins that I have installed and configured that have the osc_item_description() string correct? Do you know for certain on which ones to start looking?

Regards,

Ricardo A.

teseo

  • Hero Member
  • *****
  • Posts: 6169
Re: Index Car Attibutes for Search Queries
« Reply #5 on: December 01, 2014, 10:06:56 pm »
You're welcome. :)

Use an advanced text editor such Notepad++ to find and massively replace osc_item_description throughout a bunch of files.

Suggested procedure:

1.- Download your oc-content/plugins and oc-content/themes/[your theme] folders to your computer.
2.- Open Notepad++, go to Search / Find in Files / Find in Files

3.- Find what: osc_item_description
     Replace with: cust_item_description
     Directory: (Browse or paste here the path of downloaded oc-content/plugins folder)
     
     - First click on "Find All". This will open a new tab "Find Results". Click on first minus sign and then on second plus sign: those are the files where replacements will occur. Copy that list so you won't need to re-upload the whole lot, just the modified files.

      - Then you can return to "Find in Files" and click on "Replace in Files" to make the actual substitution.

      Repeat this process for downloaded oc-content/themes/[your theme]

4.- Re-upload modified files.

Regards


richieboymx

  • Full Member
  • ***
  • Posts: 110
Re: Index Car Attibutes for Search Queries
« Reply #6 on: December 03, 2014, 01:40:54 am »
Thanks teseo, that was super easy actually... this is fantastic.

I also have 2 more questions: how to add vehicle type? I tried adding $car_data['fk_vehicle_type_id'] at the end as you said, but the info shows a number only. Also, I noticed that every time the ad is updated, the information gets added again. Is it possible to make this tweak delete the old added string and replace it with the new one? Because it gets me thinking that this information after maybe some changes or updates the first values will be obsolete, but still searchable.

For example, one of the fields I added was color: so let's say user adds 2014 Ford Mustang Blue, and then realizes is not blue, so changes color to White. At this time, description would have something like this \n\0x07\0x12 Ford Mustang 2014 13 Blue\n\0x07\0x12 Ford Mustang 2014 13 White; where if someone searches for a blue Camaro, the white Mustang will be shown as well.

Thanks again!

teseo

  • Hero Member
  • *****
  • Posts: 6169
Re: Index Car Attibutes for Search Queries
« Reply #7 on: December 03, 2014, 09:31:02 pm »
how to add vehicle type? I tried adding $car_data['fk_vehicle_type_id'] at the end as you said, but the info shows a number only.

Yes, it's a number because is an ID, its corresponding name is stored in yet another table that I was missing. Now I've added it to the query, check my corrected post.

Quote
For example, one of the fields I added was color: so let's say user adds 2014 Ford Mustang Blue, and then realizes is not blue, so changes color to White. At this time, description would have something like this \n\0x07\0x12 Ford Mustang 2014 13 Blue\n\0x07\0x12 Ford Mustang 2014 13 White; where if someone searches for a blue Camaro, the white Mustang will be shown as well.

Yup, ad edition is not managed by theme or plugin scripts, so we need another function to clean up ad description before edition. I've added a new function cust_clean_edit_description($item) to take care of that.

Change the code according to new contents of my previous post.

Regards

richieboymx

  • Full Member
  • ***
  • Posts: 110
Re: Index Car Attibutes for Search Queries
« Reply #8 on: December 03, 2014, 10:08:59 pm »
Hi teseo,

The new code worked perfectly... thank you very much!!!

However, i'm facing issues when I replace osc_item_description for cust_item_description. In some plugins the entire description disappears and in Item.php, the social plugin buttons disappear (to name a example). Instead of changing the description source, is it possible to add the information in a custom color? That way it could be disguised with the background (white for me), and will not be visible to user. What do you think?

Regards,

Ricardo A.

teseo

  • Hero Member
  • *****
  • Posts: 6169
Re: Index Car Attibutes for Search Queries
« Reply #9 on: December 03, 2014, 10:47:52 pm »
 :(

Hm... This matter has proven to be much harder that I anticipated. I think there is a much simpler solution to all this, a small modification on core function osc_item_description.

Yes, definitely better this than a lot of modifications on theme and plugins (and more to come, as it seems).

Find in oc-includes/osclass/helpers/hItems.php:

Code: [Select]
return (string) $desc;
and replace it with:

Code: [Select]
return (string) preg_replace('~\\\\n\\\\0x07\\\\0x12.*$~m', '', $desc);
That's all. Check now everything, including those plugins, and let me know if everything's all right.

Regards

richieboymx

  • Full Member
  • ***
  • Posts: 110
Re: Index Car Attibutes for Search Queries
« Reply #10 on: December 03, 2014, 11:13:28 pm »
Sorry teseo, didn't work  :-[

Same thing happens with the social plugin on item.php and printpdf (where the description shows empty). On search_list.php, it works perfectly.

Sorry it's taking longer than anticipated.

Regards,

teseo

  • Hero Member
  • *****
  • Posts: 6169
Re: Index Car Attibutes for Search Queries
« Reply #11 on: December 04, 2014, 12:15:54 am »
Well, as you can see there are quite a few ways to get item data. Haven't checked that Social plugin, but I can see that Print PDF retrieves the ad data directly from the database. Social plugin must be doing the same thing, but it might even be using yet another mechanism. :(

This will happen with other plugins, so at this point, I only can conclude that this hack is not recommendable, too much variables.  :o Probably would be easier to add "searchability" to the very Cars Attributes plugin, and that would mean a lot of work, far beyond my intention here.

Sorry, regards

richieboymx

  • Full Member
  • ***
  • Posts: 110
Re: Index Car Attibutes for Search Queries
« Reply #12 on: December 04, 2014, 01:38:09 am »
Thank you teseo for all your help and time. It is a great hack.

Regards,