开发者

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&I开发者_运维百科mage.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?


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.


As @Sid mentioned in a comment, you can't use two size filters.

These are AND filters, meaning each one you apply makes the search stricter and stricter. Bing reads your request as wanting images that are both "small" and "medium". Of course, no images will meet both criteria, so you get an empty result set.

@gloscherrybomb thinks their answer works because bing is simply ignoring the last parameter of "Size:Small" or the even simpler "Small", and it's serving only medium results. Why? because it correctly reads the Image.Filters parameter as "Size:Medium", and then the rest of the text is just junk. The API docs clearly state that filters have to be joined with a plus sign, url-encoded, like so:

Image.Filters=Size:Medium%2BStyle:Photo

The "%2B" above is the url-encoded plus sign, which is how Bing asks you to concatenate multiple filters. But again, you can't specify two of the same type of image filter, like two size filters or two style filters.

There are two workarounds, neither of which I like. The first is to make two requests, possibly in parallel, one for each size you want, then join the results. But how do you join and sort two separate lists by relevance without some sort of score?

The easier way is to skip filtering altogether, and request more results than you need. Then filter the results by your own size requirements. If you're not paginating, this is probably the best option.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