Advertisement:

Author Topic: Custom field search  (Read 5440 times)

Sophia_OS

  • Sr. Member
  • ****
  • Posts: 416
Custom field search
« on: January 08, 2018, 04:39:30 am »
Hi everyone,
i wanna search custom field's value in website's main search box, not with its own box.
for example if there is a custom field (color) and its value is "red" and "blue" and "green" , if you search "blue" in the website's main search box, it shows this item in searched list too.
thank you
theme bender
« Last Edit: March 08, 2019, 11:50:00 am by Sophia_OS »

fre2mansur

  • Hero Member
  • *****
  • Posts: 711
Re: please help to do this
« Reply #1 on: January 09, 2018, 01:49:12 pm »
its possible by doing as plugin.. post on job section.. dev\s can help you

Sophia_OS

  • Sr. Member
  • ****
  • Posts: 416
Re:
« Reply #2 on: January 10, 2018, 08:12:27 am »
its possible by doing as plugin.. post on job section.. dev\s can help you

@fre2mansur
Thank you for your reply,
Nobody helps us here for free to do this?

marius-ciclistu

  • issues
  • Hero Member
  • *
  • Posts: 1652
  • "BE GRATEFUL TO THOSE THAT SUPPORTED YOU"
Re: Custom field search
« Reply #3 on: January 16, 2018, 04:58:48 pm »
EDIT These hooks are deprecated

Try adding the custom field's values to the end of the description before sending it to the db when the item is saved. There are some hooks: pre_item_add and pre_item_edit


« Last Edit: January 18, 2018, 01:55:51 pm by marius-ciclistu »

marius-ciclistu

  • issues
  • Hero Member
  • *
  • Posts: 1652
  • "BE GRATEFUL TO THOSE THAT SUPPORTED YOU"
Re: Custom field search
« Reply #4 on: January 18, 2018, 01:34:49 pm »
Sorry, pre_item_edit and pre_item_add are deprecated .

use item_add_prepare_data and item_edit_prepare_data instead

In your theme's functions.php file, add to the end


EDITED I reversed the mc_edit_filter_description($aItem) and mc_add_filter_description($aItem) functions
I splitted mc_add_metas in mc_add_metas_item_edit and mc_add_metas_item_add


Code: [Select]
<?php
    
function endsWith($haystack$needle)
    {
    
$length strlen($needle);

    return 
$length === || 
    (
substr($haystack, -$length) === $needle);
    }

    function 
mc_add_metas_item_add($desc$aItem) {
            
$_meta Field::newInstance()->findByCategory($aItem['catId']);
            
$meta Params::getParam("meta");

            foreach(
$_meta as $_m) {
                
$meta[$_m['pk_i_id']] = (isset($meta[$_m['pk_i_id']]))?$meta[$_m['pk_i_id']]:'';
            }

    
$d '\n\n  ';
    
$mField Field::newInstance();
    foreach(
$meta as $k => $v) {
     
$field $mField->findByPrimaryKey($k);

     
$d $d $field['s_name'] .': ' .$v'\n'
    }
    if(
endsWith($desc$d))
    return 
$desc;
    else return 
$desc $d;
    }

    function 
mc_add_metas_item_edit($desc$itemId) {

    
$metas Item::newInstance()->metaFields($item_Id);
    
$d '\n\n  ';
    foreach(
$metas as $m) {
    
$d $d $m["s_name"] .': ' .$m["s_value"] . '\n'
    }
    if(
endsWith($desc$d))
    return 
$desc;
    else return 
$desc $d;
    }

    function 
mc_edit_filter_description($aItem) {
    
        foreach(@
$aItem['description'] as $key => $value) {
            
$aItem['description'][$key] = mc_add_metas_item_edit($value,$aItem['idItem']);
        }
        
        return 
$aItem;
    }

    function 
mc_add_filter_description($aItem) {
   
        foreach(@
$aItem['description'] as $key => $value) {
            
$aItem['description'][$key] = mc_add_metas_item_add($value,$aItem);
        }
        
        return 
$aItem;
    }
    
    
osc_add_filter('item_add_prepare_data''mc_add_filter_description');
    
osc_add_filter('item_edit_prepare_data''mc_edit_filter_description');
?>

 or if you don't want the custom field's name there remove
 
Code: [Select]
$m["s_name"] .': ' .

and

Code: [Select]
$field['s_name'] .': ' .

After this you must update the descriptions in your db by editing and saving each item.




Step 2

You would want to remove the added text from description when displaying the description to the public and to the owner when it edits hi's item.
You can do that  with a function
Put this before the above code in functions.php
Code: [Select]
<?php
    
function mc_hide_metas($desc$itemId) {

    
$metas Item::newInstance()->metaFields($item_Id);
    
$d '\n\n  ';
    foreach(
$metas as $m) {
    
$d $d $m["s_name"] .': ' .$m["s_value"] . '\n'
    }
    if(
endsWith($desc$d))
    return 
str_replace($d,'',$desc);
    else return 
$desc;
    }
?>

 or if you don't want the custom field's name there remove
 
Code: [Select]
$m["s_name"] .': ' .

and call this function where the description is shown. Have a look here https://stackoverflow.com/questions/43015613/how-to-modify-ft-min-word-len-4-to-ft-min-word-len-1-so-that-osclass-3-7-1-can-s
And see where I call 'removeunderline(' for description.

If you do this admin and user side the solution will work if the item is edited and it's category is changed and or it's custom fields are changed.


