Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[bug]: v4+ Filters don't work using SearchClient::searchSingleIndex() #3698

Closed
1 task done
aykutersoy opened this issue Sep 12, 2024 · 4 comments
Closed
1 task done
Labels
bug Something isn't working

Comments

@aykutersoy
Copy link
Contributor

Description

We're using v4.4.0 and filters don't seem to work when using SearchClient::searchSingleIndex()

Either I'm doing something wrong or filters in query parameters are not taken into account when searching (Please see below reproducible scenario)

I've also tried this but because searchSingleIndex create bodyless requestOptions below body attribute just gets overridden.

...
$searchResponse = $client->searchSingleIndex(
    indexName: $indexName,
    searchParams: ['query' => ''],
    requestOptions: [
        'body' => [
            'filters' => 'value=1',
        ],
    ],
);
...

Language

PHP

Client

Search

Steps to reproduce

<?php
// File: hello_algolia.php
require_once realpath(__DIR__ . "/vendor/autoload.php");

use Algolia\AlgoliaSearch\Api\SearchClient;
$appID = "ALGOLIA_APPLICATION_ID";
// API key with `addObject` and `search` ACL
$apiKey = "ALGOLIA_API_KEY";
$indexName = "test-index";

$client = SearchClient::create($appID, $apiKey);

// Create a new record
$record = [
    "objectID" => "object-1",
    "name" => "test record",
    "value" => "1",
];
$record2 = [
    "objectID" => "object-2",
    "name" => "test record 2",
    "value" => "2",
];

// Add the record to an index
$client->saveObject($indexName, $record);
$client->saveObject($indexName, $record2);

// Search for ''
$searchResponse = $client->searchSingleIndex(
    indexName: $indexName,
    searchParams: ['query' => ''],
    requestOptions: [
        'queryParameters' => [
            'filters' => 'value=1',
        ],
    ],
);

echo json_encode($searchResponse);

// Return both of the records

Relevant log output

No response

Self-service

  • I'd be willing to fix this bug myself.
@millotp
Copy link
Collaborator

millotp commented Sep 12, 2024

Hello, this endpoints uses the read transporter to allow browser and the transporter to cache the request, can you try passing the filter like this ?

$response = $client->searchSingleIndex(
    '<YOUR_INDEX_NAME>',
    ['query' => 'myQuery',
      'filters' => 'value=1',
    ],
 );

I will add this to the snippets if it works properly

@aykutersoy
Copy link
Contributor Author

@millotp It worked perfectly, thank your very much for. I've also noticed the hitsPerPage doesn't do anything when it's in requestOptions but works when it's in search params

requestOptions: [
  'queryParameters' => [
      'hitsPerPage' => 200,
  ],
]
searchParams: [
    'query' => '',
    'filters' => 'value>1',
    'hitsPerPage' => 200,
],

@millotp
Copy link
Collaborator

millotp commented Sep 12, 2024

Thanks for your report, I'll make the usage clearer in the next release, I know its different from the last version but this version is a breaking change and the parameters are not the same as the last client.
Sorry for the discrepancy but it will make more sense for us in the long run !

@aykutersoy
Copy link
Contributor Author

It's all good! To be honest new version is simpler and easy to use, I like it. But if the documentation was more verbose similar to v3 it would've helped a lot.

@millotp millotp closed this as completed Sep 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants