类 Query
属性摘要
-
$_cache protected
Cake\Datasource\QueryCacher|null
如果此查询启用了缓存,则为查询缓存器实例。
-
$_dirty protected
bool
用于跟踪查询是否已被修改的内部状态。
-
$_elasticQuery protected
Elastica\Query
在构建后要执行的 Elastica 查询对象。
-
$_formatters protected
arrayClosure>
格式化程序类或回调函数的列表,这些类或回调函数将在获取结果后进行后处理
-
$_mapReduce protected
array
应在查询结果上应用的映射减少例程列表
-
$_options protected
array
保存使用 applyOptions 传递的任何自定义选项,这些选项可能无法由此类中的任何方法处理。
-
$_queryParts protected
array
将被传递到 elastica 查询的各种查询构建器部分。
-
$_repository protected
Cake\ElasticSearch\Index
此查询绑定的存储库对象的实例。
-
$_results protected
iterable|null
结果集。
-
$_searchOptions protected
array
Elastica\Index::search() 的其他选项
方法摘要
-
__construct() public
查询构造函数
-
_buildBoolQuery() protected
用于将条件解析为布尔查询并将它们存储在 _queryParts 变量中的辅助函数。
-
_execute() protected
执行查询。
-
aggregate() public
将聚合添加到弹性查询对象
-
aliasField() public
返回表示单个别名字段的键 => 值数组,该数组可以直接传递给 select() 方法。键将包含别名,值将包含实际字段名称。
-
aliasFields() public
对提供的列表中的每个字段运行
aliasField()
并将结果放在单个数组下返回。 -
all() public
获取此查询的结果。
-
andWhere() public
使用 AND 运算符将任何先前定义的条件集连接到提供的列表。此函数接受与方法
where
相同格式的条件列表,因此您可以使用数组、表达式对象回调函数或字符串。 -
applyOptions() public
使用数组填充或添加当前查询子句的部分。这对于一次传递所有查询子句非常方便。选项数组接受
-
cache() public
为此查询启用结果缓存。
-
clause() public
返回存储在指定子句中的任何数据。这对于修改查询的任何内部部分很有用,它在编译期间用于在执行查询之前相应地转换查询。可以检索的有效子句是:fields、filter、postFilter、query、order、limit 和 offset。
-
collapse() public
将折叠添加到弹性查询对象
-
compileQuery() public
编译 Elasticsearch 查询。
-
count() public
返回查询的命中总数
-
decorateResults() protected
使用 MapReduce 例程和格式化程序修饰结果迭代器
-
decoratorClass() protected
返回用于修饰结果的类的名称
-
find() public
对现有查询对象应用自定义查找。
-
first() public
返回执行此查询的第一个结果,如果查询之前未执行,则出于性能原因,它将限制子句设置为 1。
-
firstOrFail() public
从执行的查询中获取第一个结果或引发异常。
-
formatResults() public
注册一个新的格式化器回调函数,该函数将在尝试从数据库中获取结果时执行。
-
getIterator() public
执行此查询并返回结果迭代器。此函数需要实现 IteratorAggregate 接口,并允许在不手动调用 execute() 的情况下迭代查询,从而使其看起来像结果集而不是查询本身。
-
getMapReducers() public
返回先前注册的映射减少例程列表。
-
getOptions() public
返回一个包含应用于此查询的自定义选项的数组,这些选项尚未由此类中的其他方法处理。
-
getRepository() public
返回此查询将使用的默认存储库对象,即将出现在 from 子句中的表。
-
getResultFormatters() public
返回先前注册的格式例程列表。
-
highlight() public
设置查询的突出显示选项。
-
limit() public
设置此查询要返回的最大结果数。这会为 Elasticsearch 查询设置
size
选项。 -
mapReduce() public
注册一个新的 MapReduce 例程,该例程将在数据库结果之上执行
-
offset() public
设置应从原始结果集中跳过的记录数。这通常用于对大型结果进行分页。接受一个整数。
-
order() public deprecated
设置结果集的排序选项。
-
orderBy() public
设置结果集的排序选项。
-
page() public
设置您想要的页面结果。
-
postFilter() public
设置在 post_filter 对象中使用的查询。使用此方法添加的过滤器将堆叠在 BoolQuery 上。
-
queryMust() public
修改查询部分,考虑分数。使用此方法添加的查询将堆叠在 bool 查询上,并应用于最终 BoolQuery 的
must
部分。 -
queryShould() public
修改查询部分,考虑分数。使用此方法添加的查询将堆叠在 bool 查询上,并应用于最终 BoolQuery 的
should
部分。 -
repository() public deprecated
设置此查询将使用的默认存储库对象。
-
searchOptions() public
设置或获取搜索选项
-
select() 公共
添加要从 _source 中选择的字段。
-
setFullQuery() 公共
用于设置或覆盖查询的方法。
-
setRepository() 公共
设置此查询将使用的默认存储库对象。
-
toArray() 公共
在执行查询后,返回结果的数组表示形式。
-
where() 公共
设置要在查询对象中使用的过滤器。使用此方法添加的查询将堆叠在 bool 查询上,并应用于最终 BoolQuery 的过滤器部分。
-
withMinScore() 公共
设置结果集中的结果应具有的最小分数,以便在结果集中返回。
方法详细信息
__construct() ¶ 公共
__construct(Cake\ElasticSearch\Index $repository)
查询构造函数
参数
-
Cake\ElasticSearch\Index
$repository 文档的类型。
_buildBoolQuery() ¶ 受保护
_buildBoolQuery(string $partType, Elastica\Query\AbstractQuery|callable|array $conditions, bool $overwrite, string $type = 'addMust'): $this
用于将条件解析为布尔查询并将它们存储在 _queryParts 变量中的辅助函数。
参数
-
string
$partType 将在其中存储 bool 查询的部分的名称
-
Elastica\Query\AbstractQuery|callable|array
$conditions 条件列表。
-
bool
$overwrite 是否替换之前的查询。
-
string
$type 可选 用于将条件追加到查询的方法
返回
$this
aggregate() ¶ 公共
aggregate(Elastica\Aggregation\AbstractAggregation|array $aggregation): $this
将聚合添加到弹性查询对象
参数
-
Elastica\Aggregation\AbstractAggregation|array
$aggregation 一个或多个方面
返回
$this
aliasField() ¶ 公共
aliasField(string $field, string|null $alias = null): array<string, string>
返回表示单个别名字段的键 => 值数组,该数组可以直接传递给 select() 方法。键将包含别名,值将包含实际字段名称。
如果字段已经被别名化,则不会被改变。如果没有传入 $alias,则将使用此查询的默认表。
参数
-
string
$field -
string|null
$alias 可选
返回
array<string, string>
aliasFields() ¶ 公共
aliasFields(array $fields, string|null $defaultAlias = null): array<string, string>
对提供的列表中的每个字段运行 aliasField()
并将结果放在单个数组下返回。
参数
-
array
$fields -
string|null
$defaultAlias 可选
返回
array<string, string>
all() ¶ 公共
all(): Cake\Datasource\ResultSetInterface
获取此查询的结果。
将返回通过 setResult() 设置的结果集,或执行此查询并返回 ResultSetDecorator 对象,该对象已准备好用于结果流。
ResultSetDecorator 是一个可遍历对象,它实现了 Cake\Collection\Collection 中找到的方法。
返回
Cake\Datasource\ResultSetInterface
andWhere() ¶ 公共
andWhere(Elastica\Query\AbstractQuery|callable|array|null $conditions, array $types = []): $this
使用 AND 运算符将任何先前定义的条件集连接到提供的列表。此函数接受与方法 where
相同格式的条件列表,因此您可以使用数组、表达式对象回调函数或字符串。
重要的是要注意,当调用此函数时,为该查询定义的任何先前条件集将被视为 AND 运算符的单个参数。此函数不仅会对最近定义的条件进行运算,还会对所有条件作为一个整体进行运算。
当使用数组定义条件时,从每个数组条目创建约束将使用与 where()
函数相同的逻辑。这意味着每个数组条目将使用 AND 运算符与其他条目连接,除非您使用其他运算符将条件嵌套在数组中。
示例
$query->where(['title' => 'Hello World')->andWhere(['author_id' => 1]);
将生成
WHERE title = 'Hello World' AND author_id = 1
$query
->where(['OR' => ['published' => false, 'published is NULL']])
->andWhere(['author_id' => 1, 'comments_count >' => 10])
生成
WHERE (published = 0 OR published IS NULL) AND author_id = 1 AND comments_count > 10
$query
->where(['title' => 'Foo'])
->andWhere(function ($exp, $query) {
return $exp
->or(['author_id' => 1])
->add(['author_id' => 2]);
});
生成以下条件
WHERE (title = 'Foo') AND (author_id = 1 OR author_id = 2)
参数
-
Elastica\Query\AbstractQuery|callable|array|null
$conditions 条件列表。
-
array
$types 可选 未使用,需要符合 QueryInterface。
返回
$this
另请参阅
\Cake\ElasticSearch\QueryBuilder
applyOptions() ¶ 公共
applyOptions(array<string, mixed> $options): $this
使用数组填充或添加当前查询子句的部分。这对于一次传递所有查询子句非常方便。选项数组接受
- fields: 映射到 select 方法
- conditions: 映射到 where 方法
- order: 映射到 order 方法
- limit: 映射到 limit 方法
- offset: 映射到 offset 方法
- page: 映射到 page 方法
示例
$query->applyOptions([
'fields' => ['id', 'name'],
'conditions' => [
'created >=' => '2013-01-01'
],
'limit' => 10
]);
等效于
$query
->select(['id', 'name'])
->where(['created >=' => '2013-01-01'])
->limit(10)
参数
-
array<string, mixed>
$options 要应用新部分的查询子句列表。
返回
$this
cache() ¶ 公共
cache(Closure|string|false $key, Psr\SimpleCache\CacheInterface|string $config = 'default'): $this
为此查询启用结果缓存。
如果查询已启用缓存,则在执行时将执行以下操作
- 检查 $key 的缓存。如果存在结果,则不会执行 SQL。而是返回缓存的结果。
- 当缓存的数据陈旧/丢失时,将随着查询的执行将结果集缓存起来。
用法
// Simple string key + config
$query->cache('my_key', 'db_results');
// Function to generate key.
$query->cache(function ($q) {
$key = serialize($q->clause('select'));
$key .= serialize($q->clause('where'));
return md5($key);
});
// Using a pre-built cache engine.
$query->cache('my_key', $engine);
// Disable caching
$query->cache(false);
参数
-
Closure|string|false
$key 缓存键或生成缓存键的函数。当使用函数时,将提供此查询实例作为参数。
-
Psr\SimpleCache\CacheInterface|string
$config 可选 要使用的缓存配置的名称或缓存引擎实例。
返回
$this
clause() ¶ 公共
clause(string $name): mixed
返回存储在指定子句中的任何数据。这对于修改查询的任何内部部分很有用,它在编译期间用于在执行查询之前相应地转换查询。可以检索的有效子句是:fields、filter、postFilter、query、order、limit 和 offset。
每个部分的返回值可能会有所不同。某些子句使用 QueryExpression 来内部存储其状态,某些子句使用数组,而其他子句可能使用布尔值或整数。以下是每个子句的返回值类型的摘要。
- fields: 数组,当未设置任何字段时将返回空数组
- query: 要在查询(带有评分)部分中使用的最终 BoolQuery。
- filter: 要在最终 BoolQuery 过滤器对象中使用的查询,在未设置时返回 null
- postFilter: 要在 post_filter 对象中使用的查询,在未设置时返回 null
- order: OrderByExpression,在未设置时返回 null
- limit: 整数,在未设置时返回 null
- offset: 整数,在未设置时返回 null
参数
-
string
$name 要返回的子句的名称
返回
混合
collapse() ¶ 公共
collapse(Elastica\Collapse|string $collapse): $this
将折叠添加到弹性查询对象
参数
-
Elastica\Collapse|string
$collapse 折叠字段或弹性折叠对象
返回
$this
decorateResults() ¶ 受保护
decorateResults(iterable $result): Cake\Datasource\ResultSetInterface
使用 MapReduce 例程和格式化程序修饰结果迭代器
参数
-
iterable
$result 原始结果
返回
Cake\Datasource\ResultSetInterface
decoratorClass() ¶ 受保护
decoratorClass(): class-stringCake\Datasource\ResultSetInterface>
返回用于修饰结果的类的名称
返回
class-stringCake\Datasource\ResultSetInterface>
find() ¶ 公共
find(string $finder, mixed ...$args): static<TSubject>
对现有查询对象应用自定义查找。
允许自定义查找方法组合并应用于彼此。
$repository->find('all')->find('recent');
以上是将多个查找方法堆叠到单个查询中的示例。
参数
-
string
$finder 要使用的查找方法。
-
mixed
...$args 查找方法的选项。
返回
static<TSubject>
first() ¶ 公共
first(): mixed
返回执行此查询的第一个结果,如果查询之前未执行,则出于性能原因,它将限制子句设置为 1。
示例
$singleUser = $query->select(['id', 'username'])->first();
返回
混合
firstOrFail() ¶ 公共
firstOrFail(): mixed
从执行的查询中获取第一个结果或引发异常。
返回
混合
抛出
Cake\Datasource\Exception\RecordNotFoundException
当没有第一条记录时。
formatResults() ¶ 公共
formatResults(Closure|null $formatter = null, int|bool $mode = self::APPEND): $this
注册一个新的格式化器回调函数,该函数将在尝试从数据库中获取结果时执行。
如果第二个参数设置为 true,则将擦除先前的格式化程序并将其替换为传入的第一个参数。
回调需要返回一个迭代器对象,该对象将用作此查询结果的返回值。格式化函数在执行此查询的所有 MapReduce
例程后应用。
格式化回调将接收两个参数,第一个参数是一个实现 \Cake\Collection\CollectionInterface
的对象,可以遍历和修改该对象。第二个参数是正在应用格式化回调的查询实例。
示例
返回由 id 索引的表中的所有结果
$query->select(['id', 'name'])->formatResults(function ($results) {
return $results->indexBy('id');
});
向 ResultSet 添加新列
$query->select(['name', 'birth_date'])->formatResults(function ($results) {
return $results->map(function ($row) {
$row['age'] = $row['birth_date']->diff(new DateTime)->y;
return $row;
});
});
参数
-
Closure|null
$formatter 可选 格式化函数
-
int|bool
$mode 可选 是否覆盖、追加或预置格式化程序。
返回
$this
抛出
InvalidArgumentException
getIterator() ¶ public
getIterator(): Traversable
执行此查询并返回结果迭代器。此函数需要实现 IteratorAggregate 接口,并允许在不手动调用 execute() 的情况下迭代查询,从而使其看起来像结果集而不是查询本身。
返回
Traversable
getOptions() ¶ public
getOptions(): array
返回一个包含应用于此查询的自定义选项的数组,这些选项尚未由此类中的其他方法处理。
示例
$query->applyOptions(['doABarrelRoll' => true, 'fields' => ['id', 'name']);
$query->getOptions(); // Returns ['doABarrelRoll' => true]
返回
array
另请参阅
applyOptions()
getRepository() ¶ public
getRepository(): Cake\ElasticSearch\Index
返回此查询将使用的默认存储库对象,即将出现在 from 子句中的表。
返回
Cake\ElasticSearch\Index
highlight() ¶ public
highlight(array $highlight): $this
设置查询的突出显示选项。
参数
-
array
$highlight 要使用的突出显示选项。
返回
$this
limit() ¶ public
limit(int|null $limit): $this
设置此查询要返回的最大结果数。这会为 Elasticsearch 查询设置 size
选项。
示例
$query->limit(10) // generates LIMIT 10
$query->limit($query->newExpr()->add(['1 + 1'])); // LIMIT (1 + 1)
参数
-
int|null
$limit 要返回的文档数量。
返回
$this
mapReduce() ¶ public
mapReduce(Closure|null $mapper = null, Closure|null $reducer = null, bool $overwrite = false): $this
注册一个新的 MapReduce 例程,该例程将在数据库结果之上执行
MapReduce 路由只会在查询执行并尝试获取第一个结果时运行。
如果第三个参数设置为 true,它将擦除之前的 map reducers 并用传递的参数替换它。
参数
-
Closure|null
$mapper 可选 映射器函数
-
Closure|null
$reducer 可选 减少函数
-
bool
$overwrite 可选 设置为 true 以覆盖现有的 map + reduce 函数。
返回
$this
另请参阅
offset() ¶ public
offset(int|null $offset): $this
设置应从原始结果集中跳过的记录数。这通常用于对大型结果进行分页。接受一个整数。
在某些数据库中,此操作可能不受支持,或者需要转换查询才能限制结果集的大小。
示例
$query->offset(10) // generates OFFSET 10
$query->offset($query->newExpr()->add(['1 + 1'])); // OFFSET (1 + 1)
参数
-
int|null
$offset 要跳过的记录数
返回
$this
order() ¶ public
order(Closure|array|string $fields, bool $overwrite = false): $this
设置结果集的排序选项。
接受的 $order 参数格式为
- [['name' => ['order'=> 'asc', ...]], ['price' => ['order'=> 'asc', ...]]]
- ['name' => 'asc', 'price' => 'desc']
- 'field1' (默认为 order => 'desc')
参数
-
Closure|array|string
$fields 要使用的排序顺序。
-
bool
$overwrite 可选 是否替换之前的排序。
返回
$this
orderBy() ¶ public
orderBy(Closure|array|string $fields, bool $overwrite = false): $this
设置结果集的排序选项。
接受的 $order 参数格式为
- [['name' => ['order'=> 'asc', ...]], ['price' => ['order'=> 'asc', ...]]]
- ['name' => 'asc', 'price' => 'desc']
- 'field1' (默认为 order => 'desc')
参数
-
Closure|array|string
$fields 要使用的排序顺序。
-
bool
$overwrite 可选 是否替换之前的排序。
返回
$this
page() ¶ public
page(int $num, int|null $limit = null): $this
设置您想要的页面结果。
此方法提供了一个更易于使用的接口来设置要作为结果的记录集中的限制 + 偏移量。如果为空,则限制将默认为现有的限制子句,如果限制子句也为空,则将使用 25
。
页面应该从 1 开始。
参数
-
int
$num 您想要的页码。
-
int|null
$limit 可选 您想要在页面中显示的行数。如果为 null,则将使用当前限制子句。
返回
$this
postFilter() ¶ public
postFilter(Elastica\Query\AbstractQuery|callable|array $conditions, bool $overwrite = false): $this
设置在 post_filter 对象中使用的查询。使用此方法添加的过滤器将堆叠在 BoolQuery 上。
此方法可以像使用 where()
方法一样使用。请参阅其文档以了解更多详细信息。
参数
-
Elastica\Query\AbstractQuery|callable|array
$conditions 条件列表。
-
bool
$overwrite 可选 是否替换之前的过滤器。
返回
$this
另请参阅
queryMust() ¶ public
queryMust(Elastica\Query\AbstractQuery|callable|array $conditions, bool $overwrite = false): $this
修改查询部分,考虑分数。使用此方法添加的查询将堆叠在 bool 查询上,并应用于最终 BoolQuery 的 must
部分。
此方法可以像使用 where()
方法一样使用。请参阅其文档以了解更多详细信息。
参数
-
Elastica\Query\AbstractQuery|callable|array
$conditions 条件列表
-
bool
$overwrite 可选 是否替换之前的查询。
返回
$this
queryShould() ¶ public
queryShould(Elastica\Query\AbstractQuery|callable|array $conditions, bool $overwrite = false): $this
修改查询部分,考虑分数。使用此方法添加的查询将堆叠在 bool 查询上,并应用于最终 BoolQuery 的 should
部分。
此方法可以像使用 where()
方法一样使用。请参阅其文档以了解更多详细信息。
参数
-
Elastica\Query\AbstractQuery|callable|array
$conditions 条件列表
-
bool
$overwrite 可选 是否替换之前的查询。
返回
$this
repository() ¶ public
repository(Cake\Datasource\RepositoryInterface $repository): $this
设置此查询将使用的默认存储库对象。
参数
-
Cake\Datasource\RepositoryInterface
$repository 要使用的默认存储库对象。
返回
$this
searchOptions() ¶ public
searchOptions(array|null $options = null): $this|array
设置或获取搜索选项
参数
-
array|null
$options 可选 其他搜索选项的数组
返回
$this|array
select() ¶ public
select(Closure|array|string|float|int $fields, bool $overwrite = false): $this
添加要从 _source 中选择的字段。
多次调用此函数将向要从 _source 中选择的字段列表追加更多字段。
如果在第二个参数中传递 true
,则任何之前的选择都将被第一个参数中传递的列表覆盖。
参数
-
Closure|array|string|float|int
$fields 要从 _source 中选择的字段列表。
-
bool
$overwrite 可选 是否替换之前的选择。
返回
$this
setFullQuery() ¶ public
setFullQuery(Elastica\Query\AbstractQuery $query): $this
用于设置或覆盖查询的方法。
参数
-
Elastica\Query\AbstractQuery
$query 设置查询
返回
$this
setRepository() ¶ public
setRepository(Cake\Datasource\RepositoryInterface $repository): $this
设置此查询将使用的默认存储库对象。
参数
-
Cake\Datasource\RepositoryInterface
$repository 要使用的默认存储库对象。
返回
$this
where() ¶ public
where(Closure|array|string|null $conditions = null, array<string, string> $types = [], bool $overwrite = false): $this
设置要在查询对象中使用的过滤器。使用此方法添加的查询将堆叠在 bool 查询上,并应用于最终 BoolQuery 的过滤器部分。
使用此方法添加的过滤器对文档的最终得分没有影响,不匹配指定过滤器的文档将被排除在外。
您可以通过多种方式使用此方法。最简单的方法是传递一个简单的条件数组
{{{ // 生成一个 {"term": {"name": "jose"}} json 查询 $query->where(['name' => 'jose']); }}}
您可以在数组中包含任意多的条件,运算符也可以在数组的字段侧使用
{{{ $query->where(['name' => 'jose', 'age >' => 30, 'interests in' => ['php', 'cake']); }}}
您可以在 Cake\ElasticSearch\QueryBuilder::parse()
方法文档中阅读有关可用运算符及其如何转换为 Elasticsearch 查询的信息。
此外,可以使用闭包作为第一个参数。闭包将接收一个 QueryBuilder 实例,您可以使用它来创建任意查询组合
{{{ $query->where(function ($builder) { return $builder->and($builder->between('age', 10, 20), $builder->missing('name')); }); }}}
最后,您可以将任何已经构建的查询作为第一个参数传递
{{{ $query->where(new \Elastica\Filter\Term('name.first', 'jose')); }}{
参数
-
Closure|array|string|null
$conditions 可选 条件列表。
-
array<string, string>
$types 可选 未使用,需要符合 QueryInterface。
-
bool
$overwrite 可选 是否替换之前的查询。
返回
$this
另请参阅
withMinScore() ¶ public
withMinScore(float $score): $this
设置结果集中的结果应具有的最小分数,以便在结果集中返回。
参数
-
float
$score 要观察的最低得分
返回
$this