Example #1
0
 public static function wrapPrivateFieldQuery(Field $field, array $query)
 {
     if ($field->isPrivate()) {
         return self::restrictQueryToCollections($query, $field->getDependantCollections());
     } else {
         return $query;
     }
 }
Example #2
0
 /**
  * @todo Maybe we should put this logic in Field class?
  */
 public function localizeField(Field $field)
 {
     $index_field = $field->getIndexField();
     if ($field->getType() === Mapping::TYPE_STRING) {
         return $this->localizeFieldName($index_field);
     } else {
         return [$index_field];
     }
 }
 public static function wrapPrivateFieldAggregation(Field $field, array $aggregation)
 {
     if ($field->isPrivate()) {
         $wrapper = [];
         $wrapper['filter']['terms']['base_id'] = $field->getDependantCollections();
         $wrapper['aggs']['__wrapped_private_field__'] = $aggregation;
         return $wrapper;
     } else {
         return $aggregation;
     }
 }
Example #4
0
 public function add(Field $field)
 {
     $name = $field->getName();
     if (isset($this->fields[$name])) {
         $field = $this->fields[$name]->mergeWith($field);
     }
     $this->fields[$name] = $field;
     if ($field->getType() === Mapping::TYPE_DATE) {
         $this->date_fields[$name] = $field;
     }
     if ($field->isPrivate()) {
         $this->private[$name] = $field;
     }
     if ($field->isFacet() && $field->isSearchable()) {
         $this->facets[$name] = $field;
     }
     if ($field->hasConceptInference()) {
         $this->thesaurus_fields[$name] = $field;
     }
 }
 private function limitField(Field $field, array $allowed_collections = null)
 {
     if ($allowed_collections === null) {
         $allowed_collections = $this->allowedCollections();
     }
     $collections = array_values(array_intersect($field->getDependantCollections(), $allowed_collections));
     return $field->withOptions(['used_by_collections' => $collections]);
 }
Example #6
0
 public function testCollectionsUsedByPrivateFields()
 {
     $structure = new Structure();
     $structure->add($foo = new Field('foo', Mapping::TYPE_STRING, ['private' => true, 'used_by_collections' => [1, 2]]));
     $structure->add(new Field('foo', Mapping::TYPE_STRING, ['private' => true, 'used_by_collections' => [2, 3]]));
     $structure->add(new Field('bar', Mapping::TYPE_STRING, ['private' => true, 'used_by_collections' => [2, 3]]));
     $structure->add(new Field('baz', Mapping::TYPE_STRING, ['private' => false]));
     $this->assertEquals([1, 2], $foo->getDependantCollections());
     static $expected = ['foo' => [1, 2, 3], 'bar' => [2, 3]];
     $this->assertEquals($expected, $structure->getCollectionsUsedByPrivateFields());
 }
Example #7
0
 /**
  * Merge with another field, returning the new instance
  *
  * @param Field $other
  * @return Field
  * @throws MergeException
  */
 public function mergeWith(Field $other)
 {
     if (($name = $other->getName()) !== $this->name) {
         throw new MergeException(sprintf("Fields have different names (%s vs %s)", $this->name, $name));
     }
     // Since mapping is merged between databoxes, two fields may
     // have conflicting names. Indexing is the same for a given
     // type so we reject only those with different types.
     if (($type = $other->getType()) !== $this->type) {
         throw new MergeException(sprintf("Field %s can't be merged, incompatible types (%s vs %s)", $name, $type, $this->type));
     }
     if ($other->isPrivate() !== $this->is_private) {
         throw new MergeException(sprintf("Field %s can't be merged, could not mix private and public fields with same name", $name));
     }
     if ($other->isSearchable() !== $this->is_searchable) {
         throw new MergeException(sprintf("Field %s can't be merged, incompatible searchablility", $name));
     }
     if ($other->getFacetValuesLimit() !== $this->facet) {
         throw new MergeException(sprintf("Field %s can't be merged, incompatible facet eligibility", $name));
     }
     $thesaurus_roots = null;
     if ($this->thesaurus_roots !== null || $other->thesaurus_roots !== null) {
         $thesaurus_roots = array_merge((array) $this->thesaurus_roots, (array) $other->thesaurus_roots);
     }
     $used_by_collections = array_values(array_unique(array_merge($this->used_by_collections, $other->used_by_collections), SORT_REGULAR));
     return $this->withOptions(['thesaurus_roots' => $thesaurus_roots, 'used_by_collections' => $used_by_collections]);
 }
Example #8
0
 public function testMergeWithDependantCollections()
 {
     $field = new Field('foo', Mapping::TYPE_STRING, ['used_by_collections' => [1, 2]]);
     $other = new Field('foo', Mapping::TYPE_STRING, ['used_by_collections' => [2, 3]]);
     $merged = $field->mergeWith($other);
     $this->assertEquals([1, 2, 3], $merged->getDependantCollections());
 }
Example #9
0
 private function addFieldToMapping(Field $field, Mapping $mapping)
 {
     $type = $field->getType();
     $mapping->add($field->getName(), $type);
     if ($type === Mapping::TYPE_DATE) {
         $mapping->format(Mapping::DATE_FORMAT_CAPTION);
     }
     if ($type === Mapping::TYPE_STRING) {
         $searchable = $field->isSearchable();
         $facet = $field->isFacet();
         if (!$searchable && !$facet) {
             $mapping->notIndexed();
         } else {
             $mapping->addRawVersion();
             $mapping->addAnalyzedVersion($this->locales);
             $mapping->enableTermVectors(true);
         }
     }
 }