Skip to content

marcinwaldowski/try-postgres-fuzzy-search

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Proof of concept mini project which shows combined PostgreSQL 9.6 features (full text search and trigrams) to mimic some Google-like search capabilities typically achievable with Elasticsearch.

Implemented as a bunch of Clojure functions which allow to:

  • connect to PostgreSQL,
  • create database schema (tables and triggers),
  • populate database with random data,
  • perform search.

Usage

  1. Install Java 8.

  2. Install Leiningen.

  3. Create PostgreSQL database and user which is owner of it. The database needs to have pg_trgm extension.

CREATE EXTENSION pg_trgm;
  1. Clone this repository.

  2. Edit file /resources/config.edn to set:

  • database name,
  • database host,
  • database port,
  • database user,
  • database password.
  1. cd to cloned repository and execute:
lein repl

The output should be:

nREPL server started on port 35213 on host 127.0.0.1 - nrepl://127.0.0.1:35213
REPL-y 0.3.7, nREPL 0.2.12
Clojure 1.8.0
Java HotSpot(TM) 64-Bit Server VM 1.8.0_111-b14
    Docs: (doc function-name-here)
          (find-doc "part-of-name-here")
  Source: (source function-name-here)
 Javadoc: (javadoc java-object-or-class-here)
    Exit: Control+D or (exit) or (quit)
 Results: Stored in vars *1, *2, *3, an exception in *e

 try-postgres-fuzzy-search.core=>
  1. Create database schema with:
(create-schema!)
  1. Connect to database with:
(connect!)
  1. Populate database with one million persons with:
(insert-persons! 1000000)
  1. Get random data from database.
(pprint (get-random-persons 5))

The example output:

({:id 17820, :first_name "Barney", :last_name "Gislason"}
 {:id 17821, :first_name "Destiny", :last_name "Rogahn"}
 {:id 17822, :first_name "Allison", :last_name "Altenwerth"}
 {:id 17823, :first_name "Willie", :last_name "Rippin"}
 {:id 17824, :first_name "Pink", :last_name "Crona"})
nil
  1. Perform search:
  • search without typos:

    (pprint (search-person "Barney" "Gislason"))
  • search with typos and with beginning of word:

    (pprint (search-person "Berney" "Gisl"))
  • search by only one word (with typo):

    (pprint (search-person "Gisleson"))
  1. Disconnect from database.
(disconnect!)
  1. (Optionally) Drop database schema.
(drop-schema!)
  1. Exit REPL.
exit

License

Eclipse Public License version 1.0.

About

Trying PostgreSQL fuzzy search

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published