Git Product home page Git Product logo

search_engine's Introduction

A Search Engine Built With Redisearch and Go

This is a microservice written in Go, which allows developers to index and search for documents with Redis.

Demo

This microservice uses the player list from basketball reference as a dataset to show the functionality of the search capabilities of this Redisearch/Go API. The UI for a live version of this API can be found here. Test it out by searching for your favorite basketball players. You can also visit a live version of the API here to test out the Swagger UI.

Setup

The easiest way to get up and running is to create a config.yaml file with your environment variables, run redis-stack with docker-compose, and start the Go API with make run. The values from the config-example.yaml should work if you copy and paste them into a config.yaml file, but feel free to customize them for your needs.

Makefile commands

The makefile contains the main commands you will need to get up and running. I prefer to run make docker-run and then make run to start docker and the Go server separately. This allows me to play with the Go code without shutting down Docker. There is also a Dockerfile for the API which can be built and run in development, however I use it for deployment purposes.

Authorization

This API requires a bearer token for certain endpoints. You can retrieve your bearer token by logging in to the API with your credentials from the config.yaml file. Once you have your bearer token, you can pass it in as a header for curl commands or enter it in the Swagger UI.

Redisearch Schema

In order to customize this API for your own dataset, you will need to change the schema in models/schema.go. The Document type, the variables denoting the schema values, and the schema in models.CreateSchema will need to be updated if you use your own dataset. In the current example, I only use redisearch.NewTextFieldOptions but Redisearch also supports numeric and geo fields. Check the docs for more information.

// models/schema.go

package models

import (
	"github.com/RediSearch/redisearch-go/redisearch"
	"github.com/gomodule/redigo/redis"
)

// These values are used to build the schema and in the controller package
// for sortable and highlighted fields. 
const (
	Name   = "name"
	Link   = "link"
	Active = "active"
)

// Document should reflect the data structure you would like to index with Redis
type Document struct {
	// Name of NBA player
	Name string `json:"name"`
	// Link to player page
	Link string `json:"link"`
	// Active represents the years a player was active
	Active string `json:"active"`
}

type Documents []Document

// CreateSchema is used to create the schema for your Redisearch documents,
// which will allow you to add your data in the form of these documents
func CreateSchema() *redisearch.Schema {
	sc := redisearch.NewSchema(redisearch.DefaultOptions).
		AddField(redisearch.NewTextFieldOptions(Name,
			redisearch.TextFieldOptions{Weight: 5.0, Sortable: true})).
		AddField(redisearch.NewTextFieldOptions(Link,
			redisearch.TextFieldOptions{Weight: 5.0, Sortable: true})).
		AddField(redisearch.NewTextFieldOptions(Active,
			redisearch.TextFieldOptions{Weight: 5.0, Sortable: true}))

	return sc
}

search_engine's People

Contributors

dependabot[bot] avatar jgsheppa avatar

Stargazers

 avatar  avatar

Watchers

 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.