{"id":13880085,"url":"https://github.com/ankane/autosuggest","last_synced_at":"2025-11-17T14:14:07.104Z","repository":{"id":62553788,"uuid":"37790152","full_name":"ankane/autosuggest","owner":"ankane","description":"Autocomplete suggestions based on what your users search","archived":false,"fork":false,"pushed_at":"2024-12-30T01:18:50.000Z","size":94,"stargazers_count":203,"open_issues_count":0,"forks_count":3,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-04-01T00:34:24.073Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ankane.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2015-06-20T23:50:37.000Z","updated_at":"2025-03-24T21:37:04.000Z","dependencies_parsed_at":"2023-12-07T21:30:20.419Z","dependency_job_id":"9683c340-1398-4f63-8587-2d238fba034c","html_url":"https://github.com/ankane/autosuggest","commit_stats":{"total_commits":89,"total_committers":4,"mean_commits":22.25,"dds":0.1460674157303371,"last_synced_commit":"b0662fe2b1fb3dc50d416edfee5b257fff714f60"},"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ankane%2Fautosuggest","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ankane%2Fautosuggest/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ankane%2Fautosuggest/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ankane%2Fautosuggest/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ankane","download_url":"https://codeload.github.com/ankane/autosuggest/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247761050,"owners_count":20991531,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-06T08:02:46.602Z","updated_at":"2025-11-17T14:14:07.099Z","avatar_url":"https://github.com/ankane.png","language":"Ruby","funding_links":[],"categories":["Ruby"],"sub_categories":[],"readme":"# Autosuggest\n\nGenerate autocomplete suggestions based on what your users search\n\n:tangerine: Battle-tested at [Instacart](https://www.instacart.com/opensource)\n\n[![Build Status](https://github.com/ankane/autosuggest/actions/workflows/build.yml/badge.svg)](https://github.com/ankane/autosuggest/actions)\n\n## Installation\n\nAdd this line to your application’s Gemfile:\n\n```ruby\ngem \"autosuggest\"\n```\n\n## Getting Started\n\n#### Prepare your data\n\nStart with a hash of queries and their popularity, like the number of users who have searched it.\n\n```ruby\ntop_queries = {\n  \"bananas\" =\u003e 353,\n  \"apples\"  =\u003e 213,\n  \"oranges\" =\u003e 140\n}\n```\n\nWith [Searchjoy](https://github.com/ankane/searchjoy), you can do:\n\n```ruby\ntop_queries = Searchjoy::Search.group(:normalized_query)\n  .having(\"COUNT(DISTINCT user_id) \u003e= 5\").distinct.count(:user_id)\n```\n\nThen pass them to Autosuggest.\n\n```ruby\nautosuggest = Autosuggest::Generator.new(top_queries)\n```\n\n#### Filter duplicates\n\n[Stemming](https://en.wikipedia.org/wiki/Stemming) is used to detect duplicates like `apple` and `apples`.\n\nSpecify the stemming language (defaults to `english`) with:\n\n```ruby\nautosuggest = Autosuggest::Generator.new(top_queries, language: \"spanish\")\n```\n\nThe most popular query is preferred by default. To override this, use:\n\n```ruby\nautosuggest.prefer [\"apples\"]\n```\n\nTo fix false positives, use:\n\n```ruby\nautosuggest.not_duplicates [[\"straws\", \"straus\"]]\n```\n\n#### Filter misspellings\n\nWe tried open-source libraries like [Aspell](http://aspell.net) and [Hunspell](https://github.com/hunspell/hunspell) but quickly realized we needed to build a corpus specific to our application.\n\nThere are two ways to build the corpus, which can be used together.\n\n1. Add words\n\n  ```ruby\n  autosuggest.parse_words Product.pluck(:name)\n  ```\n\n  Use the `min` option to only add words that appear multiple times.\n\n2. Add concepts\n\n  ```ruby\n  autosuggest.add_concept \"brand\", Brand.pluck(:name)\n  ```\n\n#### Filter words\n\n[Profanity](https://github.com/tjackiw/obscenity/blob/master/config/blacklist.yml) is blocked by default. Add custom words with:\n\n```ruby\nautosuggest.block_words [\"boom\"]\n```\n\n#### Generate suggestions\n\nGenerate suggestions with:\n\n```ruby\nsuggestions = autosuggest.suggestions\n```\n\n#### Save suggestions\n\nSave suggestions in your database or another data store.\n\nWith Rails, you can generate a simple model with:\n\n```sh\nrails generate autosuggest:suggestions\nrails db:migrate\n```\n\nAnd update suggestions with:\n\n```ruby\nnow = Time.now\nrecords = suggestions.map { |s| s.slice(:query, :score).merge(updated_at: now) }\nAutosuggest::Suggestion.transaction do\n  Autosuggest::Suggestion.upsert_all(records, unique_by: :query)\n  Autosuggest::Suggestion.where(\"updated_at \u003c ?\", now).delete_all\nend\n```\n\nLeave out `unique_by` for MySQL.\n\n#### Show suggestions\n\nUse a JavaScript autocomplete library like [typeahead.js](https://github.com/twitter/typeahead.js) to show suggestions in the UI.\n\nIf you only have a few thousand suggestions, it’s much faster to load them all at once instead of as a user types (eliminates network requests).\n\nWith Rails, you can load all suggestions with:\n\n```ruby\nAutosuggest::Suggestion.order(score: :desc).pluck(:query)\n```\n\nAnd suggestions matching user input with:\n\n```ruby\ninput = params[:query]\nAutosuggest::Suggestion\n  .order(score: :desc)\n  .where(\"query LIKE ?\", \"%#{Autosuggest::Suggestion.sanitize_sql_like(input.downcase)}%\")\n  .pluck(:query)\n```\n\nYou can also cache suggestions for performance.\n\n```ruby\nRails.cache.fetch(\"suggestions\", expires_in: 5.minutes) do\n  Autosuggest::Suggestion.order(score: :desc).pluck(:query)\nend\n```\n\n#### Additional considerations\n\nYou may want to have someone manually approve suggestions:\n\n```ruby\nAutosuggest::Suggestion.where(status: \"approved\")\n```\n\nOr filter suggestions without results:\n\n```ruby\nAutosuggest::Suggestion.find_each do |suggestion|\n  suggestion.results_count = Product.search(suggestion.query, load: false).count\n  suggestion.save! if suggestion.changed?\nend\n\nAutosuggest::Suggestion.where(\"results_count \u003e 0\")\n```\n\nYou can add additional fields to your model/data store to accomplish this.\n\n## Example\n\n```ruby\ntop_queries = Searchjoy::Search.group(:normalized_query)\n  .having(\"COUNT(DISTINCT user_id) \u003e= 5\").distinct.count(:user_id)\nproduct_names = Product.pluck(:name)\nbrand_names = Brand.pluck(:name)\n\nautosuggest = Autosuggest::Generator.new(top_queries)\nautosuggest.parse_words product_names\nautosuggest.add_concept \"brand\", brand_names\nautosuggest.prefer brand_names\nautosuggest.not_duplicates [[\"straws\", \"straus\"]]\nautosuggest.block_words [\"boom\"]\n\nsuggestions = autosuggest.suggestions\n\nnow = Time.now\nrecords = suggestions.map { |s| s.slice(:query, :score).merge(updated_at: now) }\nAutosuggest::Suggestion.transaction do\n  Autosuggest::Suggestion.upsert_all(records, unique_by: :query)\n  Autosuggest::Suggestion.where(\"updated_at \u003c ?\", now).delete_all\nend\n```\n\n## History\n\nView the [changelog](https://github.com/ankane/autosuggest/blob/master/CHANGELOG.md)\n\n## Contributing\n\nEveryone is encouraged to help improve this project. Here are a few ways you can help:\n\n- [Report bugs](https://github.com/ankane/autosuggest/issues)\n- Fix bugs and [submit pull requests](https://github.com/ankane/autosuggest/pulls)\n- Write, clarify, or fix documentation\n- Suggest or add new features\n\nTo get started with development:\n\n```sh\ngit clone https://github.com/ankane/autosuggest.git\ncd autosuggest\nbundle install\nbundle exec rake test\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fankane%2Fautosuggest","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fankane%2Fautosuggest","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fankane%2Fautosuggest/lists"}