No search results using Composite.Search.GoogleSiteSearch

Topics: Standard packages
Apr 8, 2011 at 9:09 PM

Hi All,


Does anyone have successful experience using Composite.Search.GoogleSiteSearch ?


I try to use Google site search on that website  http://www.whistlercanadaweather.com 
I was provided with search engine ID - 002115411450031521320:w--xfe15kdg and link(TEST LINK) to the page which shows me that website was crawled by Google 
http://www.google.com/cse/home?cx=002115411450031521320:w--xfe15kdg

I call the function as below:

<f:function name="Composite.Search.GoogleSiteSearch.SearchResult" xmlns:f="http://www.composite.net/ns/function/1.0"> 
	<f:param name="SearchEngineId" value="002115411450031521320:w--xfe15kdg" /> 
	<f:param name="PageSize" value="20" /> 
</f:function>


and unfortunately there are no any search results for any word i try to use. 

For example http://www.whistlercanadaweather.com/Home/Link-To-Us/Search.aspx?q=Whistler doesn't return any search results but when I search with "Whistler" using TEST LINK I can see a lot of search results.

I don't see any error message in C1 log and it seems to me like there is an issue with Composite.Search.GoogleSiteSearch.

 

C1 installation information: 

Composite C1 2.0 SP1 Patch 1
Build no. 2.0.3980.20024
Installation ID:aff01032-b9fa-4990-9747-1e401edc9394

Thanks in advance!

-Ruslan

Apr 11, 2011 at 5:13 PM

I have no experience in using this package i'm afraid. I have a codesnippet for you though to use Google Custom Search directly from C# where you can create a UserControl to show your search result.

Apr 11, 2011 at 5:32 PM
Edited Apr 11, 2011 at 5:40 PM

Hi burningice,

Thank you for the reply!

C1's package for Google Custom Search works fine but I need to use Google Site Search. Maybe someone from C1 team can provide us with fully competent answer if the package is working at the moment or not.

 

-Ruslan

Apr 11, 2011 at 5:50 PM

In my terminology both Custom Search and Site Search its the same anyway... you have an CSE account where you pay for getting a feed without ads. This is based on C1 and Google Site Search (Custom Search where you pay for an xml-feed).

http://ws3.oqaasileriffik.wwwdev.punktum.gl/en/search?q=siberian

Apr 11, 2011 at 6:06 PM

OK,

then please provide me with the code snippet you have mentioned.

my email - rvesyliv AT gmail.com

-Ruslan

Coordinator
Apr 12, 2011 at 8:52 AM

Hello @rvesyliv

I just tested Google Site Search and it works with our unique ID, but doesn't work with your  002115411450031521320:w--xfe15kdg.

This package it trying to load Google URL in next format(with your unique ID):

http://www.google.com/search?output=xml&client=google-csbe&cx=002115411450031521320:w--xfe15kdg&q=Whistle&num=20&start=0&lr=lang_en

and got 403 ERROR in result output.

Currently don't know what is the problem. Do you use a paid Business account? Since you have the free or standard edition of Custom Search, you won't be able to perform this function.

Apr 12, 2011 at 6:16 PM

@revysil i just paste it here, maybe others can use it too

public abstract class BaseWebsiteSearcher
{
    public string Query { get; private set; }

    public BaseWebsiteSearcher(string query)
    {
        Query = query;
    }

    public abstract SearchResult DoSearch(int pageSize, int start);
}

public class GoogleCustomSearcher : BaseWebsiteSearcher
{
    private const string searchString = "http://www.google.com/cse?cx= search_engine_id &client=google-csbe&output=xml_no_dtd";

    public GoogleCustomSearcher(string query) : base(query) { }

    public override SearchResult DoSearch(int pageSize, int start)
    {
        string uri = searchString;

        uri += "&q=" + HttpUtility.UrlEncode(Query);
        uri += "+more:" + getMoreString();
        uri += "&num=" + pageSize;
        uri += "&start=" + (start - 1);
        uri += "&hl=" + getHostLanguage();
        uri += "&lr=" + getLanguageFilter();

        var source = XDocument.Load(uri);

        return Parse(source);
    }

    private SearchResult Parse(XDocument source)
    {
        var res = source.XPathSelectElement("/GSP/RES");
        if (res != null)
        {
            int totalCount = int.Parse(res.Element("M").Value);
            var list = getResultList(res);

            return new SearchResult(list, totalCount);
        }

        return SearchResult.Empty;
    }

    private IEnumerable<SearchResultItem> getResultList(XElement source)
    {
        foreach (var el in source.Elements("R"))
        {
            if (el.Attribute("MIME") == null)
            {
                string url = el.Element("U").Value;
                string title = el.Element("T").Value;
                string summary = el.Element("S").Value;

                var uri = new Uri(url);
                var node = SiteMap.Provider.FindSiteMapNode(uri.LocalPath);

                if (node != null)
                {
                    yield return new SearchResultItem(node, summary);
                }                    
            }
        }
    }

    private static string getMoreString()
    {
        switch (CultureInfo.CurrentCulture.Name)
        {
            case "da-DK": return "danish";
            case "en-US": return "english";
            default: return "greenlandic";
        }
    }

    private static string getHostLanguage()
    {
        switch (CultureInfo.CurrentCulture.Name)
        {
            case "en-US": return "en";
            default: return "da";
        }
    }

    private static string getLanguageFilter()
    {
        switch (CultureInfo.CurrentCulture.Name)
        {
            case "en-US": return "lang_en";
            default: return "lang_da";
        }
    }
}

public class SearchResult
{
    public static SearchResult Empty = new SearchResult() { TotalCount = 0, InnerList = Enumerable.Empty<SearchResultItem>() };

    public int TotalCount { get; private set; }
    public IEnumerable<SearchResultItem> InnerList { get; private set; }

    private SearchResult() { }

    public SearchResult(IEnumerable<SearchResultItem> list, int totalCount)
    {
        InnerList = list;
        TotalCount = totalCount;
    }
}

public class SearchResultItem
{
    public string Summary { get; private set; }
    public SiteMapNode Node { get; private set; }

    public SearchResultItem(SiteMapNode node, string summary)
    {
        Node = node;
        Summary = fixSummary(summary);
    }

    private string fixSummary(string input)
    {
        input = input.Replace("<br>", "<br />");
        input = input.Replace(";", HttpUtility.HtmlEncode(";"));

        return input;
    }
}