Lucene Query Syntax
Parse standardized Lucene-style queries with support for terms, ranges, boolean operators, and wildcards.
Lucene-style query parsing with Elasticsearch and SQL support
Parse a query and inspect its structure:
using Foundatio.Parsers.LuceneQueries;
using Foundatio.Parsers.LuceneQueries.Visitors;
var parser = new LuceneQueryParser();
var result = parser.Parse("field:[1 TO 2]");
// Debug the AST structure
Console.WriteLine(DebugQueryVisitor.Run(result));
// Regenerate the query string
string query = GenerateQueryVisitor.Run(result);
// Output: "field:[1 TO 2]"Build Elasticsearch queries:
using Foundatio.Parsers.ElasticQueries;
var parser = new ElasticQueryParser(c => c
.UseMappings(client, "my-index")
.UseFieldMap(new Dictionary<string, string> {
{ "user", "data.user.name" }
}));
// Build a NEST QueryContainer
var query = await parser.BuildQueryAsync("user:john AND status:active");
// Build aggregations
var aggs = await parser.BuildAggregationsAsync("terms:(status min:created max:created)");
// Build sort
var sort = await parser.BuildSortAsync("-created +name");