qs

package module
v0.0.0-...-70dc25e Latest Latest
Warning

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

Go to latest
Published: Feb 14, 2023 License: Apache-2.0 Imports: 8 Imported by: 0

README

GoDoc

Overview

qs implements a query language for the Bleve text indexer library.

The query syntax is aiming to be about the same as those used by Lucene and Elasticsearch.

The full syntax is described in syntax.md.

Usage

import "github.com/bcampbell/qs"

query, err := qs.Parse("grapefruit lemon orange lime")

Or, to create a parse which uses AND as the default operator:

p := qs.Parser{DefaultOp: qs.AND}
query,err := p.Parse("grapefruit lemon orange lime")

Quick Examples

Documents containing the term grapefruit:

grapefruit

There is support for boolean AND, OR and NOT:

grapefruit OR lemon
grapefruit AND lemon
grapefruit AND NOT lemon

Phrases are indicated with quotes:

"navel orange"

The default boolean operator is OR, so these two are equivalent when using the default parser:

grapefruit lemon
grapefruit OR lemon

Fields:

tags:citrus
headline:"How to Make the Perfect Negroni"

Grouping:

(lemon AND lime) OR (orange AND grapefruit)

Boosting term relevance:

fruit grapefruit^2

Inclusive ranges in square brackets, Exclusive ranges in curly brackets:

date:[2012-01-01 TO 2012-12-31]
count:{0 TO 100}

Wildcard markers * (any character sequence, 0 or more) and ? (any single character) eg to match foot, fort, fret etc:

f??t

eg to match ant, anteater, antidisestablishmentarianism etc:

ant*

Documentation

Overview

Package qs is a query language parser for Bleve (http://www.blevesearch.com).

Example:

import "github.com/bcampbell/qs"

query, err := qs.Parse("grapefruit lemon orange lime")

Or, to create a parse which uses AND as the default operator:

p := qs.Parser{DefaultOp: qs.AND}
query,err := p.Parse("grapefruit lemon orange lime")

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Parse

func Parse(q string) (query.Query, error)

Parse takes a query string and turns it into a bleve Query using the default Parser. Returned errors are type ParseError, which includes the position of the offending part of the input string.

Types

type OpType

type OpType int
const (
	OR  OpType = 0
	AND        = 1
)

type ParseError

type ParseError struct {
	// Pos is the character position where the error occured
	Pos int
	// Msg is a description of the error
	Msg string
}

ParseError is the error type returned by Parse()

func (ParseError) Error

func (pe ParseError) Error() string

type Parser

type Parser struct {

	// DefaultOp is used when no explict OR or AND is present
	// ie: foo bar => foo OR bar | foo AND bar
	// TODO: not sure AND/OR is the right terminology (but it's what others use)
	// Doesn't actually use OR or AND. AND is treated as an implied '+' prefix
	DefaultOp OpType

	// Loc is the location to use for parsing dates in range queries.
	// If nil, UTC is assumed.
	Loc *time.Location
	// contains filtered or unexported fields
}

func (*Parser) Parse

func (p *Parser) Parse(q string) (query.Query, error)

Parse takes a query string and turns it into a bleve Query.

Returned errors are type ParseError, which includes the position of the offending part of the input string.

BNF(ish) query syntax:

exprList = expr1*
expr1 = expr2 {"OR" expr2}
expr2 = expr3 {"AND" expr3}
expr3 = {"NOT"} expr4
expr4 = {("+"|"-")} expr5
expr5 = {field} part {boost}
part = lit {"~" number} | range | "(" exprList ")"
field = lit ":"
range = ("["|"}") {lit} "TO" {lit} ("]"|"}")
relational = ("<"|">"|"<="|">=") lit
boost = "^" number

(where lit is a string, quoted string or number)

Directories

Path Synopsis
bleve_queryparser - tool to parse and dump a query string to json for debugging.
bleve_queryparser - tool to parse and dump a query string to json for debugging.

Jump to

Keyboard shortcuts

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