Skip to content

Searchlight Query Language Home

Ted Spence edited this page Apr 13, 2021 · 2 revisions

The Searchlight query language is a simple, expressive query language that can be used to access a remote collection in API design. Because Searchlight is database neutral, the language can be implemented on multiple different data providers. It contains a rigorous grammar and is implemented as an abstract syntax tree parser that extracts key facts from a language.

  1. Using Searchlight in your project
When your REST API defines a collection of objects, such as "Companies", "Books", "Authors", or "Pets", you can then define a Searchlight query API for this collection. Inside your C# project, tag your object using the `SearchlightModel` and `SearchlightField` attributes:
  [SearchlightModel]
  public class MyModel
  {
      [SearchlightField]
      public string Name { get; set; }
      [SearchlightField]
      public string Description { get; set; }
      // This field won't be searchable
      public string NotASearchlightField { get; set; }
  }

Create a new query API in your project similar to the following:

[HttpGet]
public IntegrationModel[] Query(string Filter, string OrderBy, int Skip, int Take)
{
    var source = SearchlightDataSource.Create(typeof(IntegrationModel), ModelFieldMode.Strict);
    var queryData = source.Parse(Filter, null, OrderBy);
    var sql = source.RenderSQL(queryData);
    var results = mydb.Execute($"SELECT * FROM mytable WHERE ${sql.whereClause}", sql.parameters);
    return results;
}
Clone this wiki locally