jolicode / elastically

🔍 JoliCode's Elastica wrapper to bootstrap Elasticsearch PHP integrations
248 stars 37 forks source link

ResultSetBuilder::buildModel(): Argument #2 ($indexName) must be of type string #116

Closed insekticid closed 1 year ago

insekticid commented 1 year ago

ResultSetBuilder::buildModel() should accept array of indexes, when searching between multiple indexes

use JoliCode\Elastically\Client;

class ContentRepository
{
    public function __construct(
        private readonly Client $client)
    {
    }

   function getSearch(Query $query): Search  {
      $search = $this->client->getIndex('content-cs')->createSearch();
      $search->addIndexByName('content-de');
      return $search->setQuery($query);
  }
}

(new ContentRepository())->getSearch($query)->search();

[ResultSetBuilder::buildModel] Argument #2 ($indexName) must be of type string

Found problem here https://github.com/ruflin/Elastica/blob/6d51cdbff2fb37aceac8b70adf9042ccb9be0aed/src/Result.php#L126

getParam() returns empty array when key not exists and yes, it is missing https://github.com/ruflin/Elastica/blob/6d51cdbff2fb37aceac8b70adf9042ccb9be0aed/src/Result.php#L68 image

damienalexandre commented 1 year ago

Hi, thanks for the detailed report.

Did you use something like the Elasticsearch filter_path option to have the "_index" field missing? Because it's always there by default.

The thing is, buildModel will always require the index name because it use it to map the source to the appropriate Model class.

insekticid commented 1 year ago

@damienalexandre whow! you are life saver, I am using filter_path problem solved! maybe there should be exception when empty array is returned

https://github.com/jolicode/elastically/blob/a116efb71eb81576bf21efab32ea6d0bbaf9b57d/src/ResultSetBuilder.php#L70

if (!$result->getIndex()) {
throw new \Exception('Returned indexName is empty. Check your filter_path')
}
damienalexandre commented 1 year ago

Yes that would be a good idea!

Lets keep this issue open to implement this (feel free to open a PR if you have the time by the way!).