|
1 | 1 | # Foundatio.LuceneQuery |
2 | 2 |
|
3 | | -A high-performance Lucene query string parser for .NET that converts query strings into an Abstract Syntax Tree (AST). Supports query transformation via visitors and includes Entity Framework Core integration for generating LINQ expressions. |
| 3 | +A library for adding dynamic Lucene-style query capabilities to your .NET applications. Enable your users to write powerful search queries using familiar Lucene syntax, with support for Entity Framework Core and (coming soon) Elasticsearch. |
| 4 | + |
| 5 | +This project is a modern replacement for [Foundatio.Parsers](https://github.com/FoundatioFx/Foundatio.Parsers). |
4 | 6 |
|
5 | 7 | ## Features |
6 | 8 |
|
| 9 | +- **Dynamic User Queries** - Let users write powerful search queries using Lucene syntax |
| 10 | +- **Entity Framework Integration** - Convert Lucene queries directly to LINQ expressions for EF Core |
| 11 | +- **Elasticsearch Support** - (Coming soon) Generate Elasticsearch queries from the same syntax |
7 | 12 | - **Full Lucene Query Syntax** - Terms, phrases, fields, ranges, boolean operators, wildcards, regex, and more |
8 | | -- **Elasticsearch Extensions** - Date math expressions (`now-1d`, `2024-01-01||+1M/d`), `_exists_`, `_missing_` |
| 13 | +- **Date Math Expressions** - Support for Elasticsearch-style date math (`now-1d`, `2024-01-01||+1M/d`) |
9 | 14 | - **Visitor Pattern** - Transform, validate, or analyze queries with composable visitors |
| 15 | +- **Field Aliasing** - Map user-friendly field names to your actual data model |
| 16 | +- **Query Validation** - Restrict allowed fields, operators, and patterns |
10 | 17 | - **Round-Trip Capable** - Parse queries to AST and convert back to query strings |
11 | | -- **Entity Framework Integration** - Convert Lucene queries directly to LINQ expressions |
12 | 18 | - **Error Recovery** - Resilient parser returns partial AST with detailed error information |
13 | 19 |
|
14 | 20 | ## Installation |
@@ -94,20 +100,53 @@ if (!validationResult.IsValid) |
94 | 100 |
|
95 | 101 | ### Entity Framework Integration |
96 | 102 |
|
| 103 | +Enable dynamic, user-driven queries in your API endpoints: |
| 104 | + |
97 | 105 | ```csharp |
98 | 106 | using Foundatio.LuceneQuery.EntityFramework; |
99 | 107 |
|
| 108 | +// In your API controller or service |
| 109 | +[HttpGet("employees")] |
| 110 | +public async Task<IActionResult> SearchEmployees([FromQuery] string query) |
| 111 | +{ |
| 112 | + var parser = new EntityFrameworkQueryParser(); |
| 113 | + |
| 114 | + // User provides: "name:john AND salary:[50000 TO *] AND department:engineering" |
| 115 | + Expression<Func<Employee, bool>> filter = parser.BuildFilter<Employee>(query); |
| 116 | + |
| 117 | + var results = await _context.Employees |
| 118 | + .Where(filter) |
| 119 | + .ToListAsync(); |
| 120 | + |
| 121 | + return Ok(results); |
| 122 | +} |
| 123 | +``` |
| 124 | + |
| 125 | +With field aliasing to protect your data model: |
| 126 | + |
| 127 | +```csharp |
100 | 128 | var parser = new EntityFrameworkQueryParser(); |
101 | 129 |
|
102 | | -// Build a filter expression from a Lucene query |
103 | | -Expression<Func<Employee, bool>> filter = parser.BuildFilter<Employee>( |
104 | | - "name:john AND salary:[50000 TO *] AND isActive:true" |
105 | | -); |
| 130 | +// Map user-friendly names to actual entity properties |
| 131 | +var fieldMap = new FieldMap |
| 132 | +{ |
| 133 | + { "name", "FullName" }, |
| 134 | + { "dept", "Department.Name" }, |
| 135 | + { "hired", "HireDate" } |
| 136 | +}; |
106 | 137 |
|
107 | | -// Use with EF Core |
108 | | -var results = await context.Employees.Where(filter).ToListAsync(); |
| 138 | +// User query: "name:john AND dept:engineering AND hired:[2020-01-01 TO *]" |
| 139 | +Expression<Func<Employee, bool>> filter = parser.BuildFilter<Employee>(userQuery, fieldMap); |
109 | 140 | ``` |
110 | 141 |
|
| 142 | +## Use Cases |
| 143 | + |
| 144 | +- **Search APIs** - Let users filter data with powerful query syntax |
| 145 | +- **Admin Dashboards** - Enable complex filtering without custom UI for each field |
| 146 | +- **Reporting** - Allow dynamic report criteria using familiar search syntax |
| 147 | +- **Data Export** - Let users specify exactly what data they need |
| 148 | +- **Audit/Log Search** - Search through logs with date ranges, terms, and boolean logic |
| 149 | + |
111 | 150 | ## Supported Query Syntax |
112 | 151 |
|
113 | 152 | | Syntax | Example | Description | |
|
0 commit comments