meilisearch / strapi-plugin-meilisearch

A strapi plugin to add your collections to Meilisearch
https://meilisearch.com
MIT License
223 stars 58 forks source link

How can I implement closed auto-suggestions with MeiliSearch in a Gatsby and Strapi project? #928

Closed belwalshubham closed 6 months ago

belwalshubham commented 6 months ago

Tech Stack:

  1. Frontend: Gatsby
  2. Backend: Strapi
  3. Search Engine: MeiliSearch

Description: I have successfully integrated a global MeiliSearch solution into my Gatsby and Strapi project. The current implementation supports general search queries across various fields. However, I now need to implement a auto-suggestion feature. Current Implementation:

Here's a snippet of the current code setup:

import { MeiliSearch } from 'meilisearch'

export const indexes = [
  {
    index: 'event-detail',
    page: 'Event Detail',
    fieldsToSearchOn: [
      'HeroText',
      'HeroDescription',
      'JumpScrollMenu.MenuTitle',
      'HighlightsHeader.Title',
    ],
  },
]

const baseUrl = process.env.GATSBY_SITE_URL

const meiliSearchClient: MeiliSearch = new MeilisearchAutocompleteClient({
  host: process.env.MEILISEARCH_HOST,
  apiKey: process.env.MEILISEARCH_MASTER_KEY,
})

const getFilteredDataForIndex = async (index: any, filterValue: string) => {
  return await meiliSearchClient
    .index(index.index)
    .search(filterValue, {
      limit: 100,
      attributesToSearchOn: index.fieldsToSearchOn,
      attributesToRetrieve: ['id', 'Slug'],
    })
    .then((data: any) => {
      console.log(data)
      return data?.hits?.map((Item: any) => {
        return {
          keyWord: filterValue,
          redirectUrl: `${baseUrl + (Item.Slug || index.index).replace(/\//g, '')} `,
          value: Number(Item.id),
          page: index.page,
        }
      })
    })
}

export const systemWideData = async (filterValue) => {
  const filteredData = []
  const promises = indexes.map((index) =>
    getFilteredDataForIndex(index, filterValue),
  )

  const results = await Promise.all(promises)

  results.forEach((newData) => {
    if (newData.length > 0) {
      filteredData.push(newData[0])
    }
  })

  return filteredData
}

I want to implement auto-suggestions in the search input field so that when a user types, it provides relevant suggestions in real-time. How can I achieve this?

curquiza commented 6 months ago

Hello @belwalshubham

Looks like your issue is more related to a support need and not a request for a feature or a bug fix. But most of all, you need quick support to help you play with Meilisearch 😁

We are a small team, so we can’t spend much time doing support this specific repo :cry:

Capture d’écran 2023-10-11 à 16 56 38

So, if you want immediate help, we would recommend going to our Discord instead. The community will be able to help you, no matter your question.

Sorry if I miss-understood your use-case. If that's the case, let me know.

Thanks for using Meilisearch!