lucener

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: May 22, 2018 License: GPL-3.0 Imports: 2 Imported by: 0

README

go-lucener

go-lucener is a Go library for building Lucene compatable expressions for Cassandra driver

Documentation: GoDoc

Build Status: Build Status

go-lucener requires Go version 1.7 or greater.

Example

You can download the example user table and index from Stratio/cassandra-lucene-index repo

package main

import (
	"fmt"
	"log"

	"github.com/engin/go-lucener"
	"github.com/gocql/gocql"
)

func main() {
	// connect to the cluster
	cluster := gocql.NewCluster("127.0.0.1")
	cluster.Keyspace = "test"
	cluster.Consistency = gocql.Quorum
	session, err := cluster.CreateSession()
	if err != nil {
		log.Fatalf("couldn't connect to cassandra: %v", err)
	}
	defer session.Close()

	var name, gender, animal, food string
	var age int

	exp := lucener.NewExpr()
	exp.Filter(
		lucener.BooleanMust(
			lucener.Wildcard("name", "Ali*"), lucener.Wildcard("food", "tu*"),
		),
	)
	q := `SELECT name,gender,animal,age,food FROM users WHERE expr(users_index, ?)`

	// generated cql will be like:
	// SELECT * FROM users WHERE expr(users_index, '{
	// 	filter: {
	// 	   type: "boolean",
	// 	   must: [
	// 		  {type: "wildcard", field: "name", value: "*a"},
	// 		  {type: "wildcard", field: "food", value: "tu*"}
	// 	   ]
	// 	}
	//  }');
	fmt.Sprintln(exp.String())
	iter := session.Query(q, exp).Iter()
	for iter.Scan(&name, &gender, &animal, &age, &food) {
		fmt.Println("User:", name, gender, animal, age, food)
	}
	if err := iter.Close(); err != nil {
		log.Fatal(err)
	}
}

TODO

I'll try to add more test and examples

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BooleanQuery

type BooleanQuery struct {
	Type   string `json:"type"`
	Must   []Rule `json:"must,omitempty"`
	Should []Rule `json:"should,omitempty"`
	Not    []Rule `json:"not,omitempty"`
}

BooleanQuery struct

type Expr

type Expr struct {
	Q []Rule  `json:"query,omitempty"`
	F []Rule  `json:"filter,omitempty"`
	S []*Sort `json:"sort,omitempty"`
	R bool    `json:"refresh,omitempty"`
}

func NewExpr

func NewExpr() *Expr

NewExpr creates a new expression

func (*Expr) Filter

func (x *Expr) Filter(q ...Rule) *Expr

Filter add filters to expression

func (*Expr) MarshalCQL

func (x *Expr) MarshalCQL(_ gocql.TypeInfo) ([]byte, error)

MarshalCQL enables gocql library marshal into cql

func (*Expr) Query

func (x *Expr) Query(q ...Rule) *Expr

Query add queries to expression

func (*Expr) Refresh

func (x *Expr) Refresh(e bool) *Expr

Refresh enable / disable refreshing index

func (*Expr) Reset

func (x *Expr) Reset() *Expr

Reset all pre-configured options

func (*Expr) ResetFilter

func (x *Expr) ResetFilter() *Expr

ResetFilter removes pre-configured filter expression

func (*Expr) ResetQuery

func (x *Expr) ResetQuery() *Expr

ResetQuery removes pre-configured query expression

func (*Expr) ResetSort

func (x *Expr) ResetSort() *Expr

ResetSort removes pre-configured sort expression

func (*Expr) SortBy

func (x *Expr) SortBy(f string, r bool) *Expr

SortBy with field

func (*Expr) String

func (x *Expr) String() string

String for stringer interface

type Rule

type Rule interface {
}

func All

func All() Rule

All selects all indexed rows

func BooleanMust

func BooleanMust(rs ...Rule) Rule

BooleanMust query

func BooleanNot

func BooleanNot(rs ...Rule) Rule

BooleanNot query

func BooleanShould

func BooleanShould(rs ...Rule) Rule

BooleanShould query

func Contains

func Contains(f string, v interface{}) Rule

Contains query

func Match

func Match(f string, v interface{}) Rule

Match query

func Phrase

func Phrase(f string, v interface{}, slop int) Rule

Phrase query see: https://github.com/Stratio/cassandra-lucene-index/blob/branch-3.0.10/doc/documentation.rst#phrase-search slop (default = 0): number of words permitted between words.

func Prefix

func Prefix(f string, v interface{}) Rule

Prefix query

func RangeAll

func RangeAll(f string, lv, uv interface{}, incl, incu bool) Rule

RangeAll query

func RangeLower

func RangeLower(f string, v interface{}, inc bool) Rule

RangeLower query

func RangeUpper

func RangeUpper(f string, v interface{}, inc bool) Rule

RangeUpper query

type Sort

type Sort struct {
	Field   string `json:"field,omitempty"`
	Reverse bool   `json:"reverse,omitempty"`
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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