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

[Core] Highlight search results #343

Open
MaximilianKresse opened this issue Jan 29, 2024 · 8 comments · May be fixed by #349
Open

[Core] Highlight search results #343

MaximilianKresse opened this issue Jan 29, 2024 · 8 comments · May be fixed by #349
Labels
features New feature or request help wanted Extra attention is needed SEAL Core Seal Core related issue

Comments

@MaximilianKresse
Copy link

As far as I can currently see highlighting the search result is currently not supported.

Are there any plans for adding this feature in the (near) future?

@alexander-schranz alexander-schranz added SEAL Core Seal Core related issue help wanted Extra attention is needed labels Jan 29, 2024
@alexander-schranz
Copy link
Member

alexander-schranz commented Jan 29, 2024

Not yet on the issue tracker. As for all features a research would first be required so adding here the help wanted Label. The research should be focus on all currently implemented search engines and check if they support such feature and link here there documentation about it.

Currently supported search engines are:

  • Elasticsearch
  • Opensearch
  • Meilisearch
  • Algolia
  • Loupe
  • Solr
  • RediSearch
  • Typesense

Based on the result of the research it can then be decided if its possible to implement such features in the abstraction and already should us get an idea how we can achieve this. Based on that we can then design a common interface for such feature. If you already did some research around it let us know.

@alexander-schranz alexander-schranz added the features New feature or request label Jan 29, 2024
@alexander-schranz alexander-schranz changed the title Highlight search results [Core] Highlight search results Jan 29, 2024
@mario-fehr
Copy link

Done a quick search for the start:

@alexander-schranz
Copy link
Member

alexander-schranz commented Jan 30, 2024

@mario-fehr Thx for the research 👍


Depending on a quick look we maybe could go with something like and what example Loupe is doing here:

$searchBuilder
    ->addIndex('blog')
    ->addFilter(new Condition\SearchCondition('Test'))
    ->withHighlight(
        fields: ['title', 'description'],
        preTag: '<mark>',
        postTag: '</mark>',
    )

What is also interesting how we handle the highlighting in the Result object. Currently the result has directly the documents. There are two options we put it into a special key something like:

{
    "title": "Text Title",
    "description": "Some Test Description",
    "_highlight": {
        "title": "<mark>Test</mark> Title",
        "description": "Some <mark>Test</mark> description"
    }
}

Or of we split it into 2 seperate objects which would require a ResultItem object:

{
    "document": {
        "title": "Text Title",
        "description": "Some Test Description",
    },
    "highlight": {
        "title": "<mark>Test</mark> Title",
        "description": "Some <mark>Test</mark> description"
    }
}
foreach ($result as $item) {
    $item->document;
    $item->highlight;
    
    $item->document['title'];
    $item->highlight['title'];
    $item->document['description'];
    $item->highlight['description'];
}

@alexander-schranz
Copy link
Member

alexander-schranz commented Jan 30, 2024

Loupe currently seems not yet support pre and postTag configuration but tried to implement that here: loupe-php/loupe#70

@alexander-schranz
Copy link
Member

loupe-php/loupe#70 is already merged an will be part of the next feature release so we can start implementing it here. If somebody want to help implementing it let me know.

@mario-fehr
Copy link

mario-fehr commented Jan 31, 2024

@alexander-schranz Is there a preference which result variant should be used? The _highlight or the ResultItem object one?

@alexander-schranz
Copy link
Member

@mario-fehr I would go with the _highlight without seperate object to keep the bc break currently low.

For new features we create for each adapter an own Pull request to keep things reviewable. And as first implementation It would be good to create the feature first for the seal-memory-adapter.

The following tests can be copied and adopted for a WithHighlighting test:

public function testSearchCondition(): void
{
$documents = TestingHelper::createComplexFixtures();
$schema = self::getSchema();
foreach ($documents as $document) {
self::$taskHelper->tasks[] = self::$indexer->save(
$schema->indexes[TestingHelper::INDEX_COMPLEX],
$document,
['return_slow_promise_result' => true],
);
}
self::$taskHelper->waitForAll();
$search = new SearchBuilder($schema, self::$searcher);
$search->addIndex(TestingHelper::INDEX_COMPLEX);
$search->addFilter(new Condition\SearchCondition('Blog'));
$expectedDocumentsVariantA = [
$documents[0],
$documents[1],
];
$expectedDocumentsVariantB = [
$documents[1],
$documents[0],
];
$loadedDocuments = [...$search->getResult()];
$this->assertCount(2, $loadedDocuments);
$this->assertTrue(
$expectedDocumentsVariantA === $loadedDocuments
|| $expectedDocumentsVariantB === $loadedDocuments,
'Not correct documents where found.',
);
$search = new SearchBuilder($schema, self::$searcher);
$search->addIndex(TestingHelper::INDEX_COMPLEX);
$search->addFilter(new Condition\SearchCondition('Thing'));
$this->assertSame([$documents[2]], [...$search->getResult()]);
foreach ($documents as $document) {
self::$taskHelper->tasks[] = self::$indexer->delete(
$schema->indexes[TestingHelper::INDEX_COMPLEX],
$document['uuid'],
['return_slow_promise_result' => true],
);
}
}

@alexander-schranz alexander-schranz linked a pull request Feb 2, 2024 that will close this issue
11 tasks
@alexander-schranz
Copy link
Member

alexander-schranz commented Feb 2, 2024

Created the main feature branch feature/highlighting which can be used as target for all who want to help implement this feature: #349

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
features New feature or request help wanted Extra attention is needed SEAL Core Seal Core related issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants