private function createFilters(SearchEngineOptions $options) { $filters = []; $status_opts = $options->getStatus(); foreach ($options->getDataboxes() as $databox) { foreach ($databox->get_statusbits() as $n => $status) { if (!array_key_exists($n, $status_opts)) { continue; } if (!array_key_exists($databox->get_sbas_id(), $status_opts[$n])) { continue; } $filters[] = ['term' => ['status.status-' . $n => $status_opts[$n][$databox->get_sbas_id()]]]; } } $filters[] = ['terms' => ['base_id' => array_map(function (\collection $coll) { return $coll->get_base_id(); }, $options->getCollections())]]; $filters[] = ['term' => ['type' => $options->getSearchType() === SearchEngineOptions::RECORD_RECORD ? 'record' : 'story']]; if ($options->getDateFields() && ($options->getMaxDate() || $options->getMinDate())) { $range = []; if ($options->getMaxDate()) { $range['lte'] = $options->getMaxDate()->format(DATE_ATOM); } if ($options->getMinDate()) { $range['gte'] = $options->getMinDate()->format(DATE_ATOM); } foreach ($options->getDateFields() as $dateField) { $filters[] = ['range' => ['caption.' . $dateField->get_name() => $range]]; } } if ($options->getRecordType()) { $filters[] = ['term' => ['phrasea_type' => $options->getRecordType()]]; } return $filters; }
/** * Reset sphinx client and apply the options * * Only apply filters and group by * * @param SearchEngineOptions $options * @return SphinxSearch */ protected function applyOptions(SearchEngineOptions $options) { $this->resetSphinx(); $filters = []; foreach ($options->getCollections() as $collection) { $filters[] = sprintf("%u", crc32($collection->get_databox()->get_sbas_id() . '_' . $collection->get_coll_id())); } $this->sphinx->SetFilter('crc_sbas_coll', $filters); $this->sphinx->SetFilter('deleted', [0]); $this->sphinx->SetFilter('parent_record_id', [$options->getSearchType()]); if ($options->getDateFields() && ($options->getMaxDate() || $options->getMinDate())) { foreach (array_unique(array_map(function (\databox_field $field) { return $field->get_name(); }, $options->getDateFields())) as $field) { $min = $options->getMinDate() ? $options->getMinDate()->format('U') : 0; $max = $options->getMaxDate() ? $options->getMaxDate()->format('U') : pow(2, 32); $this->sphinx->SetFilterRange(ConfigurationPanel::DATE_FIELD_PREFIX . $field, $min, $max); } } if ($options->getFields()) { $filters = []; foreach ($options->getFields() as $field) { $filters[] = sprintf("%u", crc32($field->get_databox()->get_sbas_id() . '_' . $field->get_id())); } $this->sphinx->SetFilter('crc_struct_id', $filters); } if ($options->getBusinessFieldsOn()) { $crc_coll_business = []; foreach ($options->getBusinessFieldsOn() as $collection) { $crc_coll_business[] = sprintf("%u", crc32($collection->get_coll_id() . '_1')); $crc_coll_business[] = sprintf("%u", crc32($collection->get_coll_id() . '_0')); } $non_business = []; foreach ($options->getCollections() as $collection) { foreach ($options->getBusinessFieldsOn() as $BFcollection) { if ($collection->get_base_id() == $BFcollection->get_base_id()) { continue 2; } } $non_business[] = $collection; } foreach ($non_business as $collection) { $crc_coll_business[] = sprintf("%u", crc32($collection->get_coll_id() . '_0')); } $this->sphinx->SetFilter('crc_coll_business', $crc_coll_business); } elseif ($options->getFields()) { $this->sphinx->SetFilter('business', [0]); } /** * @todo : enhance : check status in a better way */ $status_opts = $options->getStatus(); foreach ($options->getDataboxes() as $databox) { foreach ($databox->get_statusbits() as $n => $status) { if (!array_key_exists($n, $status_opts)) { continue; } if (!array_key_exists($databox->get_sbas_id(), $status_opts[$n])) { continue; } $crc = sprintf("%u", crc32($databox->get_sbas_id() . '_' . $n)); $this->sphinx->SetFilter('status', [$crc], $status_opts[$n][$databox->get_sbas_id()] == '0'); } } if ($options->getRecordType()) { $this->sphinx->SetFilter('crc_type', [sprintf("%u", crc32($options->getRecordType()))]); } $order = ''; switch ($options->getSortOrder()) { case SearchEngineOptions::SORT_MODE_ASC: $order = 'ASC'; break; case SearchEngineOptions::SORT_MODE_DESC: default: $order = 'DESC'; break; } switch ($options->getSortBy()) { case SearchEngineOptions::SORT_RANDOM: $sort = '@random'; break; case SearchEngineOptions::SORT_RELEVANCE: default: $sort = '@relevance ' . $order . ', created_on ' . $order; break; case SearchEngineOptions::SORT_CREATED_ON: $sort = 'created_on ' . $order; break; } $this->sphinx->SetGroupBy('crc_sbas_record', SPH_GROUPBY_ATTR, $sort); return $this; }
private function buildACLsFilters(array $aclRules, SearchEngineOptions $options) { $filters = []; $collections = $options->getCollections(); $collectionsWoRules = []; $collectionsWoRules['terms']['base_id'] = []; foreach ($aclRules as $baseId => $flagsRules) { if (!array_key_exists($baseId, $collections)) { // no need to add a filter if the collection is not searched continue; } $ruleFilter = $baseFilter = []; // filter on base $baseFilter['term']['base_id'] = $baseId; $ruleFilter['bool']['must'][] = $baseFilter; // filter by flags foreach ($flagsRules as $flagName => $flagRule) { // only add filter if one of the status state is not allowed / allowed if ($flagRule === self::FLAG_ALLOW_BOTH) { continue; } $flagFilter = []; $flagField = sprintf('flags.%s', $flagName); $flagFilter['term'][$flagField] = $flagRule === self::FLAG_SET_ONLY ? true : false; $ruleFilter['bool']['must'][] = $flagFilter; } if (count($ruleFilter['bool']['must']) > 1) { // some rules found, add the filter $filters[] = $ruleFilter; } else { // no rules for this collection, add it to the 'simple' list $collectionsWoRules['terms']['base_id'][] = $baseId; } } if (count($collectionsWoRules['terms']['base_id']) > 0) { // collections w/o rules : add a simple list ? if (count($filters) > 0) { // no need to add a big 'should' filter only on collections $filters[] = $collectionsWoRules; } } if (count($filters) > 0) { return ['bool' => ['should' => $filters]]; } else { return []; } }