Skip to content

Commit

Permalink
Merge pull request #70 from designmynight/library-updates
Browse files Browse the repository at this point in the history
Library updates
  • Loading branch information
Will Taylor-Jackson authored Dec 4, 2018
2 parents 26e7a1f + 9925fe7 commit a2745fe
Show file tree
Hide file tree
Showing 3 changed files with 153 additions and 21 deletions.
4 changes: 2 additions & 2 deletions src/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ public function cursor($query, $bindings = [], $useReadPdo = false)

$scrollParams = array(
'scroll' => $scrollTimeout,
'size' => 500,
'size' => min(100, $limit),
'index' => $query['index'],
'body' => $query['body']
);
Expand All @@ -217,7 +217,7 @@ public function cursor($query, $bindings = [], $useReadPdo = false)
yield $result;
}

if ( $limit >= $numResults ){
if ( $limit > $numResults ){
foreach ($this->scroll($scrollId, $scrollTimeout, $limit - $numResults) as $result) {
yield $result;
}
Expand Down
108 changes: 108 additions & 0 deletions src/QueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,15 @@ public function whereNestedDoc($column, $query, $boolean = 'and'): self
return $this;
}

/**
* Add a prefix query
*
* @param string $column
* @param string $value
* @param string $boolean
* @param boolean $not
* @return self
*/
public function whereStartsWith($column, string $value, $boolean = 'and', $not = false): self
{
$type = 'Prefix';
Expand All @@ -187,6 +196,105 @@ public function whereStartsWith($column, string $value, $boolean = 'and', $not =
return $this;
}

/**
* Add a script query
*
* @param string $script
* @param array $options
* @param string $boolean
* @return self
*/
public function whereScript(string $script, array $options = [], $boolean = 'and'): self
{
$type = 'Script';

$this->wheres[] = compact('script', 'options', 'type', 'boolean');

return $this;
}

/**
* Add a "where weekday" statement to the query.
*
* @param string $column
* @param string $operator
* @param \DateTimeInterface|string $value
* @param string $boolean
* @return \Illuminate\Database\Query\Builder|static
*/
public function whereWeekday($column, $operator, $value = null, $boolean = 'and')
{
[$value, $operator] = $this->prepareValueAndOperator(
$value, $operator, func_num_args() === 2
);

if ($value instanceof DateTimeInterface) {
$value = $value->format('N');
}

return $this->addDateBasedWhere('Weekday', $column, $operator, $value, $boolean);
}

/**
* Add an "or where weekday" statement to the query.
*
* @param string $column
* @param string $operator
* @param \DateTimeInterface|string $value
* @return \Illuminate\Database\Query\Builder|static
*/
public function orWhereWeekday($column, $operator, $value = null)
{
[$value, $operator] = $this->prepareValueAndOperator(
$value, $operator, func_num_args() === 2
);

return $this->addDateBasedWhere('Weekday', $column, $operator, $value, 'or');
}

/**
* Add a date based (year, month, day, time) statement to the query.
*
* @param string $type
* @param string $column
* @param string $operator
* @param mixed $value
* @param string $boolean
* @return $this
*/
protected function addDateBasedWhere($type, $column, $operator, $value, $boolean = 'and')
{
switch ($type) {
case 'Year':
$dateType = 'year';
break;

case 'Month':
$dateType = 'monthOfYear';
break;

case 'Day':
$dateType = 'dayOfMonth';
break;

case 'Weekday':
$dateType = 'dayOfWeek';
break;
}

$type = 'Script';

$operator = $operator == '=' ? '==' : $operator;

$script = "doc.{$column}.date.{$dateType} {$operator} params.value";

$options['params'] = ['value' => (int) $value];

$this->wheres[] = compact('script', 'options', 'type', 'boolean');

return $this;
}

/**
* Add another query builder as a nested where to the query builder.
*
Expand Down
62 changes: 43 additions & 19 deletions src/QueryGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,7 @@ protected function compileWhereFunctionScore(Builder $builder, array $where): ar
}

/**
* Compile a where nested geo distance clause
* Compile a search clause
*
* @param Builder $builder
* @param array $where
Expand All @@ -482,7 +482,7 @@ protected function compileWhereSearch(Builder $builder, array $where): array
{
$fields = '_all';

if (!empty($where['options']['fields'])) {
if (! empty($where['options']['fields'])) {
$fields = $where['options']['fields'];
}

Expand All @@ -499,41 +499,65 @@ protected function compileWhereSearch(Builder $builder, array $where): array
if (is_array($fields) && count($fields) > 1) {
$type = isset($where['options']['matchType']) ? $where['options']['matchType'] : 'most_fields';

$query = array(
'multi_match' => array(
$query = [
'multi_match' => [
'query' => $where['value'],
'type' => $type,
'fields' => $fields,
),
);
],
];
} else {
$fields = is_array($fields) ? reset($fields) : $fields;
$field = is_array($fields) ? reset($fields) : $fields;

$query = array(
'match' => array(
$fields => $where['value'],
),
);
$query = [
'match' => [
$field => [
'query' => $where['value'],
]
],
];
}

if (!empty($where['options']['constant_score'])) {
if (! empty($where['options']['fuzziness'])) {
$matchType = array_keys($query)[0];

if ($matchType === 'multi_match') {
$query[$matchType]['fuzziness'] = $where['options']['fuzziness'];
}
else {
$query[$matchType][$field]['fuzziness'] = $where['options']['fuzziness'];
}
}

if (! empty($where['options']['constant_score'])) {
$query = [
'constant_score' => [
'query' => $query,
],
];
}

if (!empty($where['options']['fuzziness'])) {
$firstKey = array_keys($query)[0];
$query[$firstKey]['fuzziness'] = $where['options']['fuzziness'];
}

return $query;
}

/**
* Compile a where nested geo distance clause
* Compile a script clause
*
* @param Builder $builder
* @param array $where
* @return array
*/
protected function compileWhereScript(Builder $builder, array $where): array
{
return [
'script' => [
'script' => array_merge($where['options'], ['source' => $where['script']]),
],
];
}

/**
* Compile a geo distance clause
*
* @param Builder $builder
* @param array $where
Expand Down

0 comments on commit a2745fe

Please sign in to comment.