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

Show matching text in search results #672

Open
danieladugyan opened this issue Jan 7, 2025 · 2 comments
Open

Show matching text in search results #672

danieladugyan opened this issue Jan 7, 2025 · 2 comments
Labels
enhancement New feature or request

Comments

@danieladugyan
Copy link
Member

Description

I can search for the content of news articles, documents, and so on and find matching documents. For instance, this is what I see if I search for "Missa inte chansen att vara med" which is part of my recent article "Sista dagen att söka CPU 2025!".

image

Note that the text shown in the result is just the first part of the article, not the text matching what I searched for.

Describe the solution you'd like

Meilisearch provides a solution to this: the attributesToHighlight parameter. By setting it to ['*'] we get back a formatted search result that wraps matching text in <em> tags (or another tag of your choosing). Instead of showing the first part of the article, we could find and show the text that was wrapped in <em>.

Describe alternatives you've considered

attributesToHighlight is not perfect though: what do we do if there are multiple highlighted parts in a search result? e.g if someone searches for a word that occurs many times in an article. Meilisearch will happily wrap all matching parts, but it's not clear what we should show in the UI.

Instead, we could use showMatchesPosition. It returns an array of match positions (start and length), and we could conceivably extract a substring from the first start position until the end of the last match, within a reasonable maximum length.

Example:

"_matchesPosition": {
  "overview": [
    {
      "start": 4,
      "length": 6
    },
    {
      "start": 11,
      "length": 5
    },
    {
      "start": 234,
      "length": 6
    },
    {
      "start": 241,
      "length": 5
    }
  ]
}

could return result.substr(4, 16)

@danieladugyan danieladugyan added the enhancement New feature or request label Jan 7, 2025
@github-project-automation github-project-automation bot moved this to 🆕 New in CPU Jan 7, 2025
@danieladugyan
Copy link
Member Author

danieladugyan commented Jan 7, 2025

We could build on this to make sure PDFs are opened on the correct page by counting how many page breaks occur before the matching part of the document. Eventually we'll be able to highlight a matching part of the PDF.

cc @alfredgrip

@alfredgrip
Copy link
Contributor

This would be a very nice feature! I tried showMatchesPosition briefly and it looks promising and IMO the way to move forward. I suggest just use the first match if there are many, keep it simple since the returned values are just a preview and not the full text.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
Status: 🆕 New
Development

No branches or pull requests

2 participants