tree-search-language

module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Aug 24, 2018 License: Apache-2.0

README

Tree Search Language (TSL)

Tree Search Language (TSL) is a wonderful search langauge, With similar grammar to SQL's where part. implementing query based search engines was never that easy.

Go Report Card Build Status GoDoc License

TSL

[ awesome image by gophers... ]

The TSL language grammar is similar to SQL syntax, for example:

name like '%joe%' and (city = 'paris' or city = 'milan')
ParseTSL

The TSL package include the ParseTSL method for parsing TSL into a search tree:

tree, err := tsl.ParseTSL("name in ('joe', 'jane') and age not between 0 and 10")
SquirrelWalk

The TSL package include a helper SquirrelWalk method that adds serch context to Squirrel SelectBuilder:

sql, args, err := sq.Select("name, city, state").
    Where(tsl.SquirrelWalk(tree)).
    From("users").
    ToSql()

TSL is generated with Antlr4 tool, the antlr4 grammar file is TSL.g4, SQL generation is done using Squirrel SQL builder.

Cli tools

tls_parser, tls_mongo and tsl_to_sql are example cli tools showcasing the TSL language and TSL golang package.

tls_parser
$ ./tsl_parser -h
Usage of ./tls_parser:
  -i string
    	the tsl string to parse (e.g. "animal = 'kitty'")
  -o string
    	output format [json/yaml/prettyjson] (default "json")
$ ./tsl_parser -i "(name = 'joe' or name = 'jane') and city = 'rome'" -o yaml
func: $and
left:
  func: $eq
  left: city
  right: rome
right:
  func: $or
  left:
    func: $eq
    left: name
    right: jane
  right:
    func: $eq
    left: name
    right: joe
tsl_mongo

tsl_mongo include an example bsonWalk method, for building a mongo bson filter.

$ ./tsl_mongo -i "title is not null"
{"_id":{"$oid":"5b8083aadda18596ef42a941"},"title":"Book","author":"Joe","spec":{"pages":{"$numberLong":"100"},"raiting":{"$numberLong":"4"}}}
{"_id":{"$oid":"5b8083aadda18596ef42a942"},"title":"Other Book","author":"Jane","spec":{"pages":{"$numberLong":"200"},"raiting":{"$numberLong":"3"}}}
{"_id":{"$oid":"5b8083aadda18596ef42a943"},"title":"Some Other Book","author":"Jane","spec":{"pages":{"$numberLong":"50"},"raiting":{"$numberLong":"5"}}}
{"_id":{"$oid":"5b8083aadda18596ef42a944"},"title":"Some Other Book","author":"Jane","spec":{"pages":{"$numberLong":"50"}}}
$ ./tsl_mongo -i "title ~= 'Other' and spec.raiting > 4"
{"_id":{"$oid":"5b8083c9e0d411d1f2fbcfa4"},"title":"Some Other Book","author":"Jane","spec":{"pages":{"$numberLong":"50"},"raiting":{"$numberLong":"5"}}}
tsl_to_sql
$ ./tsl_to_sql -h
Usage of ./tsl_to_sql:
  -i string
    	the tsl string to parse (e.g. "animal = 'kitty'")
  -o string
    	output format [mysql/pgsql] (default "mysql")
  -t string
    	the table name to use for SQL generation (default "<table-name>")

$ ./tsl_to_sql -i "name != 'eli''s' or city like '%rome%' and state not between 'italy' and 'france'" -t users -o pgsql
sql:  SELECT * FROM users WHERE (name <> $1 OR (city LIKE $2 AND state NOT BETWEEN $3 AND $4))
args: [eli's %rome% italy france]

Language
Keywords
and or not is null like between in
Operators
= <= >= != ~= ~! <>
Examples
name = 'Joe'
city in ('paris', 'rome', 'milan') or sate = 'spain'
(name = 'joe' or city = 'rome') and state = 'italy'
Code snippets
import
import (
  ...
	"github.com/yaacov/tsl/pkg/tsl"
  ...
)
ParseTSL
  ...
  // Set a TSL input string
  input = "name='joe' or name='jane'"

  // Parse input string into a TSL tree
  tree, err := tsl.ParseTSL(input)
  ...
SquirrelWalk
  import (
    ...
    sq "github.com/Masterminds/squirrel"
    "github.com/yaacov/tsl/pkg/tsl"
    ...
  )
  ...
  // Convert TSL tree into SQL string using squirrel sql builder.
  sql, args, err := sq.Select("name, city, state").
    Where(tsl.SquirrelWalk(tree)).
    From("users").
    ToSql()
  ...

Directories

Path Synopsis
cmd
tsl_mongo command
Package main Package main Package main
Package main Package main Package main
tsl_parser command
Package main
Package main
tsl_to_sql command
Package main
Package main
pkg
tsl
Package tsl describe and parse the Tree Search Language (TSL), TSL is a wonderful search language, With similar grammar to SQL's where part.
Package tsl describe and parse the Tree Search Language (TSL), TSL is a wonderful search language, With similar grammar to SQL's where part.

Jump to

Keyboard shortcuts

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