neolei neolei - 1 month ago 9
C# Question

Bing API image search filter

I'm using Bing Sharp 2.0. I'd like to do image search by filters, so I follow the codes in sample:

SearchRequest searchRequest = new SearchRequest { AppId = appId, Query = query, Market = "en-US" };
ImageRequest imageRequest = new ImageRequest();
imageRequest.Filters = buildFilterArray();
imageRequest.Count = imageCount;
imageRequest.Offset = (imageCount * pageNumber);
ImageResponse response = API.Image(searchRequest, imageRequest);


And this is buildFilterArray:

private string[] BuildFilterArray()
{
List<string> filters = new List<string>();
filters.Add("Size:Small");
filters.Add("Size:Medium");
return filters.ToArray();
}


But the result comes back nothing (response.Total == 0).

I found the reason is because I specified 2 filters in buildFilterArray(), as long as I remove one (no matter which one), my search comes back with expected results.

This also proved by issuing http get request directly from IE, This returns results:

http://api.search.live.net/xml.aspx?AppId=0B409D9BA6759BCD3CC3D8B9A6A90F2907274BC7&Query=tektronix&Version=2.0&Market=en-US&Sources=Image&Image.Count=10&Image.Offset=0&Image.Filters=Size:Small


While this not:

http://api.search.live.net/xml.aspx?AppId=0B409D9BA6759BCD3CC3D8B9A6A90F2907274BC7&Query=tektronix&Version=2.0&Market=en-US&Sources=Image&Image.Count=10&Image.Offset=0&Image.Filters=Size:Medium&Image.Filters=Size:Small


Am I doing anything wrong? How can I put more than one filters into search request?

Answer Source

This is actually quite a simple fix...

You will see in your second one "Image.Filters=Size:Medium&Image.Filters=Size:Small" That the image filters bit is repeated. Change it to:

"Image.Filters=Size:Medium&Size:Small"

or even:

"Image.Filters=Size:Medium&Small"

and both return results.