SonicSoft.Azure.Search.Query.Builder 1.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package SonicSoft.Azure.Search.Query.Builder --version 1.0.0
NuGet\Install-Package SonicSoft.Azure.Search.Query.Builder -Version 1.0.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="SonicSoft.Azure.Search.Query.Builder" Version="1.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add SonicSoft.Azure.Search.Query.Builder --version 1.0.0
#r "nuget: SonicSoft.Azure.Search.Query.Builder, 1.0.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install SonicSoft.Azure.Search.Query.Builder as a Cake Addin
#addin nuget:?package=SonicSoft.Azure.Search.Query.Builder&version=1.0.0

// Install SonicSoft.Azure.Search.Query.Builder as a Cake Tool
#tool nuget:?package=SonicSoft.Azure.Search.Query.Builder&version=1.0.0

Introduction

Azure Cognitive Search (formerly known as "Azure Search") is a search-as-a-service cloud solution that gives developers APIs and tools for adding a rich search experience over private, heterogeneous content in web, mobile, and enterprise applications.

SonicSoft azure search query builder design and develop to support any c# project that required to create filter queries for retrieve data from Azure Cognitive Search index. This query builder supports most of the query types. More details about supported query types and details of how to use this package included in the below sections.

For more details about Azure Cognitive Search please refer to this link: Azure Cognitive Search documentation

Supported Query types

  • Collection filter expressions using any.
  • Logical expressions that combine other Boolean expressions using the operators and, or, and not.
  • Comparison expressions, which compare fields or range variables to constant values using the operators eq, ne, gt, lt, ge, and le. -The Boolean literals true and false.
  • search.in, which compares a field or range variable with each value in a list of values.

Learn more about filters: OData $filter syntax in Azure Cognitive Search

Sample filter queries you can build using SonicSoft azure search query builder

HotelName eq 'Double Sanctuary Resort'
(HotelName eq 'Double Sanctuary Resort' or Category eq 'Resort and Spa')
search.in(Rooms/Type, 'Suite|Standard Room', '|')
Rooms/any(x: x/Type ne 'Standard Room' and x/BaseRate ge 100 and x/BaseRate le 200)
Rooms/any(x: x/Type ne 'Standard Room' and x/BaseRate ge 100 and search.in(x/Tags, 'jacuzzi tub|bathroom shower','|'))
((Rooms/any(x: x/Type ne 'Standard Room' and x/BaseRate ge 100 and search.in(x/Tags, 'jacuzzi tub|bathroom shower','|')) and search.in(Tags, 'view|laundry service', '|')) or Address/Country eq 'USA')

Installation

dotnet add package SonicSoft.Azure.Search.Query.Builder --version 1.0.0

Usage

Step 1
  • Create your azure search index property list by inheriting IAzureSearchProperties interface
  • Example
public class AzureSearchProperties : IAzureSearchProperties
{
	public const string HotelName = "HotelName";
	public const string StreetAddress = "StreetAddress";
	public const string Rooms = "Rooms";
	public const string RoomType = "Type";
	public const string RoomBaseRate = "BaseRate";

	public AzureSearchProperties()
	{
		SearchPropertyMaps = new List<SearchPropertyMap>()
		{
			CreateSearchPropertyMap(HotelName, HotelName, HotelName),
			CreateSearchPropertyMap(StreetAddress, StreetAddress, $"Address/{StreetAddress}"),
			CreateSearchPropertyMap(Rooms, Rooms, Rooms, true),
			CreateSearchPropertyMap(Rooms, RoomType, $"{RoomType}"),
			CreateSearchPropertyMap(Rooms, RoomBaseRate, $"{RoomBaseRate}"),
		};
	}

	public List<SearchPropertyMap> SearchPropertyMaps { get; set; }

