Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ require (
github.com/aws/aws-sdk-go-v2/service/dynamodb v1.53.5
github.com/aws/aws-sdk-go-v2/service/sns v1.39.10
github.com/gocql/gocql v1.7.0
github.com/grindlemire/go-lucene v0.0.26
github.com/scylladb/go-reflectx v1.0.1
github.com/scylladb/gocqlx/v3 v3.0.4
gopkg.in/yaml.v3 v3.0.1
Expand Down Expand Up @@ -41,7 +42,6 @@ require (
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/go-sql-driver/mysql v1.8.1 // indirect
github.com/golang-jwt/jwt/v5 v5.3.0 // indirect
github.com/grindlemire/go-lucene v0.0.26 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect
github.com/jackc/pgx/v5 v5.6.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 h1:EzJWgHo
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415/go.mod h1:GwrjFmJcFw6At/Gs6z4yjiIwzuJ1/+UwLxMQDVQXShQ=
github.com/xeipuuv/gojsonschema v1.2.0 h1:LhYJRs+L4fBtjZUfuSZIKGeVu0QRy8e5Xi7D17UxZ74=
github.com/xeipuuv/gojsonschema v1.2.0/go.mod h1:anYRn/JVcOK2ZgGU+IjEV4nwlhoK5sQluxsYJ78Id3Y=
go.yaml.in/yaml/v2 v2.4.3 h1:6gvOSjQoTB3vt1l+CU+tSyi/HOjfOjRLJ4YwYZGwRO0=
go.yaml.in/yaml/v2 v2.4.3/go.mod h1:zSxWcmIDjOzPXpjlTTbAsKokqkDNAVtZO0WOMiT90s8=
golang.org/x/crypto v0.45.0 h1:jMBrvKuj23MTlT0bQEOBcAE0mjg8mK9RXFhRH6nyF3Q=
golang.org/x/crypto v0.45.0/go.mod h1:XTGrrkGJve7CYK7J8PEww4aY7gM3qMCElcJQ8n8JdX4=
golang.org/x/net v0.47.0 h1:Mx+4dIFzqraBXUugkia1OOvlD6LemFo1ALMHjrXDOhY=
Expand Down
2 changes: 1 addition & 1 deletion storage/dynamodb.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ func (s *DynamoDBAdapter) Search(dest any, sortKey string, query string, limit i
// Parse Lucene query
destType := reflect.TypeOf(dest).Elem().Elem()
model := reflect.New(destType).Elem().Interface()
parser, _ := lucene.NewParserFromType(model)
parser, _ := lucene.NewParser(model)
whereClause, params, _ := parser.ParseToDynamoDBPartiQL(query)

// Build query
Expand Down
94 changes: 94 additions & 0 deletions storage/search/lucene/dynamodb_driver.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package lucene

import (
"fmt"
"strings"

"github.com/aws/aws-sdk-go-v2/service/dynamodb/types"
"github.com/grindlemire/go-lucene/pkg/driver"
"github.com/grindlemire/go-lucene/pkg/lucene/expr"
)

// DynamoDBPartiQLDriver converts Lucene queries to DynamoDB PartiQL.
type DynamoDBPartiQLDriver struct {
driver.Base
fields map[string]FieldInfo
}

func NewDynamoDBDriver(fields []FieldInfo) *DynamoDBPartiQLDriver {
fieldMap := make(map[string]FieldInfo)
for _, f := range fields {
fieldMap[f.Name] = f
}

fns := map[expr.Operator]driver.RenderFN{
expr.Literal: driver.Shared[expr.Literal],
expr.And: driver.Shared[expr.And],
expr.Or: driver.Shared[expr.Or],
expr.Not: driver.Shared[expr.Not],
expr.Equals: driver.Shared[expr.Equals],
expr.Range: driver.Shared[expr.Range],
expr.Must: driver.Shared[expr.Must],
expr.MustNot: driver.Shared[expr.MustNot],
expr.Wild: driver.Shared[expr.Wild],
expr.Regexp: driver.Shared[expr.Regexp],
expr.Like: dynamoDBLike, // Custom LIKE for DynamoDB functions
expr.Greater: driver.Shared[expr.Greater],
expr.GreaterEq: driver.Shared[expr.GreaterEq],
expr.Less: driver.Shared[expr.Less],
expr.LessEq: driver.Shared[expr.LessEq],
expr.In: driver.Shared[expr.In],
expr.List: driver.Shared[expr.List],
}

return &DynamoDBPartiQLDriver{
Base: driver.Base{
RenderFNs: fns,
},
fields: fieldMap,
}
}

// RenderPartiQL renders the expression to DynamoDB PartiQL with AttributeValue parameters.
func (d *DynamoDBPartiQLDriver) RenderPartiQL(e *expr.Expression) (string, []types.AttributeValue, error) {
// Use base rendering with ? placeholders
str, params, err := d.RenderParam(e)
if err != nil {
return "", nil, err
}

// Convert params to DynamoDB AttributeValues
attrValues := make([]types.AttributeValue, len(params))
for i, param := range params {
attrValues[i] = &types.AttributeValueMemberS{Value: fmt.Sprintf("%v", param)}
}

return str, attrValues, nil
}

// dynamoDBLike implements LIKE using DynamoDB's begins_with and contains functions.
func dynamoDBLike(left, right string) (string, error) {
// Remove quotes from right side to analyze pattern
pattern := strings.Trim(right, "'")

// Replace wildcards for analysis
hasPrefix := strings.HasPrefix(pattern, "%")
hasSuffix := strings.HasSuffix(pattern, "%")

if hasPrefix && hasSuffix {
// %value% -> contains(field, value)
value := strings.Trim(pattern, "%")
return fmt.Sprintf("contains(%s, '%s')", left, value), nil
} else if !hasPrefix && hasSuffix {
// value% -> begins_with(field, value)
value := strings.TrimSuffix(pattern, "%")
return fmt.Sprintf("begins_with(%s, '%s')", left, value), nil
} else if hasPrefix && !hasSuffix {
// %value -> contains(field, value) (DynamoDB doesn't have ends_with)
value := strings.TrimPrefix(pattern, "%")
return fmt.Sprintf("contains(%s, '%s')", left, value), nil
}

// Exact match
return fmt.Sprintf("%s = %s", left, right), nil
}
Loading