angeloashmore / gatsby-plugin-local-search

Gatsby plugin for providing client-side search for data available in Gatsby's GraphQL layer using a variety of engines
MIT License
51 stars 29 forks source link

Prioritizing search results / multiple data types #23

Closed sn3h closed 4 years ago

sn3h commented 4 years ago

Hello,

let's say I have sections : [{"section: "Economy", url: '/economy'}, {"section: "Fun", url: '/fun'}] articles: [{id: 1, "section: "Economy", title, url}, {id: 2, "section: "Economy", title, url}, {id: 3, "section: "Fun", title, url}]

and when I query "economy" I'd like to get something like this:

Sections: Economy

Articles: Title (from Economy) Title 2 (from Economy)

where the query is either in sections section key, or article title.

Would you suggest to use multiple useFlexSearch hooks and multiple gatsby-plugin-local-search instances, or is there any simple way how to connect the data in normalizer as I could query multiple object types and filtering the results somehow later in render function?

Thank you very much for any insight. Even pointing me in any direction would be great :)

angeloashmore commented 4 years ago

Hi @sn3h, apologies for the late reply.

I've typically handled this kind of problem by creating two indexes and using separate useFlexSearch hooks. This is helpful since the two types (e.g. "sections", "articles") could potentially have different shapes and affords you the ability to handle them differently.

There may technically be some negative performance implications by having two separate indexes processed at runtime, but it's been fast enough for me to not notice.

Hope that helps! Let me know if you need further clarification.