-2

我必须实现一个包含多个 google 支持的搜索表单的页面。我们从 Google 获得了 CSE 的许可证,情况是这样的:

  • 我有一个搜索表单,它出现在每个页面的顶部,它执行简单的搜索并将结果显示在单独的页面中。这行得通。

  • 我有一个特定的页面,此外,它还显示了另外两种搜索表单:一个应该按类别过滤文章,另一个应该按类别过滤文章并将结果限制在某个月份。为此,我在每篇文章中添加了一个带有发布日期的元键。

不过,我在文档中有点迷失了:如果我添加

<gcse:searchbox-only resultsUrl="/[site]/stat/search/google_search_results.html"></gcse:searchbox-only></div>

到页面,我无法过滤结果。如果我开始使用 CustomSearchObject,我看不到在不同页面上显示结果的选项。

对于基于类别的过滤,我尝试附加

more:pagemap:metatags-taxonomies:news

到结果页面 URL 中的查询参数,它确实有效,但我不明白如何将其注入表单。对于基于日期的限制,我尝试添加

&sort=more:pagemap:metatags-pubdate:r:YYYYMMDD:YYYYMMDD

但无法使其工作。获取 XML 确实有效:

http://www.google.com/search?q=intitle:[mysite]%20more:pagemap:metatags-taxonomies:News&sort=metatags-pubdate:r:20120401:20120830&cx=[mykey]client=google-csbe&output=xml

返回正确的结果。

有没有假设这么多的文档?我发现的只是没有上下文的代码片段。我已经检查过Filtering and sortingCustom Search Element Control API,当然还有这个站点,但我无法将所有部分放在一起。

4

1 回答 1

0

我设法实现了我想要的。在搜索页面中,我构建了指向我的结果页面的简单表单(如果您必须实施 google 品牌,这可能不可行),并在结果页面中输入以下内容:

  • (在<head>

    <script src="http://www.google.com/jsapi"></script>
    <script>
    // This function extracts the query from the URL (if GET) or builds a search query.
    // Code removed to simplify the example.
    function buildQuery () {
        return '<?php echo $_POST['q'];?> more:pagemap:metatags-taxonomias:News'); // injecting the taxonomy metatag filter
    }
    
    google.load('search', '1', {language : 'es'});
    google.setOnLoadCallback(function() {
        var customSearchOptions = {};
        customSearchOptions[google.search.Search.RESTRICT_EXTENDED_ARGS] = {'sort':'metatags-pubdate:d,metatags-pubdate:r:<?php echo $_POST['startdate'];?>:<?php echo $_POST['enddate'];?>'}; // these come from the POST request, are processed earlier in the script.
        var customSearchControl = new google.search.CustomSearchControl('XXXXXXXXXXX', customSearchOptions); // Put your own App key here.
        customSearchControl.setResultSetSize(google.search.Search.FILTERED_CSE_RESULTSET);
    
        var drawOptions = new google.search.DrawOptions();
        drawOptions.enableSearchResultsOnly(); // I don't want the search box here
    
        customSearchControl.draw('cse-results-press', drawOptions);
        var query = parseQuery();
        if (query) {
            customSearchControl.execute(query);
        }
    }, true);
    </script>
    
  • <body>

    <div id="cse-results-press">Loading...</div>
    
于 2013-04-18T19:17:51.990 回答