Typesense: A search engine that works out of the box

Typesense: A search engine that works out of the box

Typesense is an open source and fast typo-tolerant search engine that is optimised for instant searches alongside creating a great developer-experice. The aim is to enable developers build fast blazing search experience.

In this article we will go through a short tutorial on creating a document, indexing the document and then searching it on typesense. But before that, we'll talk about some features of typesense.

Prerequisites

•knowledge on APIs

•Basic knowledge on one of these languages; javascript, Ruby, python, php,because they are some of the tools that integrates with typesense.

Features

Typesense has more features than this but here are some features of typesense.

•Typo-tolerance: Apart from being a super fast search engine, typesense handles typographical errors elegantly. It automatically corrects your spelling mistakes.

•Synonyms: The synonyms is a feature that shows results for a word in place of another when you define them as synonyms. Example is to show car when a user searches for vehicles.

•Grouping: Grouping provides more variety in your results by grouping the results. For example, combining all sizes of a shoe into a single result.

•Filtering: This feature helps fetch records that match a given filter, aggregate field values and get counts of values across records.

Here's a short demo showing how we can create a collection, index a document and search it on typesense using php.

First, let's start the typesense server via Docker:

docker run -p 8108:8108 -v/tmp/data:/data typesense/typesense:0.13.0 --data-dir /data --api-key=Hu52dwsas2AdxdE

Then let's install the PhP client for typesense. Note that there are clients for other for other languages like javascript, python, Ruby and so on.

Composer require php-http/curl-Client typesense/typesense-php
$Client = new Client (
[ 'api_key' => 'Hu52dwsas2AdxdE'
   'nodes' => [['host' => 'localhost', 'port' => '8108', 'protocol' => 'http']],
  ]
);
$schema = [ 
   "name" =>"companies",
    "feilds" => [
        [ "name" => "company_name", "type" => "string" ],
        [ "name" => "num_employees", "type" => "int32" ],
        [ "name" => "country", "type" => "string", "facet" = true ]
  ],
  "default_sorting_feild" => "num_employees"
  ];
  $Client->collections->create($schema)

Next, Let's add(index) a document to the collection we just created.

$document = [
  'id' => '124',
  'company_name' => 'Stark Industries',
  'num_employees' => 5215,
  'country' => 'USA'
 ];
 $schema->collections['companies']document->create($document);

Note that the id should not include spaces or any other character that requires encoding in URLs.

Now, we can search for the document we just indexed.

$searchParameters = [
  'q' => 'stark',
  'query_by' => 'company_name',
  'filter_by => 'num_employees: > 100',
  'sort_by' => 'num_employees: desc"
  ];
  $client->collections['companies']->documents->create($document)->search($searchParameter);

Conclusion

There are several other nice stuffs about typesense that are not covered in this article e.g some features, you can know more about typesense here. Also for a deep dive into typesense API, visit its API documentation. When next you're building a web application or web site that requires a search engine, I recommend that you use typesense. Happy coding!