search

package module
v0.2.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 4, 2024 License: MIT Imports: 7 Imported by: 0

README

In-memory fuzzy search engine for Golang.

Go Reference Test

Installation

go get github.com/radulucut/search

Usage

package main

import (
	"fmt"

	"github.com/radulucut/search"
)

type Book struct {
	Id   int64
	Text string
}

var items = []Book{
	{Id: 1, Text: "Ultima noapte de dragoste, întâia noapte de război de Camil Petrescu"},
	{Id: 2, Text: "Pădurea spânzuraţilor de Liviu Rebreanu"},
	{Id: 3, Text: "Moromeții I de Marin Preda"},
	{Id: 4, Text: "Maitreyi de Mircea Eliade"},
	{Id: 5, Text: "Enigma Otiliei de George Călinescu"},
	{Id: 6, Text: "La țigănci de Mircea Eliade"},
	{Id: 7, Text: "Moara cu noroc de Ioan Slavici"},
	{Id: 8, Text: "Amintiri din copilărie de Ion Creangă"},
	{Id: 9, Text: "Patul lui Procust de Camil Petrescu"},
	{Id: 10, Text: "Elevul Dima dintr-a VII-A de Mihail Drumeș"},
	{Id: 11, Text: "Întoarcerea din rai de Mircea Eliade"},
	{Id: 12, Text: "La hanul lui Mânjoală de Ion Luca Caragiale"},
	{Id: 13, Text: "O scrisoare pierdută de Ion Luca Caragiale"},
	{Id: 14, Text: "Ion de Liviu Rebreanu"},
	{Id: 15, Text: "Baltagul de Mihail Sadoveanu"},
}

func main() {
	// Create a new search engine
	engine := search.NewEngine()
	engine.SetTolerance(2)

	// Add items to the search engine
	for _, item := range items {
		engine.SetItem(item.Id, item.Text)
	}

	// Search for a book
	results := engine.Search(search.SearchOptions{ Query: "Eliade", Limit: 5 })

	// Print the results
	// search.SearchResult{ Items: { 11, 6, 4 }, Total: 3, Pages: 1 }
	fmt.Println(results)
}

TODO

  • Add stemming support
  • Add tf-idf support

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Engine

type Engine struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

func NewEngine

func NewEngine() *Engine

func (*Engine) DeleteItem

func (e *Engine) DeleteItem(id int64)

Remove an item from the search engine.

func (*Engine) Search

func (e *Engine) Search(opts SearchOptions) SearchResult

Search finds the most similar items to the given query. limit is the maximum number of items to return. ignore is a list of item ids to ignore.

func (*Engine) SetItem

func (e *Engine) SetItem(id int64, text string)

Add a new item to the search engine.

func (*Engine) SetTolerance

func (e *Engine) SetTolerance(tolerance int)

Set the maximum number of typos per word allowed. The default value is 1.

type SearchOptions added in v0.2.2

type SearchOptions struct {
	Query  string
	Limit  int
	Offset int
	Ignore []int64
}

type SearchResult added in v0.2.2

type SearchResult struct {
	Items []int64
	Total int
	Pages int
}

type TokenizeFunc

type TokenizeFunc func(input string) [][]rune

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL