search

package module
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Jul 17, 2022 License: MIT Imports: 38 Imported by: 0

README

main release

search is a project to provide a simple search engine, built on top of the bleve Go library.

Suitable for a small projects, like blogs.

It concist of two parts:

  • indexer Go struct, which can be used to create index,
  • server Go app, which reads index and serves search requests.

Indexer

Initialize an indexer with NewIndexer:

import "github.com/chuhlomin/search"

indexer, err := search.NewIndexer(searchIndexPath, buildPathPrefix)

Register types to index with RegisterType:

err := indexer.RegisterType(someStruct{}, "en")

Struct may implement bleve's mapping.Classifier interface to specify a type name. Otherwise, struct name will be used as type name.

type Classifier interface {
	Type() string
}

Also, structs may implement Lanuguage interface to specify language for all text fields in the struct. Otherwise, the default language will be used (passed to RegisterType).

type Language interface {
	Language() string
}

Use struct tags to define a special behaviour:

type someStruct struct {
	SomeField string `indexer:"text"`
	Date      string `indexer:"date"`
	Password  string `indexer:"no_store"`
	Blob      string `indexer:"no_index"`
}

Index documents with Index:

err := indexer.Index("id", someStruct{SomeField: "needle & needle"})

Don't forget to call Close when you're done:

err := indexer.Close()

Server

You may run the server container with index mounted:

version: "3.9"

services:
  search:
    image: chuhlomin/search:v0.1.0
    ports:
    - 127.0.0.1:8081:80
    environment:
    - INDEX_PATH=/index
    - BIND=0.0.0.0:80
    volumes:
    - ./index:/index

Then send a search query:

curl "http://127.0.0.1:8081/?q=needle"

Result will be a JSON array of documents:

[
    {
        "id": "id",
        "score": 1.0,
        "fragments": {
            "field": "SomeField",
            "locations": {
                "needle": [
                    {
                        "start": 0,
                        "end": 6
                    },
                    {
                        "start": 10,
                        "end": 16
                    }
                ]
            }
        },
        "document": {}
    }
]

You may also specify which document fields to return:

## Search Local
curl -X "POST" "http://127.0.0.1:8081/?q=needle" \
     -d $'{
  "SomeField": true
}
'
[
    {
        "id": "id",
        "score": 1.0,
        "fragments": {
            "field": "SomeField",
            "locations": {
                "needle": [
                    {
                        "start": 0,
                        "end": 6
                    },
                    {
                        "start": 10,
                        "end": 16
                    }
                ]
            }
        },
        "document": {
            "SomeField": "needle & needle"
        }
    }
]

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Indexer

type Indexer struct {
	// contains filtered or unexported fields
}

func NewIndexer

func NewIndexer(indexPath, buildDir string) (*Indexer, error)

func (*Indexer) Close

func (i *Indexer) Close() error

func (*Indexer) Index

func (i *Indexer) Index(id string, data interface{}) error

func (*Indexer) RegisterType

func (i *Indexer) RegisterType(structType interface{}, lang string) error

type Language

type Language interface {
	Language() string
}

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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