Google sitelink search box with Google custom search
Asked Answered
R

4

6

We want to implement Google sitelink search box with Google custom search. In the Google documentation, I found that we need to include the below code to enable sitelink search box

    <script type="application/ld+json">
{
   "@context": "http://schema.org",
   "@type": "WebSite",
   "url": "https://www.example-petstore.com/",
   "potentialAction": {
     "@type": "SearchAction",
     "target": "https://query.example-petstore.com/search?q={search_term_string}",
     "query-input": "required name=search_term_string"
   }
}
</script>

But we are stuck at "target" node in the above properties. Since we dont have any own search page, we want to use Google custom searh, so what value should I fill in this "target" node.

We have already created a Google custom search engine for our site. And found below code there

<script>
  (function() {
    var cx = 'CX_ID';
    var gcse = document.createElement('script');
    gcse.type = 'text/javascript';
    gcse.async = true;
    gcse.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') +
        '//www.google.com/cse/cse.js?cx=' + cx;
    var s = document.getElementsByTagName('script')[0];
    s.parentNode.insertBefore(gcse, s);
  })();
</script>
<gcse:search></gcse:search>

We want to implement sitelink search box like Mashable, Imdb

enter image description here

Please suggest, how to point Google custom search in sitelink search box code.

Thanks

Robbegrillet answered 2/3, 2015 at 11:31 Comment(4)
Maybe this will help you out.Termagant
Thanks for reply @Termagant . But this link is also pointing to same Google documentation :(Robbegrillet
Any other suggestions??Robbegrillet
Nope, not from my side. In my answer I explained what's possible and what's not - obviously you need to dive into the documentation yourself. Or implement a search page - you should be good using google custom search.Termagant
T
1

The only solution:

For this you need a search page on your own website.

For the "target" parameter, do a search on your site and take that URL and replace the search term you used to do the search with "{search_term_string}"

If you make a search page on your website where you use a custom Google Search, you should be able to take that link for the target-property.

The reason for this is that Google does not supply the search functiop

will send the user directly to your website's own search pages.

Sources:

Termagant answered 2/3, 2015 at 12:53 Comment(3)
But websites like mashable.com, imdb.com, makemytrip.com do not have any own search page, still sitelink search box is visible in Google SERP. On searching in sitelink search box, it lands on Google SERP with "site:mashable.com" appended in query string.Robbegrillet
they all do have their own search pages. IMDB for example: http://www.imdb.com/find?ref_=nv_sr_fn&q={search_term_string}Termagant
hmm...imdb has own search engine but this search page is not mapped from sitelink search box. It lands again on Google SERP. imdb must have not provided own search page in "target" node .Robbegrillet
S
1

Shishdem's links do not identify what the write for the target either, after some guess work this works

https://example.com/index.html?q={search_term_string}

for a search box included as a part of the index.html page

if you search box is on the sitemap page use

https://examples.com/sitemap.html?q={search_term_string}

I was only able to find this because the 404 page included the search box, so it returned 404 then results appeared, so I tweaked the code.

This ignores anything else on that page and appears to come up with the right results, so should fit into the JSON-LD (or microdata if you prefer). JSON-LD might be better since google recommends it but states microdata is accepted. Reference to link in original question documentation.

Swagerty answered 27/7, 2015 at 23:38 Comment(1)
Not sure if the sitelinks search box will show up with this target (since I know it takes time for Google to decide on whether to show it or not), but that target links seems to work for me. Link goes straight to the google custom search. FYI, for others, if your site's homepage does not need index.html, you can omit that. So it would just be https://www.example.com/?q={search_term_string}Hypoglossal
W
1

You will need Google CSE WordPress plugin: https://wordpress.org/plugins/google-cse/

  1. Install it.
  2. Enable Custom Search API
  3. Generate API
  4. Use the your generated API and custom Search Engine ID
  5. Find your built in WordPress search on the site

Now you will be able to use SearchAction Shema.org markup (Sitelink search box).

Only works with WordPress.

Wiredraw answered 20/7, 2016 at 17:7 Comment(0)
Z
1

It is not possible!

The Community-Manager https://support.google.com/profile/1532198 answered the question.

https://support.google.com/programmable-search/thread/5714081/how-to-get-search-handler-uri-while-setting-up-a-custom-search-engine-on-my-website?hl=en&msgid=5715607

Zincography answered 6/9, 2021 at 9:38 Comment(2)
Please add further details to expand on your answer, such as working code or documentation citations.Phanerozoic
This feature "Sitelinks" is not available with CSE engine. Please review the document below for guidance: developers.google.com/search/docs/data-types/…Zincography

© 2022 - 2024 — McMap. All rights reserved.