Created
May 14, 2014 08:27
-
-
Save steadweb/60b573d27c2760308136 to your computer and use it in GitHub Desktop.
Custom indexer which combines fields into one (example)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// put into fuel/app/classes/elasticsearch | |
namespace Elasticsearch; | |
class Index_MyProduct extends Index_Product | |
{ | |
protected static function _get_products_full_fields() { | |
$fields = parent::_get_products_full_fields(); | |
$properties = \Arr::get($fields, 'variants.properties.combined_fields.properties', []); | |
// Add your own properties | |
$properties['colour'] = [ | |
'type' => 'string', | |
'index' => 'not_analyzed', | |
]; | |
$properties['material'] = [ | |
'type' => 'string', | |
'index' => 'not_analyzed', | |
]; | |
\Arr::set($fields, 'variants.properties.combined_fields.properties', $properties); | |
return $fields; | |
} | |
public static function make_product_full_doc($product) { | |
$doc = parent::make_product_full_doc($product); | |
// Change these as required | |
static::_combine_field($doc, 'colour_', 'colour'); | |
static::_combine_field($doc, 'material_', 'material'); | |
return $doc; | |
} | |
protected static function _combine_field($doc, $prefix, $field) { | |
$variants = $doc->variants; | |
foreach ($variants as &$variant) { | |
$vals = []; | |
// Logic will change here depending on | |
// what fields are called. | |
foreach ($variant['combined_fields'] as $name=>$value) { | |
if (substr($name, 0, strlen($prefix)) == $prefix) { | |
$value = (array)$value; | |
$vals = \Arr::merge($vals, $value); | |
} | |
} | |
$variant['combined_fields'][$field] = $vals; | |
} | |
$doc->variants = $variants; | |
return $doc; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment