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

Allow custom hits iterator #88

Open
booni3 opened this issue Feb 11, 2020 · 4 comments
Open

Allow custom hits iterator #88

booni3 opened this issue Feb 11, 2020 · 4 comments
Labels
help wanted Extra attention is needed

Comments

@booni3
Copy link

booni3 commented Feb 11, 2020

We need a method to be able to swap out the hits generator on a search by search basis. For example if adding the highlight feature, we need to be able to merge the highlighted html back into the results.

I suggested add this into the ElasticSearchEngine class but obviously needs better implementation.

$model::search($query,
    function (Client $client, Search $body) use ($model, $field) {
        $???->useHitsIterator(HighlighterHitsIteratorAggregate::class);
        $highlight = new Highlight();
        $highlight->addField($field);
        $body->addHighlight($highlight);
        return $client->search(['index' => $model->searchableAs(), 'body' => $body->toArray()]);
    })->paginate(100);
@matchish
Copy link
Owner

@matchish
Copy link
Owner

matchish commented Feb 12, 2020

Or you can extend builder in your app to

$model::search($query,
    function (Client $client, Search $body) use ($model, $field) {
        $highlight = new Highlight();
        $highlight->addField($field);
        $body->addHighlight($highlight);
        return $client->search(['index' => $model->searchableAs(), 'body' => $body->toArray()]);
    })->using(new Engine(new HitsIteratorFactory))->paginate(100);

But I'm still not sure what solution is better. Or maybe we should propose to change api
in laravel/scout

@matchish
Copy link
Owner

Now perfect api for me looks like(no callback at all)

$model::search(new Query($term))->engine(new Engine(new ResultsFactory))->paginate(100);

Or now we can do like this

$raw = $model::search($query,
    function (Client $client, Search $body) use ($model, $field) {
        $highlight = new Highlight();
        $highlight->addField($field);
        $body->addHighlight($highlight);
        return $client->search(['index' => $model->searchableAs(), 'body' => $body->toArray()]);
    })->raw();
$resullts = new HiglightedResults($raw);

@matchish
Copy link
Owner

Or without changing laravel/scout api
$model::search($term)->query(new ResultsFactory)->paginate(100);

We should't choose how to map results in the engine if we have queryCallback just send to the callback raw results then ResultsFactory can map it in the right way

@booni3 Now I'm working on another issue, I'm trying to finish parallel importing so feel free to send PR if you want.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants