black
black

Reputation: 151

Exclude a result by ID from search in elasticsearch-php

I was trying something like:

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

But it doesn't work. Need some help for the correct syntax please.

full code:

$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);

Updated full code

$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);

Instead of excluding that result with the pid, now it is showing only that result.

NOTE: Figured it out myself..Added in the answer section

Upvotes: 1

Views: 1233

Answers (3)

black
black

Reputation: 151

According to this link https://www.elastic.co/guide/en/elasticsearch/guide/current/combining-filters.html ,for combining mutiple filters all needs to be under bool filter.

So it worked after i changed all to :

 $params['body']['query']['bool']['must'][]['term']['userid'] = $uid;
 $params['body']['query']['bool']['must'][]['term']['categoryid2'] = $pc;

 $params['body']['query']['bool']['must_not'][]['ids']['values'] = [$pid];

Upvotes: 0

Val
Val

Reputation: 217304

The correct query for that is ids not id

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

Besides your query is ill-formed, it should be

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

Upvotes: 2

Sudhakar Annadurai
Sudhakar Annadurai

Reputation: 136

try this

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

Upvotes: 0

Related Questions