Skip to content

vedang/meilisearch-clj

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Meilisearch Clojure

Meilisearch Clojure

License

⚡ The Meilisearch API client written for Clojure ☕️

Meilisearch Clojure is the Meilisearch API client for Clojure developers. It is a thin shim over the Java API client, and makes the Java API client Clojure friendly.

Meilisearch is an open-source search engine. Learn more about Meilisearch.

Table of Contents

📖 Documentation

This readme contains all the documentation you need to start using this Meilisearch SDK.

For general information on how to use Meilisearch—such as our API reference, tutorials, guides, and in-depth articles—refer to our main documentation website.

⚡ Supercharge your Meilisearch experience

Say goodbye to server deployment and manual updates with Meilisearch Cloud. No credit card required.

🔧 Installation

meilisearch-clj is not currently available from Clojars / Maven. To be able to use this package right now, declare it as a git dependency in your deps file (as shown below). At this point, the library is already useful for all basic usage, and for more complex usage the user can drop down to the Java SDK.

I will flesh out more of the library as and when time permits, and I will upload it to Maven once I have met the criteria outlined in the Meilisearch Integration Guide

The versioning scheme will follow the meilisearch-java versioning scheme, perhaps with a -alpha<version> added to the end until sufficient functionality coverage is achieved.

Maven / Clojars

Currently unavailable, as explained above

Deps.edn

io.github.vedang/meilisearch-clj {:git/sha "<PUT-LATEST-SHA-HERE>"}

Run Meilisearch

There are many easy ways to download and run a Meilisearch instance.

For example, using the curl command in your Terminal:

 # Install Meilisearch
 curl -L https://install.meilisearch.com | sh

 # Launch Meilisearch
 ./meilisearch --master-key=masterKey

NB: you can also download Meilisearch from Homebrew or APT or even run it using Docker.

🚀 Getting started

Add documents

(require '[com.meilisearch.sdk.core :as core])

(def documents
  [{:id 1 :title "Carol" :genres ["Romance" "Drama"]}
   {:id 2 :title "Wonder Woman" :genres ["Action" "Adventure"]}
   {:id 3 :title "Life of Pi" :genres ["Drama" "Adventure"]}
   {:id 4 :title "Mad Max: Fury Road" :genres ["Science Fiction" "Adventure"]}
   {:id 5 :title "Moana" :genres ["Action" "Fantasy"]}
   {:id 6 :title "Philadelphia" :genres ["Drama"]}])

(def task-info "Store this to demonstrate the get-task function later"
      ;; Client Configuration
  (-> {:host "https://localhost:7700"
       :api-key "aWildSalherApplicationAppears"}
      ;; Create a new client
      core/client!
      ;; Get the index where we plan to store the documents
      (core/index "movies")
      ;; If the index 'movies' does not exist, Meilisearch creates it
      ;; when you first add the documents.
      (core/add-documents! documents)))
;; #'user/task-info
;; := {:task-uid 0,
;;     :status "enqueued",
;;     :index-uid "movies",
;;     :type "documentAdditionOrUpdate",
;;     ... }

With the task-uid, you can check the status (enqueued, processing, succeeded or failed) of your documents ongoing addition using the task endpoint.

Continuing on from previous example, let's formalize things a bit and see the status of our task.

;; Continuing on from previous example

(def config "Configuration to connect to our Meilisearch Client"
  {:host "https://localhost:7700"
   :api-key "aWildSalherApplicationAppears"})

(def client "A Meilisearch Client Instance for the rest of our examples"
  (core/client! {:host "https://localhost:7700"
                 :api-key "aWildSalherApplicationAppears"}))

(core/get-task client (:task-uid task-info))
;; :=
;; {:task-uid ...,
;;  :started-at #inst "2023-07-06T10:11:26.904-00:00",
;;  :type "documentAdditionOrUpdate",
;;  :duration "PT0.103231S",
;;  :index-uid "movies",
;;  :status "succeeded",
;;  :error nil,
;;  :finished-at #inst "2023-07-06T10:11:27.008-00:00",
;;  :enqueued-at #inst "2023-07-06T10:11:26.904-00:00"}

Basic Search

A basic search can be performed by calling the search method, with a simple string query.

;; Continuing from previous code blocks

(def index "The Movies Index against which we want to execute a search"
  (core/index client "movies"))

;; Meilisearch is typo-tolerant:
(core/search index "carlo")
;; := {:hits ({:title "Carlos" ...}, {:title "Carol" ...} ...)
;;     :processing-time-ms ... }

Custom Search

If you want a custom search, the easiest way is to create a SearchRequest object, and set the parameters that you need.
All the supported options are described in the search parameters section of the documentation.

;; Continuing from previous code blocks
(core/search "of" {:show-matches-position true
                   :attributes-to-highlight ["title"]
                   :attributes-to-retrieve ["title" "id" "genres"]})
  • Output:
[{:id 3,
  :title "Life of Pi",
  :genres ["Adventure" "Drama"],
  :_formatted {:id 3,
               :title "Life <em>of</em> Pi",
               :genres ["Adventure" "Drama"]},
  :_matchesPosition {:title [{:start 5.0, :length 2.0}]}}]

Custom Search With Filters

If you want to enable filtering, you must add your attributes to the filterableAttributes index setting.

(core/update-filterable-attributes-settings! index ["id" "genres"])
;; := {:type "settingsUpdate", :status "enqueued", :index-uid "movies" ... }

You only need to perform this operation once.

Note that Meilisearch will rebuild your index whenever you update filterableAttributes. Depending on the size of your dataset, this might take time. You can track the process using the task status.

Then, you can perform the search:

(core/search index "wonder" {:filter ["id > 1 AND genres = Action"]})
;; := {:hits ... :query "wonder" ... }
{:hits '({:genres ["Action" "Adventure"], :id 2.0, :title "Wonder Woman"}),
 :facet-distribution nil,
 :processing-time-ms 0,
 :query "wonder",
 :offset 0,
 :limit 20,
 :estimated-total-hits 1}

🤖 Compatibility with Meilisearch

This package guarantees compatibility with version v1.x of Meilisearch, but some features may not be present. Please check the issues for more info.

💡 Learn more

The following sections in our main documentation website may interest you:

⚙️ Contributing

Any new contribution is more than welcome in this project!

If you want to know more about the development workflow or want to contribute, please visit our contributing guidelines for detailed instructions!


Meilisearch provides and maintains many SDKs and Integration tools like this one. We want to provide everyone with an amazing search experience for any kind of project. If you want to contribute, make suggestions, or just know what's going on right now, visit us in the integration-guides repository.

About

Clojure client for the Meilisearch API

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published