/** * Preprocess aggregations params before they are used into the aggregation factory. * * @param FieldInterface $field Bucket field. * @param array $aggregationParams Aggregation params. * @param array $filters Filter applied to the search request. * * @return array */ private function getBucketParams(FieldInterface $field, array $aggregationParams, array $filters) { $bucketField = $field->getMappingProperty(FieldInterface::ANALYZER_UNTOUCHED); if ($bucketField === null) { throw new \LogicException("Unable to init the filter field for {$field->getName()}"); } $bucketParams = ['field' => $bucketField, 'name' => $field->getName(), 'metrics' => [], 'filter' => array_diff_key($filters, [$field->getName() => true])]; $bucketParams += $aggregationParams['config']; if (empty($bucketParams['filter'])) { unset($bucketParams['filter']); } if ($field->isNested() && !isset($bucketParams['nestedPath'])) { $bucketParams['nestedPath'] = $field->getNestedPath(); } elseif ($field->isNested() === false && isset($bucketParams['nestedPath'])) { unset($bucketParams['nestedPath']); } return $bucketParams; }
/** * Append a field to a mapping properties list. * The field is append and the new properties list is returned. * * @param array $properties Initial properties map. * @param FieldInterface $field Field to be added. * * @return array */ private function addField(array $properties, FieldInterface $field) { $fieldName = $field->getName(); $fieldRoot =& $properties; // Read property config from the field. $property = $field->getMappingPropertyConfig(); if ($field->isNested()) { /* * Nested field management : * * For nested field we need to * - change the insertion root to the parent field. * - create the parent field with type nested if not yet exists. * - using the suffix name of the field instead of the name including nested path. * * Ex: "price.is_discount" field has to be inserted with name "is_discount" into the "price" field. * */ $nestedPath = $field->getNestedPath(); if (!isset($properties[$nestedPath])) { $properties[$nestedPath] = ['type' => FieldInterface::FIELD_TYPE_NESTED, 'properties' => []]; } $fieldRoot =& $properties[$nestedPath]['properties']; $fieldName = $field->getNestedFieldName(); } elseif (strstr($fieldName, '.')) { $fieldPathArray = explode('.', $fieldName); if (!isset($properties[current($fieldPathArray)])) { $properties[current($fieldPathArray)] = ['type' => FieldInterface::FIELD_TYPE_OBJECT, 'properties' => []]; } $fieldRoot =& $properties[current($fieldPathArray)]['properties']; $fieldName = end($fieldPathArray); } /* * Retrieving location where the property has to be copied to. * Ex : searchable fields are copied to default "search" field. */ $copyToProperties = $this->getFieldCopyToProperties($field); if (!empty($copyToProperties)) { // For normal fields, copy_to is append at the property root. $copyToRoot =& $property; if ($property['type'] == FieldInterface::FIELD_TYPE_MULTI) { /* * For field with type "multi_field", the copy_to has to be added in the * default subfield. * This is changing the root. */ $copyToRoot =& $property['fields'][$fieldName]; } $copyToRoot['copy_to'] = $copyToProperties; } $fieldRoot[$fieldName] = $property; return $properties; }