Site Search for blogs

Occasional Contributor

Is there any good we to implement Keyword search on Blog sites? I read post of usign Google Custom Search in past but as of April google stopped paid subscription for Google Custom Search and we dont want advertisement on our search.

 

Is there any alternative way to implement it?

7 Replies 7
New Contributor | Diamond Partner | Certified Trainer

Hey Rikin, 

 

You are right, as of April, Google has stopped offering their GSS and now shows advertisements if you try using their custom site search. If you are looking for alternatives to try, check out some of these. One of them may be an acceptable substitute to avoid ads! 

 

- Christine

Reply
0 Upvotes
Regular Contributor

You can use query string parameters and the "is string_containing" expression test. When creating the blog content loop. To connect a "search bar" to this search functionality, you'll need a simple form that, when submitted, refreshes the page OR takes the user to a new "search results" page.

 

Try something like this on your homepage:

<form action="/your-blog">
    <input type="text" name="search" default="Search" required>
    <input type="submit" value="Submit">
</form>

The code above puts a simple form with a text box and a submit button wherever you want. When the user clicks "submit" (after adding a search term) they will be taken to yourdomain.com/your-blog (which should be your blog slug).

 

Within your Blog Listing code (take a walk through this resource to learn more), find the {% for loop %} that goes through each piece of content before outputting the HTML. It will typically look like this: 

{% for content in contents %}

The magic here is in reading the values submitted by the form from the previous page. We can use the request.query_dict variable (see more about supported variables) to check for a "search" parameter in the URL.

{% set querys = request.query_dict %}
{% if querys.search %} {% set search = querys.search|lower %}
{% else %}{% set search = " " %}{% endif %}

This sets a variable = to all of the querys in the url. Ex: your form submit leads you to:

The querys variable would now equal a dictionary of 'search': 'fun' -- you can now use querys.search to directly get the value "fun" for use in your comparison to the post title.

 

Note: I used the lower filter to make the search term all lowercase (this will be the same for the title of the post so we don't have case matching issues)

 

Here's what the full code for your blog listing should look like:

{% set querys = request.query_dict %}
{% if querys.search %} {% set search = querys.search|lower %}{% else %}{% set search = " " %}{% endif %}
{% for content in contents %}
    {% if content.name|lower is string_containing search %}
        {{content.name}}
    {% endif %}
{% endfor %}

This full copypasta checks for a query_dict in the URL, sets the dictionary to the search query, loops through all blog content in your content list, and compares the search term to the title of the post.

 

You can use this functionality wherever you want really, as you can call blog_recent_posts(<blogid>,100) wherever (landing pages, blogs, website pages). So if you want to have the search bar on your homepage and link to a "search results" page instead of a blog, you can do that as well.

 

Happy HubLing,

- Mike

Regular Contributor

Looks awesome mgrubbs. Do you have a working example of this functionality?

Reply
0 Upvotes
Highlighted
Regular Contributor

I don't have a public-facing version unfortunately - but you can very easily test it out yourself on an internal blog by adding in the HubL code (don't need the form) in the listing view of a blog and putting a ?search=word in your blog listing page when you are on the live blog url.

Reply
0 Upvotes
Top Contributor

This is great. With a few tweaks, I got it to work on a listing template page:

{% set querys = request.query_dict %}
{% if querys.search %} {% set search_value = querys.search|lower %}{% else %}{% set search_value = " " %}{% endif %}
{% for content in contents %}
    {% set content_searched = content.name|lower %}
    {% if content_searched is string_containing search_value %}
        {{content.name}}
    {% endif %}
{% endfor %}

content.name|lower had to be stored in a variable to work in the if statement 

Reply
0 Upvotes
Regular Contributor

Where do we put this HubL code in the blog template?

Reply
0 Upvotes
Top Contributor

That would be a great feature - extended to the website itself as well!

Reply
0 Upvotes