2013-04-17 121 views
-2

我必須實現一個頁面與多個谷歌搜索形式。我們有來自谷歌的自定義搜索引擎的許可證,這是這種情況:通過過濾實施谷歌自定義搜索

  • 我有一個搜索表單這是目前在每執行一個簡單的搜索,並顯示在一個單獨的網頁搜索結果頁面的頂部。這工作。

  • 我有一個特定的頁面,另外還顯示了另外兩個搜索表單:一個應該按類別過濾文章,另一個應該按類別過濾文章並將結果限制在某個月。我爲此添加了每個文章的發佈日期的元鍵。

我已經獲得了位的文件丟失,雖然:如果我添加

<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 sorting,Custom Search Element Control API,當然這個網站,但我不能把所有的東西放在一起。

回答

0

我設法實現我想要的。在搜索頁面中,我建立簡單的形式指着我的結果頁面(這可能不是可行的,如果你必須實現谷歌的品牌),並在搜索結果頁,我把下列:

  • (在<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>