MCPcopy Create free account
hub / github.com/pydio/cells / BleveQueryToJSONPath

Function BleveQueryToJSONPath

common/storage/boltdb/query.go:36–85  ·  view source on GitHub ↗

BleveQueryToJSONPath transforms a Blevesearch query string to a JSONPath query

(queryString string, rootSelector string, insensitive bool, fieldTransformer func(string) string, computeLength bool)

Source from the content-addressed store, hash-verified

34
35// BleveQueryToJSONPath transforms a Blevesearch query string to a JSONPath query
36func BleveQueryToJSONPath(queryString string, rootSelector string, insensitive bool, fieldTransformer func(string) string, computeLength bool) (gval.Evaluable, string, error) {
37 q, e := bleve.NewQueryStringQuery(queryString).Parse()
38 if e != nil {
39 return nil, "", e
40 }
41 var parts []string
42 var out string
43 if bQ, o := q.(*query.BooleanQuery); o {
44 if cj := bQ.Must; cj != nil {
45 ff := uniqueQueryToFilters(cj, fieldTransformer, insensitive, false)
46 parts = append(parts, ff...)
47 }
48 if dj := bQ.Should; dj != nil {
49 ff := uniqueQueryToFilters(dj, fieldTransformer, insensitive, false)
50 parts = append(parts, ff...)
51 }
52 if mn := bQ.MustNot; mn != nil {
53 ff := uniqueQueryToFilters(mn, fieldTransformer, insensitive, true)
54 parts = append(parts, ff...)
55 }
56 }
57 out = fmt.Sprintf("%s[?(%s)]", rootSelector, strings.Join(parts, " && "))
58 if computeLength {
59 out = "length(" + out + ")"
60 }
61
62 // Define a custom function for calculating length
63 lengthFunc := gval.Function("length", func(arguments ...interface{}) (interface{}, error) {
64 if len(arguments) != 1 {
65 return nil, errors.New("length function expects exactly one argument")
66 }
67 switch v := arguments[0].(type) {
68 case []interface{}:
69 return len(v), nil
70 case string:
71 return len(v), nil
72 default:
73 return nil, errors.New("unsupported type for length function")
74 }
75 })
76
77 // Create a Gval expression language with custom operators
78 language := gval.NewLanguage(
79 jsonpath.Language(),
80 gval.Full(),
81 lengthFunc,
82 )
83 jp, er := language.NewEvaluable(out)
84 return jp, out, er
85}
86
87func line(fieldName, comparator string, value any, not ...bool) string {
88 var zeline string

Callers 4

ActivitiesForMethod · 0.92
TestBleveQueryToJSONPathFunction · 0.85

Calls 4

uniqueQueryToFiltersFunction · 0.70
ParseMethod · 0.65
NewMethod · 0.65
JoinMethod · 0.45

Tested by 3

TestBleveQueryToJSONPathFunction · 0.68

Used in the wild real call sites across dependent graphs

searching dependent graphs…