Code Monkey home page Code Monkey logo

autocompletex's Introduction

Autocompletex

Coverage Status Build Status hex.pm

Autocompletex is a low-latency plug-and-play autocomplete tool using Redis sorted set. Written in pure Elixir, it focuses on rapid prototyping using your existing stack: simply start a redis instance, and start_link a GenServer.

Currently, it provides these implementation:

  • Google-like query prediction based on popularity. E.G. ne -> netflix, new york times
  • Lexicographic. Sorted in alphabetical order (faster)

There are two ways to run it:

  • Use it as a standalone microservice with HTTP connection.
  • GenServer

Installation

Add the :autocompletex to your mix.exs file:

def deps do
  [{:autocompletex, "~> 0.1.0"}]
end

Then add it to applications:

defp application() do
  [applications: [:logger, :autocompletex]]
end

Then, run mix deps.get in your shell to fetch the new dependency.

Usage

Overview

Currently, two types of autocompletion are supported:

  • Lexicographic
  • Predictive

If you want to suggest another scheme, please post an issue.

There are 3 ways to run it.

  1. Standalone HTTP service
  2. Using a GenServer
  3. Supervision tree

Manual

To start a GenServer manually:

# For Lexicographic:
{:ok, conn} = Redix.start_link
db = "testdb"
{:ok, worker} = Autocompletex.Lexicographic.start_link(conn, db, Autocompletex.Lexicographic)

# For Predictive:
{:ok, conn} = Redix.start_link
db_prefix = "autocompletex"
{:ok, worker} = Autocompletex.Predictive.start_link(conn, db_prefix, Autocompletex.Predictive)

Alternatively, you can use it in a supervision tree.

Add this to config.exs:

config :autocompletex,
  redis_host: "localhost",
  redis_port: 6379,
  redis_string: nil,
  http_server: true,
  http_port: 3000,
  debug: false, # runs :observer.start if true
  type: :lexicographic #:predictive

Then call

Autocompletex.Lexicographic.upsert(Autocompletex.Lexicographic, ["test", "example"])

If http_server is set to true, two http endpoints will be accessible at the designated http_port(default: 3000).

upsert   -> /add?term=Elixir
complete -> /complete?term=te

API

There are two functions: upsert and complete.

upsert/2 means insert or update. For Lexicographic, if a query is already inserted, it will do nothing. For Predictive, it will increment the score of the query.

complete/3 returns a list of matched results. It takes 3 parameters: pid, prefix, rangelen. rangelen is the number of results to be returned. Defaults to 50.

:ok = Autocompletex.Lexicographic.upsert(worker, ["test", "example"])
{:ok, val} == complete(worker, "te") # assert val == ["test"]

Misc

Import file into Redis

If you have a list of user-generated search queries, you can use a mix task to index and provision the redis instance.

Simply do:

mix autocompletex.import --filename [path/to/file] [--predictive]

Internals

For predictive autocompletion, this tool will create keys [dbname]:[prefixes] as sorted sets. For example, for dbname autocompletex, word test:

autocompletex:t
autocompletex:te
autocompletex:tes

For lexicographic autocompletion, under sorted set [dbname].

Docs

To be updated. In the meantime, I'm happy to answer questions in issues.

autocompletex's People

Contributors

0b01 avatar njichev avatar rickyhan avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.