question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Approaches to implement search autocomplete suggestions

See original GitHub issue

While user is typing, I want it to suggest terms in a dropdown that can autocomplete. On the Next.js side, I think it’s just a typical debounced input field.

But is there any support for this feature in next-drupal? And how to configure for on the Drupal side?

Thanks.

(Please let me know if Issues is not the place for such discussions. If the discussion is open, I’ll probably post there.)

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:9

github_iconTop GitHub Comments

1reaction
shadcncommented, Apr 13, 2022

@fnick851 The original question did not mention Search API so I went with the easiest solution 😃

You can totally replace getResourceCollection with getSearchIndex with filters.

1reaction
shadcncommented, Apr 12, 2022

Yes. You can use a simple debounced field and an API route for filtering.

Here’s an example: /pages/api/autocomplete.ts

import { NextApiRequest, NextApiResponse } from "next"
import { getResourceCollection } from "next-drupal"

export default async function (
  request: NextApiRequest,
  response: NextApiResponse
) {
  if (request.method !== "POST") {
    return response.status(405).end()
  }

  try {
    const { search } = request.body

    if (!search) {
      throw new Error(`'search' missing`)
    }

    // Make a request to Drupal to find article containing search.
    const articles = await getResourceCollection("node--article", {
      params: {
        "filter[title][operator]": "CONTAINS",
        "filter[title][value]": search,
        "fields[node--article]": "title",
      },
    })

    return response.json(articles)
  } catch (error) {
    return response.status(422).end(error.message)
  }
}

You can now make a post request to this route to get articles containing search keywords.

Read more comments on GitHub >

github_iconTop Results From Across the Web

10 Autocomplete Search Best Practices - How Predictive ...
10 Search Autocomplete Strategies · 1. How Ranking the Suggestions Should Work · 2. Personalization Makes Autocomplete Predictions More Effective · 3. Keep...
Read more >
A Complete Guide for Lighting Fast Autocomplete Search ...
Implementation for API. Let's start with the quickest solution to get up and running in a couple of minutes. Use any SQL database...
Read more >
Approaches for implementing Autocomplete Feature
The various approaches for Text Auto-completion are Linear Search (Brute Force), Binary Search Approach, TRIE Data Structure, Ternary Search Tree Approach, ...
Read more >
What is Autocomplete Search? How Does it work? - AddSearch
One easy way to implement autocomplete is using the AddSearch widget. It offers automatic search suggestions, generated from search queries made ...
Read more >
The Ultimate Guide to Search Suggestions and Autocomplete
Search suggestions and autocomplete all perform similar tasks - predicting the search query of the user as they type and providing best-matching search...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found