| 1: | <?php |
| 2: | |
| 3: | namespace Baril\Sqlout; |
| 4: | |
| 5: | use Closure; |
| 6: | use Laravel\Scout\Builder as ScoutBuilder; |
| 7: | |
| 8: | class Builder extends ScoutBuilder |
| 9: | { |
| 10: | public const NATURAL_LANGUAGE = 'in natural language mode'; |
| 11: | public const QUERY_EXPANSION = 'in natural language mode with query expansion'; |
| 12: | public const BOOLEAN = 'in boolean mode'; |
| 13: | |
| 14: | /** |
| 15: | * The search mode (one of the consts above). |
| 16: | * |
| 17: | * @var string |
| 18: | */ |
| 19: | public $mode; |
| 20: | |
| 21: | /** |
| 22: | * The array of scopes that will be applied to the model query. |
| 23: | * |
| 24: | * @var array |
| 25: | */ |
| 26: | public $scopes = []; |
| 27: | |
| 28: | public function __call($method, $parameters) |
| 29: | { |
| 30: | if (static::hasMacro($method)) { |
| 31: | return parent::__call($method, $parameters); |
| 32: | } |
| 33: | |
| 34: | $this->scopes[] = [$method, $parameters]; |
| 35: | return $this; |
| 36: | } |
| 37: | |
| 38: | /** |
| 39: | * @deprecated Will be removed in next major version, use Scout's $builder->query($callback) instead. |
| 40: | */ |
| 41: | public function scope(Closure $callback) |
| 42: | { |
| 43: | $this->scopes[] = $callback; |
| 44: | return $this; |
| 45: | } |
| 46: | |
| 47: | /** |
| 48: | * Order the query by score. |
| 49: | * |
| 50: | * @param string $direction |
| 51: | * @return $this |
| 52: | */ |
| 53: | public function orderByScore($direction = 'desc') |
| 54: | { |
| 55: | return $this->orderBy('_score', $direction); |
| 56: | } |
| 57: | |
| 58: | /** |
| 59: | * Restrict the search to the provided field(s). |
| 60: | * |
| 61: | * @param string|array|\Illuminate\Contracts\Support\Arrayable $fields |
| 62: | * @return $this |
| 63: | */ |
| 64: | public function only($fields) |
| 65: | { |
| 66: | return parent::where('field', $fields); |
| 67: | } |
| 68: | |
| 69: | /** |
| 70: | * Switches to the provided mode. |
| 71: | * |
| 72: | * @param string $mode |
| 73: | * @return $this |
| 74: | */ |
| 75: | public function mode($mode) |
| 76: | { |
| 77: | $this->mode = trim(strtolower($mode)); |
| 78: | return $this; |
| 79: | } |
| 80: | |
| 81: | /** |
| 82: | * Switches to natural language mode. |
| 83: | * |
| 84: | * @param string $mode |
| 85: | * @return $this |
| 86: | */ |
| 87: | public function inNaturalLanguageMode() |
| 88: | { |
| 89: | return $this->mode(static::NATURAL_LANGUAGE); |
| 90: | } |
| 91: | |
| 92: | /** |
| 93: | * Switches to natural language mode with query expansion. |
| 94: | * |
| 95: | * @param string $mode |
| 96: | * @return $this |
| 97: | */ |
| 98: | public function withQueryExpansion() |
| 99: | { |
| 100: | return $this->mode(static::QUERY_EXPANSION); |
| 101: | } |
| 102: | |
| 103: | /** |
| 104: | * Switches to boolean mode. |
| 105: | * |
| 106: | * @param string $mode |
| 107: | * @return $this |
| 108: | */ |
| 109: | public function inBooleanMode() |
| 110: | { |
| 111: | return $this->mode(static::BOOLEAN); |
| 112: | } |
| 113: | |
| 114: | /** |
| 115: | * Returns the total number of hits |
| 116: | * |
| 117: | * @return int |
| 118: | */ |
| 119: | public function count() |
| 120: | { |
| 121: | return $this->engine()->getTotalCount( |
| 122: | $this->engine()->search($this) |
| 123: | ); |
| 124: | } |
| 125: | } |
| 126: |