Skip to content

Instantly share code, notes, and snippets.

@jappievw
Last active December 15, 2015 09:29
Show Gist options
  • Save jappievw/5238757 to your computer and use it in GitHub Desktop.
Save jappievw/5238757 to your computer and use it in GitHub Desktop.
Setting dynamic templates with Elastica
<?php
/**
* Gather some prerequisites.
*/
$es_client = new Elastica_Client(array('host' => '127.0.0.1', 'port' => 9200));
$es_index = $es_client->getIndex('yourindex');
$es_type = $es_index->getType('yourtype');
/**
* Initialize a mapping object.
*/
$mapping = new Elastica_Type_Mapping();
/**
* Set the default, on beforehand known, properties of the document type.
*/
$mapping->setProperties(array(
'name' => array('type' => 'string', 'index' => 'not_analyzed'),
'price' => array('type' => 'float', 'index' => 'not_analyzed', 'include_in_all' => false),
));
/**
* Set the dynamic mapping, based on these rules:
* http://www.elasticsearch.org/guide/reference/mapping/root-object-type.html
*/
$mapping->setParam('dynamic_templates', array(
// used for faceting and filtering of the new attributes
array('attribute_identifier' => array(
'match' => 'attribute_identifier_*',
'mapping' => array('type' => 'string', 'index' => 'not_analyzed', 'include_in_all' => false),
)),
// used when a user performs a full text search.
array('attribute_search' => array(
'match' => 'attribute_search_*',
'mapping' => array('type' => 'string', 'omit_norms' => true),
)),
));
/**
* Set the mapping for the type in Elasticsearch.
*/
$res = $es_type->setMapping($mapping);
if ($res->isOk())
{
echo "The mapping with dynamic fields has been set!";
}
else
{
echo "Could not set the mapping with the dynamic templates. Error is shown below:" . PHP_EOL;
echo $res->getError();
exit 1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment