search-input-query-parser 0.1.2 → 0.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +137 -0
- package/package.json +1 -1
package/README.md
ADDED
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
# Search Input Query
|
|
2
|
+
|
|
3
|
+
## Parser
|
|
4
|
+
|
|
5
|
+
### Installation
|
|
6
|
+
|
|
7
|
+
```typescript
|
|
8
|
+
npm install search-input-query-parser
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
### Basic Usage
|
|
12
|
+
|
|
13
|
+
```typescript
|
|
14
|
+
import {
|
|
15
|
+
parseSearchInputQuery,
|
|
16
|
+
type FieldSchema
|
|
17
|
+
} from 'search-input-query-parser';
|
|
18
|
+
|
|
19
|
+
// Define your field schemas
|
|
20
|
+
const schemas: FieldSchema[] = [
|
|
21
|
+
{ name: 'title', type: 'string' },
|
|
22
|
+
{ name: 'price', type: 'number' },
|
|
23
|
+
{ name: 'date', type: 'date' }
|
|
24
|
+
];
|
|
25
|
+
|
|
26
|
+
// Parse a search query
|
|
27
|
+
const query = 'title:"winter boots" AND price:>100';
|
|
28
|
+
const result = parseSearchInputQuery(query, schemas);
|
|
29
|
+
|
|
30
|
+
if (result.type === 'SEARCH_QUERY') {
|
|
31
|
+
// Handle successful parse where the expression is in AST format
|
|
32
|
+
console.log(result.expression);
|
|
33
|
+
} else {
|
|
34
|
+
// Handle validation errors
|
|
35
|
+
console.log(result.errors);
|
|
36
|
+
}
|
|
37
|
+
```
|
|
38
|
+
## SQL Conversion
|
|
39
|
+
|
|
40
|
+
The parser can convert search queries into SQL WHERE clauses using three different search strategies:
|
|
41
|
+
|
|
42
|
+
1. ILIKE - Case-insensitive pattern matching
|
|
43
|
+
2. tsvector - PostgreSQL full-text search
|
|
44
|
+
3. ParadeDB - BM25-based full-text search
|
|
45
|
+
|
|
46
|
+
### Basic Usage
|
|
47
|
+
|
|
48
|
+
```typescript
|
|
49
|
+
import {
|
|
50
|
+
searchStringToIlikeSql,
|
|
51
|
+
searchStringToTsVectorSql,
|
|
52
|
+
searchStringToParadeDBSql,
|
|
53
|
+
type FieldSchema
|
|
54
|
+
} from 'search-input-query-parser';
|
|
55
|
+
|
|
56
|
+
// Define searchable columns and schemas
|
|
57
|
+
const searchableColumns = ['title', 'description'];
|
|
58
|
+
const schemas: FieldSchema[] = [
|
|
59
|
+
{ name: 'title', type: 'string' },
|
|
60
|
+
{ name: 'price', type: 'number' },
|
|
61
|
+
{ name: 'date', type: 'date' }
|
|
62
|
+
];
|
|
63
|
+
|
|
64
|
+
// Convert a search query to SQL
|
|
65
|
+
const query = 'winter boots AND price:>100';
|
|
66
|
+
|
|
67
|
+
// Using ILIKE (case-insensitive pattern matching)
|
|
68
|
+
const ilikeResult = searchStringToIlikeSql(query, searchableColumns, schemas);
|
|
69
|
+
// Result:
|
|
70
|
+
// {
|
|
71
|
+
// text: "((lower(title) LIKE lower($1) OR lower(description) LIKE lower($1)) AND price > $2)",
|
|
72
|
+
// values: ["%winter boots%", 100]
|
|
73
|
+
// }
|
|
74
|
+
|
|
75
|
+
// Using tsvector (PostgreSQL full-text search)
|
|
76
|
+
const tsvectorResult = searchStringToTsVectorSql(query, searchableColumns, schemas);
|
|
77
|
+
// Result:
|
|
78
|
+
// {
|
|
79
|
+
// text: "((to_tsvector('english', title) || to_tsvector('english', description)) @@ plainto_tsquery('english', $1) AND price > $2)",
|
|
80
|
+
// values: ["winter boots", 100]
|
|
81
|
+
// }
|
|
82
|
+
|
|
83
|
+
// Using ParadeDB (BM25 search)
|
|
84
|
+
const paradedbResult = searchStringToParadeDBSql(query, searchableColumns, schemas);
|
|
85
|
+
// Result:
|
|
86
|
+
// {
|
|
87
|
+
// text: "((title @@@ $1 OR description @@@ $1) AND price @@@ '>' || $2)",
|
|
88
|
+
// values: ['"winter boots"', 100]
|
|
89
|
+
// }
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
### Search Types Comparison
|
|
93
|
+
|
|
94
|
+
| Feature | ILIKE | tsvector | ParadeDB |
|
|
95
|
+
|---------|-------|----------|----------|
|
|
96
|
+
| Case Sensitivity | Case-insensitive | Case-insensitive | Case-sensitive |
|
|
97
|
+
| Pattern Matching | Simple wildcards | Language-aware tokens | BM25 ranking |
|
|
98
|
+
| Performance | Slower on large datasets | Fast with proper indexes | Fast with proper indexes |
|
|
99
|
+
| Setup Required | None | PostgreSQL extension | ParadeDB extension |
|
|
100
|
+
| Best For | Simple searches, small datasets | Advanced text search | Relevance-based search |
|
|
101
|
+
|
|
102
|
+
### Configuration Options
|
|
103
|
+
|
|
104
|
+
```typescript
|
|
105
|
+
// Common options for all search types
|
|
106
|
+
interface SearchQueryOptions {
|
|
107
|
+
language?: string; // Language for text analysis (default: 'english')
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
// Example with options
|
|
111
|
+
const result = searchStringToTsVectorSql(
|
|
112
|
+
query,
|
|
113
|
+
searchableColumns,
|
|
114
|
+
schemas,
|
|
115
|
+
{
|
|
116
|
+
language: 'spanish'
|
|
117
|
+
}
|
|
118
|
+
);
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
### Using with Raw SQL
|
|
122
|
+
|
|
123
|
+
The converters return objects with `text` (the WHERE clause) and `values` (the parameter values):
|
|
124
|
+
|
|
125
|
+
```typescript
|
|
126
|
+
import { searchStringToIlikeSql } from 'search-input-query-parser';
|
|
127
|
+
|
|
128
|
+
const base = 'SELECT * FROM products';
|
|
129
|
+
const { text, values } = searchStringToIlikeSql(query, searchableColumns, schemas);
|
|
130
|
+
const fullQuery = `${base} WHERE ${text}`;
|
|
131
|
+
|
|
132
|
+
// Using with node-postgres
|
|
133
|
+
const result = await client.query(fullQuery, values);
|
|
134
|
+
|
|
135
|
+
// Using with Prisma
|
|
136
|
+
const result = await prisma.$queryRaw`${Prisma.raw(base)} WHERE ${Prisma.raw(text)}`;
|
|
137
|
+
```
|