在elasticsearch-php中从搜索中排除ID的结果

时间:2016-02-18 07:10:44

标签: php elasticsearch

我尝试的是:

$params['body']['query']['bool']['must_not']['id']['value']=$pid;

但它不起作用。请为正确的语法提供一些帮助。

完整代码:

$pid = $_GET['pid'];
$params = array();
$params['index'] = 'xyz';
$params['type'] = 'product';
$params['body']['query']['filtered']['filter']['and'][]['term']['userid'] = $uid;
$params['body']['query']['filtered']['filter']['and'][]['term']['categoryid3'] = $pc;
$params['body']['query']['bool']['must_not']['id']['value']=$pid;
$params['size'] ='5';
$result = $client->search($params);

更新完整代码

$pid = $_GET['pid'];
$params = array();
$params['index'] = 'xyz';
$params['type'] = 'product';
$params['body']['query']['filtered']['filter']['and'][]['term']['userid'] = $uid;
$params['body']['query']['filtered']['filter']['and'][]['term']['categoryid3'] = $pc;
$params['body']['query']['filtered']['filter']['not'][]['ids']['values'] = [$pid];
$params['size'] ='5';
$result = $client->search($params);

现在只显示该结果,而不是使用pid排除该结果。

注意:自己弄清楚..在答案部分添加

3 个答案:

答案 0 :(得分:2)

正确的查询是ids而不是id

$params['body']['query']['bool']['must_not']['ids']['values'] = [$pid];
                                                ^         ^     ^    ^
                                                |         |     |    |
                                             fix this and this and this

除了你的查询格式不正确之外,它应该是

$params['body']['query']['filtered']['filter']['not'][]['ids']['values'] = [$pid];

答案 1 :(得分:0)

试试这个

$pid = 25;
$params = array();
$params['body']['query']['bool']['must_not']['id']['value']=$pid;

答案 2 :(得分:0)

根据此链接 https://www.elastic.co/guide/en/elasticsearch/guide/current/combining-filters.html ,对于多个过滤器的组合,所有都需要在bool过滤器下。

所以在我将所有内容改为:

后,它才起作用
db.collection.aggregate([{
    $redact: {
        $cond: [
             {$eq: [
                    {$size: {
                         $filter: {
                                   input: '$objects', 
                                   as: 'item', 
                                   cond: {$eq: ['$$item.a', 1]}
                     }}},
                     2]
             },
         '$$KEEP', 
         '$$PRUNE'
         ]}
}]);