itemsapi / itemsjs

Extremely fast faceted search engine in JavaScript - lightweight, flexible, and simple to use
Apache License 2.0
346 stars 41 forks source link

searchableFields does not work well if array items contain dashes or spaces #133

Closed LouisRoyT closed 10 months ago

LouisRoyT commented 11 months ago

When items in the dataset have a (flat)array property, searching through that flat array using "config.searchableFields" only works reliably if the strings are "simple" (no dashes or spaces).

For instance, given the following config & dataset, I believe the expectation would be searching for "bananas", "CL-bananas", "plums" or "oranges" should return the item with id = 1. This is NOT what happens.

searchTerm:"oranges" works fine searchTerm:"CL" matches, but "CL-bananas" does not searchTerm:"purple" matches, but "purple plums", "plums" do not

config = { searchableFields: ["fruits"] },
items = [
      {
        id: "1",
        fruits: [
          "CL-bananas", "purple plums", "oranges"
        ]
      },
      //...
];

Codepen reproduction: https://codepen.io/louisroyt/pen/abXvBNo

cigolpl commented 10 months ago

I'd like to suggest integrating with Lunr 2.0 for the search functionality. I believe that by leveraging Lunr 2.0, the search queries can be enhanced and made significantly better. More info here: https://github.com/itemsapi/itemsjs/blob/master/docs/lunr2-integration.md and there is Lunr 2 query interface - https://lunrjs.com/guides/searching.html#scoring

LouisRoyT commented 10 months ago

Thanks for the quick response. I will try this approach and get back to you.