Skip to content

Instantly share code, notes, and snippets.

@steadweb
Created May 14, 2014 08:27
Show Gist options
  • Save steadweb/60b573d27c2760308136 to your computer and use it in GitHub Desktop.
Save steadweb/60b573d27c2760308136 to your computer and use it in GitHub Desktop.
Custom indexer which combines fields into one (example)
<?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