GithubHelp home page GithubHelp logo

agoodway / ex_typesense Goto Github PK

View Code? Open in Web Editor NEW

This project forked from jaeyson/ex_typesense

0.0 0.0 0.0 82 KB

Typesense client for Elixir with support for importing your Ecto schemas.

Home Page: https://hexdocs.pm/ex_typesense

License: MIT License

Elixir 100.00%

ex_typesense's Introduction

ExTypesense

Hex.pm Hexdocs.pm Hex.pm Typesense badge

Typesense client for Elixir with support for your Ecto schemas.

Todo

  • creating collection using auto schema detection
  • implement multisearch
  • implement geosearch
  • implement curation
  • implement synonyms

Installation

If available in Hex, the package can be installed by adding ex_typesense to your list of dependencies in mix.exs:

def deps do
  [
    {:ex_typesense, "~> 0.3"}
  ]
end

If you're adding this dep as a local path:

def deps do
  [
    {:ex_typesense, path: "/path/to/ex_typesense"}
  ]
end

then

mix deps.compile ex_typesense --force

Getting started

1. Spin up local typesense server

mkdir /tmp/typesense-server-data
docker container run --rm -it -d \
  --name typesense \
  -e TYPESENSE_DATA_DIR=/data \
  -e TYPESENSE_API_KEY=xyz \
  -v /tmp/typesense-server-data:/data \
  -p 8108:8108 \
  docker.io/typesense/typesense:0.25.1

2. Add creds to config

Config for setting up api key, host, etc.

You can also find api key and host in your dashboard if you're using cloud-hosted Typesense.

config :ex_typesense,
  api_key: "xyz",
  host: "localhost", # "111222333aaabbbcc-9.x9.typesense.net"
  port: 8108, # 443
  scheme: "http" # "https"

3. Create a collection

using Ecto

In this example, we're adding person_id that points to the id of persons schema.

defmodule Person do
  use Ecto.Schema

  defimpl Jason.Encoder, for: __MODULE__ do
    def encode(value, opts) do
      value
      |> Map.take([:id, :person_id, :name, :country])
      |> Enum.map(fn {key, val} ->
        cond do
          key === :id -> {key, to_string(Map.get(value, :id))}
          key === :person_id -> {key, Map.get(value, :id)}
          true -> {key, val}
        end
      end)
      |> Enum.into(%{})
      |> Jason.Encode.map(opts)
    end
  end

  schema "persons" do
    field(:name, :string)
    field(:country, :string)
    field(:person_id, :integer, virtual: true)
  end

  def get_field_types do
    %{
      default_sorting_field: "person_id",
      fields: [
        %{name: "person_id", type: "int32"},
        %{name: "name", type: "string"},
        %{name: "country", type: "string"}
      ]
    }
  end
end

next, create the collection from a module name

ExTypesense.create_collection(Person)

using maps

schema = %{
  name: "companies",
  fields: [
    %{name: "company_name", type: "string"},
    %{name: "company_id", type: "int32"},
    %{name: "country", type: "string"}
  ],
  default_sorting_field: "company_id"
}

ExTypesense.create_collection(schema)

4. Indexing documents

4.a via indexing multiple documents

Post |> Repo.all() |> ExTypesense.index_multiple_documents()

4.b or indexing single document

Post |> Repo.get!(123) |> ExTypesense.create_document()

5. Search

params = %{q: "John Doe", query_by: "name"}

ExTypesense.search(schema.name, params)
ExTypesense.search(Person, params)

Check cheatsheet for more examples

Documentation can be generated with ExDoc and published on HexDocs. Once published, the docs can be found at https://hexdocs.pm/ex_typesense.

ex_typesense's People

Contributors

jaeyson avatar kianmeng avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.