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.
-
Install Java 8.
-
Install Leiningen.
-
Create PostgreSQL database and user which is owner of it. The database needs to have pg_trgm extension.
CREATE EXTENSION pg_trgm;
-
Clone this repository.
-
Edit file
/resources/config.edn
to set:
- database name,
- database host,
- database port,
- database user,
- database password.
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=>
- Create database schema with:
(create-schema!)
- Connect to database with:
(connect!)
- Populate database with one million persons with:
(insert-persons! 1000000)
- 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
- 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"))
- Disconnect from database.
(disconnect!)
- (Optionally) Drop database schema.
(drop-schema!)
- Exit REPL.
exit
Eclipse Public License version 1.0.