An on-disk Search Engine with boolean and free text queries and spelling correction.
Table of contents
Here is an high level overview of the project architecture, you can find a more detailed presentation in the following Medium article.
The backbone of the engine is an inverted index. The main idea is to have, for each word appearing in the documents, a list of document IDs. This allows us to quickly find documents containing a given word.
More specifically, for each term we save a postings list as follows:
Where
We also store offsets for each term, allowing us to jump to the beginning of the postings list for a given term. They are stored in a separate file.
Delta encoding is used to represent document IDs, as they are strictly increasing, the same goes for the term positions and offsets. All those integers are written with Gamma coding. Generic integers, such as list lengths are written in VByte encoding.
The vocabulary is written on disk using prefix compression. The idea is to sort terms and then write them as "matching prefix length", and suffix.
Here is an example with three words:
Spelling correction is used before answering queries. Given a
word
Finally, document paths and lenghts are stored with a similar format.
You can query the index with boolean or free test queries. In the first case you can use the usual boolean operators to compose a query, such as:
In the second case, you just enter a phrase and receive a ranked collection of documents matching the query, ordered by BM25 score.
A window score is also computed, as the cardinality of the user query, divided by the minimum size windows where all query terms appears in a document, or plus infinity if they don't appear all togheter.
Finally they are combined with the following formula:
Index a new document collection
make cli folder=path/to/folder action=build min_f=1 max_p=0.99
The min_f
param filters terms appearing less that it, while max_p
filters terms appearing more than
in max_p
percentage of the documents.
The folder param is a path to a folder containing the documents to index.
The index files will be placed inside a subfolder, .index
.
Here is an example of such structure:
example
├── .index
│ ├── idx.alphas
│ ├── idx.docs
│ ├── idx.offsets
│ └── idx.postings
├── 1.txt
├── 2.txt
├── 3.txt
└── subfolder
├── 1.txt
├── 2.txt
└── 3.txt
The builder will walk recursively down the input folder, skipping hidden ones. The indexer will skip and show an error for non UTF-8 files.
Load a document collection
You can load a pre-build index by running:
make web folder=path/to/folder
This will load the index inside path/to/folder/.index
You can then visit http://0.0.0.0:3000
to find a web interface to enter free text and boolean queries.
Query Syntax
You can perform Google-like free test queries.
You can also specify boolean queries with "b: "
prefix such as:
b: hello AND there OR NOT man
Introduction to Information Retrieval - Christopher D. Manning, Prabhakar Raghavan and Hinrich Schütze
Feel free to get in touch to discuss the project!