/** * 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; }
/** * Retrieve base params for a sort order field. * * @param FieldInterface $field Sort order field. * @param array $sortOrderParams Sort order params. * * @return array */ private function getSortOrderParams(FieldInterface $field, array $sortOrderParams) { $sortOrderParams['field'] = $field->getMappingProperty(FieldInterface::ANALYZER_SORTABLE); if ($field->isNested() && !isset($sortOrderParams['nestedPath'])) { $sortOrderParams['nestedPath'] = $field->getNestedPath(); } return $sortOrderParams; }
/** * Transform the condition into a search request query object. * * @param FieldInterface $field Filter field. * @param array|string $condition Filter condition. * * @return QueryInterface */ private function prepareFieldCondition(FieldInterface $field, $condition) { $queryType = QueryInterface::TYPE_TERMS; $condition = $this->prepareCondition($condition); if (count(array_intersect(['gt', 'gte', 'lt', 'lte'], array_keys($condition))) >= 1) { $queryType = QueryInterface::TYPE_RANGE; $condition = ['bounds' => $condition]; } $condition['field'] = $field->getMappingProperty(FieldInterface::ANALYZER_UNTOUCHED); if ($condition['field'] === null) { $condition['field'] = $field->getMappingProperty(FieldInterface::ANALYZER_STANDARD); } if (in_array('queryText', array_keys($condition))) { $queryType = QueryInterface::TYPE_MATCH; $condition['minimumShouldMatch'] = '100%'; } $query = $this->queryFactory->create($queryType, $condition); if ($field->isNested()) { $queryParams = ['path' => $field->getNestedPath(), 'query' => $query]; $query = $this->queryFactory->create(QueryInterface::TYPE_NESTED, $queryParams); } return $query; }