Elastic raw querying
When using the Elastic search provider, Ucommerce supports raw queries to the provider in two forms: raw JSON and NEST queries.
This is for special requirements, when you need functionality not covered by the Ucommerce search APIs.
Examples
Raw JSON string
You can pass a JSON string to the FindRaw()
method. This query is then forwarded to the provider followed by the default skip, take and sort parameters.
var index = ObjectFactory.Instance.Resolve<IIndex<Product>>(); var skuQuery = @"{ ""term"" : { ""Sku"" : {""value"":""shirt""} } }"; var productsFromQuery = index.FindRaw(skuQuery).ToList();
For further information on what is supported by the provider, please visit the Elastic documentation.
Nest query
If you are looking to use DSL to query instead, you can do this by passing a Nest.SearchDescriptor<T>
instance to the FindRaw()
method.
var query = new SearchDescriptor<CustomProduct>() .Query(q => q.Match(m => m.Field(f => f.Sku))); var products = index.FindRaw(query).ToList();
For further information on querying using DLS, please visit the Elastic documentation.
Custom model queries
When retrieving data from a raw query, it is possible to specify a custom model by providing it as a type argument to the FindRaw<T>()
method. This is useful when, for example, you want a subset of the fields returned only from the index for a particular query.
This works with both syntaxes mentioned above.
public class CustomProduct: Model { public string Name { get; set; } public int Id { get; set; } public string Sku { get; set; } }
var customProductQuery = new SearchDescriptor<CustomProduct>() .Query(q => q.Match(m => m.Field(f => f.Id) .Query("23") ) ); var customProductsQuery = index.FindRaw<CustomProduct>(customProductQuery).ToList();