Source of file SearchQuery.php
Size: 9,825 Bytes - Last Modified: 2021-12-23T10:31:32+00:00
/var/www/docs.ssmods.com/process/src/src/Search/Queries/SearchQuery.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393 | <?php namespace SilverStripe\FullTextSearch\Search\Queries; use SilverStripe\Dev\Deprecation; use SilverStripe\FullTextSearch\Search\Adapters\SearchAdapterInterface; use SilverStripe\FullTextSearch\Search\Criteria\SearchCriteria; use SilverStripe\FullTextSearch\Search\Criteria\SearchCriteriaInterface; use SilverStripe\View\ViewableData; use stdClass; /** * Represents a search query * * API very much still in flux. */ class SearchQuery extends ViewableData { public static $missing = null; public static $present = null; public static $default_page_size = 10; /** These are public, but only for index & variant access - API users should not manually access these */ public $search = []; public $classes = []; public $require = []; public $exclude = []; /** * @var SearchCriteriaInterface[] */ public $criteria = []; protected $start = 0; protected $limit = -1; /** * @var SearchAdapterInterface */ protected $adapter = null; /** These are the API functions */ /** * SearchQuery constructor. * @throws \Psr\Container\NotFoundExceptionInterface */ public function __construct() { if (self::$missing === null) { self::$missing = new stdClass(); } if (self::$present === null) { self::$present = new stdClass(); } } /** * @param SearchAdapterInterface $adapter * @return SearchQuery */ public function setHandler(SearchAdapterInterface $adapter) { $this->adapter = $adapter; return $this; } /** * @param string $text Search terms. Exact format (grouping, boolean expressions, etc.) depends on * the search implementation. * @param array $fields Limits the search to specific fields (using composite field names) * @param array $boost Map of composite field names to float values. The higher the value, * the more important the field gets for relevancy. * @return $this */ public function addSearchTerm($text, $fields = null, $boost = []) { $this->search[] = [ 'text' => $text, 'fields' => $fields ? (array) $fields : null, 'boost' => $boost, 'fuzzy' => false ]; return $this; } /** * Similar to {@link addSearchTerm()}, but uses stemming and other similarity algorithms * to find the searched terms. For example, a term "fishing" would also likely find results * containing "fish" or "fisher". Depends on search implementation. * * @param string $text See {@link addSearchTerm()} * @param array $fields See {@link addSearchTerm()} * @param array $boost See {@link addSearchTerm()} * @return $this */ public function addFuzzySearchTerm($text, $fields = null, $boost = []) { $this->search[] = [ 'text' => $text, 'fields' => $fields ? (array) $fields : null, 'boost' => $boost, 'fuzzy' => true ]; return $this; } /** * @return array */ public function getSearchTerms() { return $this->search; } /** * @param string $class * @param bool $includeSubclasses * @return $this */ public function addClassFilter($class, $includeSubclasses = true) { $this->classes[] = [ 'class' => $class, 'includeSubclasses' => $includeSubclasses ]; return $this; } /** * @return array */ public function getClassFilters() { return $this->classes; } /** * Similar to {@link addSearchTerm()}, but typically used to further narrow down * based on other facets which don't influence the field relevancy. * * @param string $field Composite name of the field * @param mixed $values Scalar value, array of values, or an instance of SearchQuery_Range * @return $this */ public function addFilter($field, $values) { $requires = isset($this->require[$field]) ? $this->require[$field] : []; $values = is_array($values) ? $values : [$values]; $this->require[$field] = array_merge($requires, $values); return $this; } /** * @return array */ public function getFilters() { return $this->require; } /** * Excludes results which match these criteria, inverse of {@link addFilter()}. * * @param string $field * @param mixed $values * @return $this */ public function addExclude($field, $values) { $excludes = isset($this->exclude[$field]) ? $this->exclude[$field] : []; $values = is_array($values) ? $values : [$values]; $this->exclude[$field] = array_merge($excludes, $values); return $this; } /** * @return array */ public function getExcludes() { return $this->exclude; } /** * You can pass through a string value, Criteria object, or Criterion object for $target. * * String value might be "SiteTree_Title" or whatever field in your index that you're trying to target. * * If you require complex filtering then you can build your Criteria object first with multiple layers/levels of * Criteria, and then pass it in here when you're ready. * * If you have your own Criterion object that you've created that you want to use, you can also pass that in here. * * @param string|SearchCriteriaInterface $target * @param mixed $value * @param string|null $comparison * @param AbstractSearchQueryWriter $searchQueryWriter * @return SearchCriteriaInterface */ public function filterBy( $target, $value = null, $comparison = null, AbstractSearchQueryWriter $searchQueryWriter = null ) { if (!$target instanceof SearchCriteriaInterface) { $target = new SearchCriteria($target, $value, $comparison, $searchQueryWriter); } $this->addCriteria($target); return $target; } public function setStart($start) { $this->start = $start; return $this; } /** * @return int */ public function getStart() { return $this->start; } public function setLimit($limit) { $this->limit = $limit; return $this; } /** * @return int */ public function getLimit() { return $this->limit; } public function setPageSize($page) { $this->setStart($page * self::$default_page_size); $this->setLimit(self::$default_page_size); return $this; } /** * @return int */ public function getPageSize() { return (int) ($this->getLimit() / $this->getStart()); } /** * @return bool */ public function isFiltered() { return $this->search || $this->classes || $this->require || $this->exclude; } /** * @return SearchAdapterInterface */ public function getAdapter() { return $this->adapter; } public function __toString() { return "Search Query\n"; } /** * @codeCoverageIgnore * @deprecated */ public function search($text, $fields = null, $boost = []) { Deprecation::notice('4.0', 'Use addSearchTerm() instead'); return $this->addSearchTerm($text, $fields, $boost); } /** * @codeCoverageIgnore * @deprecated */ public function fuzzysearch($text, $fields = null, $boost = []) { Deprecation::notice('4.0', 'Use addFuzzySearchTerm() instead'); return $this->addFuzzySearchTerm($text, $fields, $boost); } /** * @codeCoverageIgnore * @deprecated */ public function inClass($class, $includeSubclasses = true) { Deprecation::notice('4.0', 'Use addClassFilter() instead'); return $this->addClassFilter($class, $includeSubclasses); } /** * @codeCoverageIgnore * @deprecated */ public function filter($field, $values) { Deprecation::notice('4.0', 'Use addFilter() instead'); return $this->addFilter($field, $values); } /** * @codeCoverageIgnore * @deprecated */ public function exclude($field, $values) { Deprecation::notice('4.0', 'Use addExclude() instead'); return $this->addExclude($field, $values); } /** * @codeCoverageIgnore * @deprecated */ public function start($start) { Deprecation::notice('4.0', 'Use setStart() instead'); return $this->setStart($start); } /** * @codeCoverageIgnore * @deprecated */ public function limit($limit) { Deprecation::notice('4.0', 'Use setLimit() instead'); return $this->setLimit($limit); } /** * @codeCoverageIgnore * @deprecated */ public function page($page) { Deprecation::notice('4.0', 'Use setPageSize() instead'); return $this->setPageSize($page); } /** * @return SearchCriteriaInterface[] */ public function getCriteria() { return $this->criteria; } /** * @param SearchCriteriaInterface[] $criteria * @return SearchQuery */ public function setCriteria($criteria) { $this->criteria = $criteria; return $this; } /** * @param SearchCriteriaInterface $criteria * @return SearchQuery */ public function addCriteria($criteria) { $this->criteria[] = $criteria; return $this; } } |