我正在尝试编写查询(使用 foselastica bundle ),其中值应该是某个 id 或可以为空。
在 mysql 中:WHERE city.id = 1 OR city.id IS NULL
我知道我应该使用存在而不是表达,但不适合我。有什么建议吗?
$query = new \Elastica\Query();
if ($phrase) {
$queryString = new \Elastica\Query\QueryString($phrase);
$query->setQuery($queryString);
}
$filter = new \Elastica\Query\BoolQuery();
$city = new \Elastica\Query\Match();
$city->setFieldQuery('city.id', $cityId);
$filter->addShould($city);
$nullCity = new \Elastica\Query\Exists('city.id');
$filter->addMustNot($nullCity);
$query->setPostFilter($filter);
最佳答案
你已经很接近了。 尝试类似的事情
$query = new \Elastica\BoolQuery();
// ...
$filter = new \Elastica\Query\BoolQuery();
$city = new \Elastica\Query\Match();
$city->setFieldQuery('city.id', $cityId);
$filter->addShould($city);
$nullCity = new BoolQuery();
$existQuery = new \Elastica\Query\Exists('city.id');
$nullCity->addMustNot($existQuery);
$filter->addShould($nullCity);
$query->addMust($filter);
关于php - elasticsearch - 其中字段为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45167729/