在Azure搜索中包含/数组中的(预览) [英] Contains / in array in Azure Search (Preview)

查看:40
本文介绍了在Azure搜索中包含/数组中的(预览)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用新的(预览版)Azure搜索SDK(此处的信息).我正在使用oData表达式语法使用$filter参数来建立搜索查询,并且希望能够将ID数组中的内容馈送到端点. brandId字段在我的Azure搜索索引中,并且是可搜索的,可过滤的,可排序的和可表的.理想情况下,我希望能够执行以下操作:

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"]

()

我不知道要使用什么语法在$filter查询中包括集合文字语法.因此,我目前仅将逻辑OR的负载字符串串联在一起以形成查询,而不是理想的情况:

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

}

最终的(URL编码)URI如下所示:

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

希望有人能启发我吗?

推荐答案

Azure Search不支持集合文字或参数化过滤器,但在这种情况下确实提供了专用功能:search.in. search.in函数="nofollow noreferrer">此处.

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.

这是在特定情况下如何使用search.in的示例:

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

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

这篇关于在Azure搜索中包含/数组中的(预览)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