babenkoivan / elastic-scout-driver-plus

Extension for Elastic Scout Driver
MIT License
267 stars 52 forks source link

Adding constraints to multi-index multimatch query #123

Closed alexc-wormhole closed 2 years ago

alexc-wormhole commented 2 years ago

I am searching 10+ indexes at once for the same keyword. However, i need to return only the results a user is authorized to view.

Each of these have a "property_id" key attached to them on the ES index. The eloquent models do not have this key.

Here's the query itself:

        $config = Query::multiMatch ()
          ->fields (['type', 'label', 'description', 'canonical', 'property', 'building', 'location',])
          ->query ('*'.$request->get ('q').'*');

Which i then use like so:

        $engine = Room::searchQuery ($config)
          ->load (['some_relation'], Room::class);
          ->join (ModelA::class)->load (['another_relation'], ModelA::class)
          ->join (ModelB::class)->load (['different_relation'], ModelB::class);

Finally getting it with this:

$results = $engine->execute()->models();

Ideally i need to use one of these to constrain the query on each index to the results tagged with a specific (parent) property_id to avoid users receiving search results they're not entitled to see.

->should (Query::match()->field('property_id')->query('12345')) // or
->method ('match', ['property_id' => '12345']) // 

// equivalent to in Scout
->where ('property_id', '12345')

Adding it to SearchQuery() generates an exception. Is there a better way to constrain multi-index queries where you need to search multiple fields?

P.S. Amazing package man, thanks for creating it.

babenkoivan commented 2 years ago

Hey @alexc-wormhole, have you tried to make a bool query? Not sure if this is exactly what you need, but maybe it drives you in the right direction:

$config = Query::bool()
    ->must(
        Query::multiMatch()
            ->fields (['type', 'label', 'description', 'canonical', 'property', 'building', 'location',])
            ->query ('*'.$request->get ('q').'*')
    )
    ->filter( // you can also use should instead
        Query::term() // you can use match instead
            ->field('property_id')
            ->query('12345')
    );
github-actions[bot] commented 2 years ago

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 7 days