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

Add support for escape characters #1311

Open
thomas-lcdp opened this issue Jul 30, 2024 · 3 comments
Open

Add support for escape characters #1311

thomas-lcdp opened this issue Jul 30, 2024 · 3 comments
Labels
enhancement New feature or request

Comments

@thomas-lcdp
Copy link
Contributor

thomas-lcdp commented Jul 30, 2024

Description
In Algolia we can exclude values by adding an escape character "-" as explain in documentation
https://www.algolia.com/doc/api-reference/api-parameters/facetFilters/#escape-characters

Solution
Maybe add the conversion from attribute=-value to attribute!=value in file-adapter.ts

/**
 * Transform InstantSearch [facet filter](https://www.algolia.com/doc/api-reference/api-parameters/facetFilters/)
 * to Meilisearch compatible filter format.
 * Change sign from `:` to `=`
 * "facet:facetValue" becomes "facet=facetValue"
 *
 * Wrap both the facet and its facet value between quotes.
 * This avoids formatting issues on facets containing multiple words.
 * Escape backslash \\ and quote " characters.
 *
 * 'My facet:My facet value' becomes '"My facet":"My facet value"'
 *
 * @param {string} filter
 * @returns {string}
 */
function transformFacetFilter(filter: string): string {
  const escapedFilter = getValueWithEscapedBackslashesAndQuotes(filter)
  const colonIndex = escapedFilter.indexOf(':')
  const attribute = escapedFilter.slice(0, colonIndex)
  const value = escapedFilter.slice(colonIndex + 1)
  return `"${attribute}"="${value}"`
}
@curquiza
Copy link
Member

curquiza commented Aug 7, 2024

Hello @thomas-lcdp

Thank you for the suggestion

I'm not sure it will work (or maybe I did not understand) because meilisearch, the search engine itself, is not able to accept escaped value.
I tried to create a facet field named -price and then to apply facet search on it , but the engine does not return anything

/indexes/{{indexUID}}/facet-search
{ 
      "facetName": "-price",
      "q": ""
}
{
    "facetHits": [],
    "facetQuery": null,
    "processingTimeMs": 0
}

I think it's more a bug on the engine side, what do you think?

@thomas-lcdp
Copy link
Contributor Author

thomas-lcdp commented Aug 7, 2024

Hello @curquiza

In Algolia, the escaped value is supported, so you can have a facetFilters: ["owner.id:-7"] to exclude data.
But the conversion from meilisearch-instantsearch is owner.id = -7
Maybe it could be possible to transform owner.id:-7 to owner.id != 7

@curquiza
Copy link
Member

curquiza commented Aug 7, 2024

Oh, in my previous message I confused with "search in facet value". We don't always have the same naming with Algolia.

I think I now better understand your issue. When you want to do filter, you want Meilisearch to send "filter": "\"field\" = \"my value\"" instead of "filter": "field = my value"?

Specifically, regarding what you said about the -: the search engine itself does not support the exclusion of some facets value or filter with - but with the NOT

@curquiza curquiza added the enhancement New feature or request label Aug 7, 2024
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
None yet
Development

No branches or pull requests

2 participants