谷歌自定义搜索按日期范围排序条件无法工作

30
在Google自定义搜索中,我们在排序参数中传递日期范围条件。
http://www.example.com/search?cx=xxxxxxxxxxxxxxxxxxxxx:yyyyyy_yyyyy&q=google&sort=date:r:20160304:20160505.
https://developers.google.com/custom-search/docs/structured_search#restrict_to_range

但是根据日期范围条件,我们并没有得到预期的结果。在调试代码后,即使我们把排序参数传递给了搜索页面URL,它也没有传递到自定义搜索URL。

Google 自定义搜索 URL:

https://cse.google.com/cse?q=google&r=m&cx=xxxxxxxxxxxxxxxxxxxxx:yyyyyy_yyyyy&fexp=20606&client=google-coop&hl=en&type=0&oe=UTF-8&ie=UTF-8&format=p4&ad=p4&nocache=1081472127237880&num=0&output=uds_ads_only&source=gcsc&v=3&allwcallad=1&adext=as1%2Csr1%2Cctc1&bsl=10&u_his=50&u_tz=330&dt=1472127237882&u_w=1366&u_h=768&biw=1349&bih=233&psw=1349&psh=233&frm=0&uio=uv3st16sd13sv13lhsl1sr1cc1-&jsv=14100&rurl=http%3A%2F%2Fwww.example.com%2Fsearch%3Fcx%xxxxxxxxxxxxxxxxxxxx_yyyyy%26q%3Dgoogle%26sort%3Ddate%253Ar%253A20160304%253A20160505

但是,当我们尝试使用带有日期范围排序参数的直接API时,我们得到了预期的结果。

https://www.googleapis.com/customsearch/v1?q=google&cx=xxxxxxxxxxxxxxxxxxxxx:yyyyyy_yyyyy&sort=date%3Ar%3A20160701%3A20160724&key=<APIKEY>
我们正在使用Google自定义搜索V2实现。
谢谢, Anup

1
分享您的JavaScript代码(用于生成Google自定义搜索URL)将有助于社区仔细查找可能的修复方法。 - nyedidikeke
4个回答

2

尝试转义所有作为查询参数传递的特殊字符。例如

>  - &lt;
<  - &gt;
&  - &amp;
'  - &#039;
'' - &#034;

这些内容被编码成这样。您可以通过两种方式来转义它:
  1. 显式地指定 escapeXml = "**false**",默认值为 true。
  2. 如果您正在使用 JSP,则在 JSP 中添加 url <%@taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>,然后通过调用 ${fn:escapeXml(myString)} 来进行转义。

@Anup,你也可以尝试这个选项。 - Bhaskara Arani
@Anirudh - 我用了JS实现的GCE。 - Anup Khandelwal
1
@AnupKhandelwal - 我了解您已经使用了<gcse:search>,它是在命名空间gcse中的普通搜索标签。这些基本上是JavaScript命名空间。我认为您可以使用escapeXml来转义要转义特殊字符的标签。 - user5809385

1
似乎是您的URL参数编码出现了问题,可能是双重编码。

1
我认为在代码中,如果你对查询参数使用了任何转义字符,请尝试删除该检查。

0

确保你使用正确的格式。

https://www.googleapis.com/customsearch/v1?key=INSERT_YOUR_API_KEY&cx=017576662512468239146:omuauf_lfve&q=searchterm&sort=document-weight

谢谢!


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接