	private SearchPropertyMap CreateSearchPropertyMap(string parent, string property, string azureSearchPropertyMap,
		bool isArray = false)
	{
		return new SearchPropertyMap
		{
			ParentPropertyName = parent,
			PropertyName = property,
			AzureSearchPropertyMap = azureSearchPropertyMap,
			IsArray = isArray
		};
	}
}
Step 2
  • Create or dependency inject SearchQueryBuilder object
  • Example
 var searchConfig = new SearchConfiguration("yyyy-MM-dd", "|");
Step 3
  • Create or dependency inject PropertyMapper
  • Example
 var propertyMaps = new AzureSearchProperties();
 var mapper = new PropertyMapper(propertyMaps);
Step 4
  • Finally Create or dependency inject you search builder
  • Example
var _queryBuilder = new SearchQueryBuilder(mapper, searchConfig);
Step 5
  • Create your SearchQueryParameters and Build your query using query builder
  • Example 1
var searchOptions = new List<SearchQueryParameter>
	{
		new SearchQueryParameter()
		{
			Name = TestAzureSearchProperties.HotelName,
			Value = "Double Sanctuary Resort",
			Parent = TestAzureSearchProperties.HotelName,
			ODataOperator = ODataOperators.Eq
		}
	};
            
var actualQuery = _queryBuilder.BuildQuery(null, new SearchQueryParameters(searchOptions));
  • Example 1 result
"HotelName eq 'Double Sanctuary Resort'"
  • Example 2
 var searchOptions = new List<SearchQueryParameter>
 {
    new SearchQueryParameter()
    {
		Parent = TestAzureSearchProperties.Rooms,
		Name = TestAzureSearchProperties.Rooms,
		Value = "",
		ODataOperator = ODataOperators.Any,
		SubQueryParameterQueryOperators = QueryOperators.And,
		SubQueryParameters = new List<SearchSubQueryParameter>
		{
			new SearchSubQueryParameter
			{
				AdditionalFilterParent = TestAzureSearchProperties.Rooms,
				AdditionalFilterName = TestAzureSearchProperties.RoomType,
				Value = "Standard Room",
				ODataOperator = ODataOperators.Ne
			},
			new SearchSubQueryParameter
			{
				AdditionalFilterParent = TestAzureSearchProperties.Rooms,
				AdditionalFilterName = TestAzureSearchProperties.RoomBaseRate,
				Value = 100,
				ODataOperator = ODataOperators.Ge
			},
			new SearchSubQueryParameter
			{
				AdditionalFilterParent = TestAzureSearchProperties.Rooms,
				AdditionalFilterName = TestAzureSearchProperties.Tags,
				Value = "jacuzzi tub|bathroom shower",
				ODataOperator = ODataOperators.SearchIn
			}
		}
	},
	new SearchQueryParameter()
	{
		Parent = TestAzureSearchProperties.Tags,
		Name = TestAzureSearchProperties.Tags,
		Value = "view|laundry service",
		ODataOperator = ODataOperators.SearchIn
	}
};

var countryQuery = new SearchQueryParameters(new List<SearchQueryParameter>
{
	new SearchQueryParameter
	{
		Parent = TestAzureSearchProperties.Country,
		Name = TestAzureSearchProperties.Country,
		Value = "USA",
		ODataOperator = ODataOperators.Eq
	}
});

var actualQuery =
	_queryBuilder.BuildQuery(QueryOperators.Or, new SearchQueryParameters(searchOptions, QueryOperators.And), countryQuery);
  • Example 2 result
"((Rooms/any(x: x/Type ne 'Standard Room' and x/BaseRate ge 100 and search.in(x/Tags, 'jacuzzi tub|bathroom shower','|')) and search.in(Tags, 'view|laundry service', '|')) or Address/Country eq 'USA')"

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

License

MIT License

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

This package has no dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2.0.0 1,920 10/4/2020
1.0.3 372 9/30/2020
1.0.2 403 6/2/2020
1.0.1 510 5/31/2020
1.0.0 408 5/31/2020