If the admin makes the editing and the above function can't be called from oc-admin/themes/modern... files, the last function from above must be placed in /oc-includes/osclass/helpers/hSearch.php and removed from functions.php file.
« Last Edit: January 20, 2018, 12:43:38 pm by marius-ciclistu »

marius-ciclistu

  • issues
  • Hero Member
  • *
  • Posts: 1652
  • "BE GRATEFUL TO THOSE THAT SUPPORTED YOU"
Re: Custom field search
« Reply #5 on: January 18, 2018, 02:35:36 pm »
This is a related solution for including category tree to the main search's results.

https://stackoverflow.com/questions/44529922/search-shows-nothing-if-user-types-category-name-in-search-bar-in-osclass/48318427#48318427

Anyway I think that including custom fields values to the search results is better than categories names.

Maybe someone can solve the above question mark about the itemId marked with red.

marius-ciclistu

  • issues
  • Hero Member
  • *
  • Posts: 1652
  • "BE GRATEFUL TO THOSE THAT SUPPORTED YOU"
Re: Custom field search
« Reply #6 on: January 18, 2018, 03:15:18 pm »
Here, https://forums.osclass.org/3-5-x/custom-field-search-when-'all-categories'/msg159218/#msg159218
Teseo said something about
DBCommandClass

It's in DBCommandClass.php

Variable "dao" is assigned in DAO.php as an instance of DBCommandClass class:

$this->dao         = new DBCommandClass($data);

Regards

How to get the item id then?

  $itemId = $this->manager->dao->insertedId();
« Last Edit: January 20, 2018, 12:29:28 pm by marius-ciclistu »

Sophia_OS

  • Sr. Member
  • ****
  • Posts: 416
Re: Custom field search
« Reply #7 on: January 20, 2018, 03:05:50 am »
@marius-ciclistu
Thank you very much for helping,
Is there a way just by a function in function.php? And not to make mess with description?
« Last Edit: January 20, 2018, 03:12:20 am by Sophia_OS »

marius-ciclistu

  • issues
  • Hero Member
  • *
  • Posts: 1652
  • "BE GRATEFUL TO THOSE THAT SUPPORTED YOU"
Re: Custom field search
« Reply #8 on: January 20, 2018, 10:37:35 am »
From what I know, the search is made in the indexes made in db from title and description, so this is the easiest way to not slow down the search queries.

Maybe others have a better idea. Consider ft_min_word_len =1 if you have custom fields made up from only 1character.

Maybe others could help with indexing metas and extend the search on them also.

https://doc.osclass.org/Fine-Tuning_MySQL_Full-Text_Search_-_Improving_search
« Last Edit: January 20, 2018, 11:29:31 am by marius-ciclistu »

marius-ciclistu

  • issues
  • Hero Member
  • *
  • Posts: 1652
  • "BE GRATEFUL TO THOSE THAT SUPPORTED YOU"
Re: Custom field search
« Reply #9 on: January 20, 2018, 12:13:52 pm »
Now that  I think about it, mc_add_metas($desc, $itemId)  will not work when posting a new add....

because by the time the filter runs,
 $metas = Item::newInstance()->metaFields($item_Id);
won't return nothing..
hmm how to get those metas...
I gues like this:

            $_meta = Field::newInstance()->findByCategory($aItem['catId']);
            $meta = Params::getParam("meta");

EDIT.

I modified the solution. I think now it should work also for item-add.
« Last Edit: January 20, 2018, 12:31:01 pm by marius-ciclistu »

marius-ciclistu

  • issues
  • Hero Member
  • *
  • Posts: 1652
  • "BE GRATEFUL TO THOSE THAT SUPPORTED YOU"
Re: Custom field search
« Reply #10 on: January 20, 2018, 12:58:28 pm »
I would implemet this on my site also but from my point of view it has no sense. You search for something and then you can filter your search via search-sidebar after the custom fields values.

Sophia_OS

  • Sr. Member
  • ****
  • Posts: 416
Re: Custom field search
« Reply #11 on: January 23, 2018, 02:40:48 am »
I would implemet this on my site also but from my point of view it has no sense. You search for something and then you can filter your search via search-sidebar after the custom fields values.

 :'(

marius-ciclistu

  • issues
  • Hero Member
  • *
  • Posts: 1652
  • "BE GRATEFUL TO THOSE THAT SUPPORTED YOU"
Re: Custom field search
« Reply #12 on: January 23, 2018, 10:00:59 am »
Anyway I'm curious if there is a better solution than this, without slowing down the search query.

EDIT.
How many items does the site have?
« Last Edit: January 23, 2018, 10:27:43 am by marius-ciclistu »

Sophia_OS

  • Sr. Member
  • ****
  • Posts: 416
Re: Custom field search
« Reply #13 on: January 24, 2018, 02:56:33 am »
Anyway I'm curious if there is a better solution than this, without slowing down the search query.

EDIT.
How many items does the site have?


More than 1,000,000 items
« Last Edit: January 24, 2018, 02:58:17 am by Sophia_OS »

marius-ciclistu

  • issues
  • Hero Member
  • *
  • Posts: 1652
  • "BE GRATEFUL TO THOSE THAT SUPPORTED YOU"
Re: Custom field search
« Reply #14 on: January 24, 2018, 10:36:27 am »
Nice job :)
If nobody comes with a bether solution for searching in metas too, then a cron job could be made to update all items' descriptions in groups by their ids each hour (to avoid updating them all at once).