【问题标题】:Implementing Google Custom Search with filtering使用过滤实现 Google 自定义搜索
【发布时间】:2013-04-17 21:05:33
【问题描述】:

我必须实现一个包含多个 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

返回正确的结果。

是否有没有太多假设的文档?我发现的只是没有上下文的代码 sn-ps。我检查了Filtering and sortingCustom Search Element Control API,当然还有这个网站,但我无法将所有部分放在一起。

【问题讨论】:

    标签: google-custom-search


    【解决方案1】:

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

    • (在&lt;head&gt;

      <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>
      
    • &lt;body&gt;

      <div id="cse-results-press">Loading...</div>
      

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-09-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-24
      • 1970-01-01
      • 2015-01-18
      相关资源
      最近更新 更多