Php Plastic/Elasticsearch-搜索具有空值的条目
我使用的是Laravel5.2,它有一个名为job_-ads的表。job_-ads表有一个type列,有时为null。我想搜索所有类型为null的招聘广告,但如果我尝试Php Plastic/Elasticsearch-搜索具有空值的条目,php,laravel,
elasticsearch,laravel-5.2,Php,Laravel,
elasticsearch,Laravel 5.2,我使用的是Laravel5.2,它有一个名为job_-ads的表。job_-ads表有一个type列,有时为null。我想搜索所有类型为null的招聘广告,但如果我尝试 App\JobAd::search()->term('type', null)->sortBy("id", "ASC")->get()->totalHits(); 通过php artisan tinker,我得到 Elasticsearch\Common\Exceptions\BadRequest400
App\JobAd::search()->term('type', null)->sortBy("id", "ASC")->get()->totalHits();
通过php artisan tinker,我得到
Elasticsearch\Common\Exceptions\BadRequest400Exception with message 'query_parsing_exception: No value specified for term query'
我看了elasticsearch文档,但没有发现我的错误。我使用的是elasticsearch 2.4。您需要使用inside must not子句。它基本上是一个(在2.2.0中已弃用)查找空值的函数
{
"query": {
"bool": {
"must_not": {
"exists": {
"field": "type"
}
}
}
}
}
我没有使用塑料,但它应该很容易转换。从查询结果来看,它应该类似于下面的查询
App\JobAd::search()->mustNot()->exists('type')->sortBy("id", "ASC")->get()->totalHits();
希望有帮助 我刚刚读到关于丢失的查询的信息,所以我的思路是正确的:)您的代码可以正常工作,谢谢!很高兴我能帮忙!