如何将 Google 自定义搜索限制为特定 URL?

发布于 2025-01-04 14:50:04 字数 2158 浏览 1 评论 0原文

网页上有一个很长的公司名称列表。
每个名称都是指向位于列表页面下方的个人资料页面的链接。

因此,列表页面位于此处

http://example.com/companies/

,个人资料位于此处

http://example.com/companies/companyOne  
http://example.com/companies/companyTwo  
http://example.com/companies/companyThree

我想在该列表页面上实现 Google 自定义搜索框,以便访问者可以输入查询并仅显示相关的公司名称。我已经为搜索结果创建了一个单独的页面,在这里:

http://example.com/companies/results

所以我想基本上,我想将搜索限制到该单个列表页面

http://example.com/companies/

我已经搜索了代码和文档,但我有权利现在返回网站所有页面的结果。并且 setSearchStartingCallback 永远不会被调用

(抱歉,我无法提供实时 URL,这是一个私人测试版。)感谢您的任何想法

<script src="http://www.google.com/jsapi" type="text/javascript"></script>
<script type="text/javascript"> 
    google.load('search', '1', {language : 'fr', style : google.loader.themes.MINIMALIST});
    google.setOnLoadCallback
    (  
        function() 
        {
            var customSearchControl = new google.search.CustomSearchControl('MY_GOOGLE_ID_THINGIE');            
            customSearchControl.setResultSetSize(google.search.Search.FILTERED_CSE_RESULTSET);            
            customSearchControl.setSearchStartingCallback
            (
                this,
                function(control, searcher, query) 
                {
                    alert("setSearchStartingCallback");
                    searcher.setQueryAddition("inurl:www.example.com/companies/");    
                    searcher.setRestriction(google.search.Search.RESTRICT_EXTENDED_ARGS,{"as_sitesearch": "www.example.com/companies/"});                    
                }            
            )                        
            var options = new google.search.DrawOptions();
            options.setAutoComplete(true);
            customSearchControl.setAutoCompletionId('MY_GOOGLE_ID_THINGIE+qptype:1');
            options.enableSearchboxOnly("http://www.example.com/companies/results");
            customSearchControl.draw('cse-search-form', options);
        }
        ,true
    );
</script>

On a web page is a very long list of company names.
Each name is a link to a profile page located beneath the list page.

So the list page is located here

http://example.com/companies/

And profiles are here

http://example.com/companies/companyOne  
http://example.com/companies/companyTwo  
http://example.com/companies/companyThree

I want to implement a Google Custom Search box on that list page, so that a visitor could enter a query and be presented with only the relevant company names. I have created a separate page for the search results, here:

http://example.com/companies/results

So I guess basically, I want to limit the search to that single list page:

http://example.com/companies/

I've searched for code and documentation but what I have right now returns results from all the pages of the website. And the setSearchStartingCallback is never called.

(Sorry I cannot provide live URLS, this is a private beta.) Thanks for any ideas!

<script src="http://www.google.com/jsapi" type="text/javascript"></script>
<script type="text/javascript"> 
    google.load('search', '1', {language : 'fr', style : google.loader.themes.MINIMALIST});
    google.setOnLoadCallback
    (  
        function() 
        {
            var customSearchControl = new google.search.CustomSearchControl('MY_GOOGLE_ID_THINGIE');            
            customSearchControl.setResultSetSize(google.search.Search.FILTERED_CSE_RESULTSET);            
            customSearchControl.setSearchStartingCallback
            (
                this,
                function(control, searcher, query) 
                {
                    alert("setSearchStartingCallback");
                    searcher.setQueryAddition("inurl:www.example.com/companies/");    
                    searcher.setRestriction(google.search.Search.RESTRICT_EXTENDED_ARGS,{"as_sitesearch": "www.example.com/companies/"});                    
                }            
            )                        
            var options = new google.search.DrawOptions();
            options.setAutoComplete(true);
            customSearchControl.setAutoCompletionId('MY_GOOGLE_ID_THINGIE+qptype:1');
            options.enableSearchboxOnly("http://www.example.com/companies/results");
            customSearchControl.draw('cse-search-form', options);
        }
        ,true
    );
</script>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心清如水 2025-01-11 14:50:04

用这个

customSearchOptions[google.search.Search.RESTRICT_EXTENDED_ARGS] =  {'as_sitesearch' : 'mywebsite.com/dir/*'};

Use this

customSearchOptions[google.search.Search.RESTRICT_EXTENDED_ARGS] =  {'as_sitesearch' : 'mywebsite.com/dir/*'};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文