-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
113 lines (93 loc) · 3.75 KB
/
Copy pathmain.go
File metadata and controls
113 lines (93 loc) · 3.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// Example: Full-text search with trigram index (CONTAINS queries)
package main
import (
"fmt"
"os"
"github.com/pkg/errors"
gin "github.com/amikos-tech/ami-gin"
)
func main() {
if err := run(); err != nil {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(1)
}
}
func run() error {
// Enable trigrams in config (enabled by default)
config := gin.DefaultConfig()
config.EnableTrigrams = true
builder, err := gin.NewBuilder(config, 4)
if err != nil {
return errors.Wrap(err, "create builder")
}
// Row group 0: Tech articles
builder.AddDocument(0, []byte(`{
"title": "Introduction to Machine Learning",
"content": "Machine learning is a subset of artificial intelligence that enables systems to learn from data."
}`))
builder.AddDocument(0, []byte(`{
"title": "Deep Learning Fundamentals",
"content": "Deep learning uses neural networks with multiple layers to process complex patterns."
}`))
// Row group 1: Database articles
builder.AddDocument(1, []byte(`{
"title": "PostgreSQL Performance Tuning",
"content": "Learn how to optimize your PostgreSQL database for better performance and scalability."
}`))
builder.AddDocument(1, []byte(`{
"title": "Introduction to NoSQL Databases",
"content": "NoSQL databases provide flexible schemas and horizontal scaling for modern applications."
}`))
// Row group 2: Web development
builder.AddDocument(2, []byte(`{
"title": "Building REST APIs with Go",
"content": "Learn how to build performant REST APIs using the Go programming language."
}`))
builder.AddDocument(2, []byte(`{
"title": "React Best Practices",
"content": "Best practices for building scalable React applications with modern patterns."
}`))
// Row group 3: DevOps
builder.AddDocument(3, []byte(`{
"title": "Kubernetes for Beginners",
"content": "Getting started with Kubernetes container orchestration and deployment."
}`))
builder.AddDocument(3, []byte(`{
"title": "CI/CD Pipeline Setup",
"content": "How to set up continuous integration and deployment pipelines for your team."
}`))
idx := builder.Finalize()
fmt.Println("=== Full-Text Search (CONTAINS) ===")
// Search for "learning"
fmt.Println("--- Query: title CONTAINS 'learning' ---")
result := idx.Evaluate([]gin.Predicate{gin.Contains("$.title", "learning")})
fmt.Printf("Row groups: %v\n\n", result.ToSlice())
// Search for "database"
fmt.Println("--- Query: content CONTAINS 'database' ---")
result = idx.Evaluate([]gin.Predicate{gin.Contains("$.content", "database")})
fmt.Printf("Row groups: %v\n\n", result.ToSlice())
// Search for "performance"
fmt.Println("--- Query: content CONTAINS 'performance' ---")
result = idx.Evaluate([]gin.Predicate{gin.Contains("$.content", "performance")})
fmt.Printf("Row groups: %v\n\n", result.ToSlice())
// Case insensitive (trigrams are lowercased)
fmt.Println("--- Query: title CONTAINS 'POSTGRESQL' (case insensitive) ---")
result = idx.Evaluate([]gin.Predicate{gin.Contains("$.title", "POSTGRESQL")})
fmt.Printf("Row groups: %v\n\n", result.ToSlice())
// Partial word match
fmt.Println("--- Query: content CONTAINS 'scala' (matches 'scalable', 'scaling') ---")
result = idx.Evaluate([]gin.Predicate{gin.Contains("$.content", "scala")})
fmt.Printf("Row groups: %v\n\n", result.ToSlice())
// Combined with equality
fmt.Println("--- Query: title CONTAINS 'Introduction' AND content CONTAINS 'learn' ---")
result = idx.Evaluate([]gin.Predicate{
gin.Contains("$.title", "Introduction"),
gin.Contains("$.content", "learn"),
})
fmt.Printf("Row groups: %v\n\n", result.ToSlice())
// No matches
fmt.Println("--- Query: content CONTAINS 'blockchain' (no matches) ---")
result = idx.Evaluate([]gin.Predicate{gin.Contains("$.content", "blockchain")})
fmt.Printf("Row groups: %v\n", result.ToSlice())
return nil
}