Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Contains / in array in Azure Search (Preview)

I am using the new (preview) Azure Search SDK (information here). I am using the oData expression syntax to build up my search query using the $filter parameter and I would like to be able to feed across an array of ids to the endpoint. The brandId field is in my Azure Search Index and it is searchable, filterable, sortable, and facetable. Ideally I would like to be able to do something along the lines of:

http://host/service/Products?brands=["1","2"]

(See specification here under the section entitled "Complex and Collection Literals")

I can't figure out what syntax to use to include the collection literal syntax inside of the $filter query. So I'm currently just string concatenating loads of logical ORs together to form the query instead which is less than ideal:

var sp = new SearchParameters();

string filter = "$filter=";

if(brands.Length > 0)
{   
    int counter = 0;

    foreach(string brand in brands)
    {
        if(counter == 0)
        {
            filter += "(brandId eq '" + brand + "'";
        }   
        else if(counter == (brands.Count() - 1))
        {
            filter += " or brandId eq '" + brand + "')";
        }
        else
        {
            filter += " or brandId eq '" + brand + "'";
        }
        counter++;
    }
}

sp.Filter = filter;

DocumentSearchResult response = indexClient.Documents.Search(theSearchQuery, sp);

foreach(SearchResult result in response.Results)
{
    // do stuff to the results

}

The final (url-encoded) URI comes out as:

https://[mysearchservicename].search.windows.net/indexes/products/docs?api-version=2015-02-28&$filter=language%20eq%20%27us%27%20and%20%28brandId%20eq%20%276%27%20or%20brandId%20eq%20%278%27%20or%20brandId%20eq%20%279%27%20or%20brandId%20eq%20%2710%27%20or%20brandId%20eq%20%2711%27%20or%20brandId%20eq%20%2713%27%20or%20brandId%20eq%20%2722%27%29

Was hoping someone could enlighten me?

like image 770
adaam Avatar asked Feb 02 '16 12:02

adaam


People also ask

When would you use a filter in an Azure Search query?

When to use a filter. Filters are foundational to several search experiences, including "find near me" geospatial search, faceted navigation, and security filters that show only those documents a user is allowed to see. If you implement any one of these experiences, a filter is required.

Which of the following search operators in Azure searches need to be escaped when passing them through the rest API select two answers?

The NOT operator - only needs to be escaped if it's the first character after a whitespace.

Is Azure Search the same as Azure Cognitive Search?

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

What is search score in Azure Cognitive Search?

The search score is computed based on statistical properties of the string input and the query itself. Azure Cognitive Search finds documents that match on search terms (some or all, depending on searchMode), favoring documents that contain many instances of the search term.


1 Answers

Azure Search doesn't support collection literals or parameterized filters, but it does offer a specialized function for this case: search.in. The subset of OData supported by Azure Search, as well as the search.in function, are documented here.

Here's an example of how you would use search.in for this specific case:

$filter=search.in(brandId, '1,2')

like image 97
Bruce Johnston Avatar answered Oct 16 '22 19:10

Bruce Johnston