lucaong / minisearch

Tiny and powerful JavaScript full-text search engine for browser and Node
https://lucaong.github.io/minisearch/
MIT License
4.64k stars 133 forks source link

Is it possible to make autoSuggest suggest the entire title of my blogs instead of just one word? #231

Closed wentallout closed 10 months ago

wentallout commented 11 months ago

I never use a library like this before so sorry for my limited knowledge.

lucaong commented 11 months ago

Hi @wentallout , with the autoSuggest method, it is not possible to autocomplete the full title of the blog post. That's because autoSuggest will suggest plausible search queries (based on the terms present in the index), not search results. In your case, since you want to suggest entire results (the full title of matching blog posts), it’s better to use the search method.

In particular, it is possible to implement what you need by performing a prefix search, and show the title of the best matching articles in the tooltip.

Here is a scaffold of a solution:

const miniSearch = new MiniSearch({
  fields: ['title', /* ...etc. */],
  storeFields: ['title']
})

// Find articles that have "how to" in the title:
const results = miniSearch.search('how to', {
  // Perform prefix search (so that "how to co" would match "how to
  // come up with...")
  prefix: true,
  // Optionally, also enable fuzzy search, to allow for small typos
  fuzzy: true
})

// Then iterate results and suggest the title of the best matching ones
results.forEach((result) => {
  // Suggest the full title (stored in `result.title`)
  // or, alternatively, use `result.id` to lookup the full document
})

You can read the documentation about the available search options for more advanced setup, for example to restrict the search to specific fields, boost fields, etc.

lucaong commented 10 months ago

@wentallout I hope this answer helps you. Since I think this solves your issue, and there is no further reply, I will go on and close the issue for now, but feel free to reply if more information is needed.