lucene_to_sql

package module
v0.0.0-...-ad603c1 Latest Latest
Warning

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

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

README

lucene-to-sql

Introduction

This package can parse lucene query and convert to WHERE predicates in SQL, this package is pure go package.

Features

  • 1、This package can convert lucene query to WHERE predicates SQL.
  • 2、According to ES Mapping to convert Lucene query to SQL.

Usage

get package:

go get github.com/zhuliquan/lucene-to-sql@latest

example of lucene-to-sql:

import (
    "fmt"

    "github.com/zhuliquan/lucene-to-sql"
    esMapping "github.com/zhuliquan/es-mapping"
)

func getSchema(mapping *esMapping.Mapping) *esMapping.PropertyMapping {
    res, _ := esMapping.NewPropertyMapping(mapping)
    return res
}

func main() {
    cvt := lucene_to_sql.NewSqlConvertor(
        lucene_to_sql.WithSQLStyle(lucene_to_sql.SQLite),
        lucene_to_sql.WithSchema(getSchema(&esMapping.Mapping{
            Properties: map[string]*esMapping.Property{
                "field1": {
                    Type:   esMapping.DATE_FIELD_TYPE,
                    Format: "yyyy-MM-dd'T'HH:mm:ss",
                },
                "field2": {
                    Type: esMapping.KEYWORD_FIELD_TYPE,
                },
                "field3": {
                    Type: esMapping.TEXT_FIELD_TYPE,
                },
            },
        })),
    )
    query := `field1:["2008-01-01T09:09:08" TO * ] AND field2:foo OR field3:bar`
    got, err := cvt.LuceneToSql(query)
    if err != nil {
        panic(err)
    } else {
        // field1 >= '2008-01-01 09:09:08' AND field2 = 'foo' OR field3 like '%bar%'
        fmt.Println(got)
    }
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func WithSQLStyle

func WithSQLStyle(sqlStyle SQL_STYLE) func(s *SqlConvertor)

func WithSchema

func WithSchema(mappings *esMapping.PropertyMapping) func(s *SqlConvertor)

func WithTokenizer

func WithTokenizer(field string, tokenizer Tokenizer) func(s *SqlConvertor)

Types

type SQL

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

func NewSQL

func NewSQL() *SQL

func (*SQL) AddAndClause

func (s *SQL) AddAndClause(clause string, andSymbol, notSymbol bool)

func (*SQL) AddORClause

func (s *SQL) AddORClause(clause string, orSymbol bool)

func (*SQL) AddSubClause

func (s *SQL) AddSubClause(clause string, notSymbol bool)

func (*SQL) String

func (s *SQL) String() string

type SQL_STYLE

type SQL_STYLE int32
const (
	Standard SQL_STYLE = iota // SQL99
	SQLite
	MySQL
	Oracle
	PostgreSQL
	ClickHouse
)

func (SQL_STYLE) String

func (s SQL_STYLE) String() string

type SqlConvertor

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

func NewSqlConvertor

func NewSqlConvertor(options ...func(s *SqlConvertor)) *SqlConvertor

func (*SqlConvertor) LuceneToSql

func (c *SqlConvertor) LuceneToSql(query string) (string, error)

type Tokenizer

type Tokenizer interface {
	Split(string) []string
}

Jump to

Keyboard shortcuts

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