+1-888-365-2779
Try Now
More in this section

Forums / Developing with Sitefinity / Search fields & Inserting query strings

Search fields & Inserting query strings

5 posts, 1 answered
  1. Michael
    Michael avatar
    32 posts
    Registered:
    14 Jan 2010
    14 May 2010
    Link to this post

    Hello,

    I looking for advice on two issues I'm currently having, both are related to search but cover cover slightly different aspects. We have a search function, that uses a custom index along with a custom SearchResult control, along with this we've also modified the SearchBox control, to incorporate extra functionality. This functionality takes the form of two drop down boxes, one is used to select specific course areas to search in and the other selects whether to search specifically for Full Time or Part Time courses. 

    The two issues I'm having are:

    1. In the custom index I'd like to include extra fields in which data can be held, specifically discipline (course area) and attendance (full/part time), as at the moment when indexing information, this is being put in with the rest of the content.
     
    I would like it seperated so better searches can be performed looking for specific content in the extra fields rather than the whole of the content as the way we are currently indexing results means that in the future clashes will occur for future updates we have planned and this will return inaccurate results.

    2. The second issue relates to the drop down boxes being used in the SearchBox control and the resulting queries. At present I'm working by adding data to the querystring for the page thats called, so say someone searches for "cooking" in the ICT department (from the discipline box) the query will then become "cooking ICT", which results in the search box displaying "cooking ICT" when the page loads, when I would only like the user to be able to see there original search string and no modifiers that are being added.

    So what I'm wondering is if theres a way that this can be done, by injecting the extra query information into the query string further along the line which then wont be displayed back to the user.
     
    This also links in with problem 1. in that when I've found out how to do that I'd then like to add the "ICT" querystring modifier to search specifically in the discipline (course area) field within the CustomIndex. 

    I've provided a small piece of example code below to show how I'm currently using the drop down boxes and updating the querystring.

    switch(DropDownList1.SelectedIndex)
            {
                case 0:
                    if (DropDownList2.SelectedIndex == 0)
                    {
                        Response.Redirect(Request.AppRelativeCurrentExecutionFilePath + "?IndexCatalogue=" + "CustomIndex" + "&SearchQuery=" +
                                          queryText.Text);
                    }
                    else if (DropDownList2.SelectedIndex == 1)
                    {
                        Response.Redirect(Request.AppRelativeCurrentExecutionFilePath + "?IndexCatalogue=" + "CustomIndex" + "&SearchQuery=" +
                                          queryText.Text + " FT");
                    }
                    else
                    {
                        Response.Redirect(Request.AppRelativeCurrentExecutionFilePath + "?IndexCatalogue=" + "CustomIndex" + "&SearchQuery=" +
                                          queryText.Text + " PT");
                    }
                    break;

    I know what I've put isn't paticularly clear, but I've tried as best I can to explain it at the moment, so if you have any questions please feel free to ask and I'll try and clarify our goals a little better. I can also provide further code showing our updates to the SearchResult and SearchBox controls and also our CustomIndex methods.
  2. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    17 May 2010
    Link to this post
    Hello Michael,

    1.You need to create a custom index provider. Sample code can be found at implementing search index
    The sample illustrates how to create a provide that inherits from ContentIndexProvider. By default this provider adds the following data to the index

    path to the item, content of an item , MetaFields of the item, culture of the item , AdditionalFields  - all additional meta fields that you may want to append to the index.

    You could add some more data to the index by overriding GetContentToIndex method of ContentIndexProvider or creating a custom IndexerInfor class and override GetMetaData().

    By default the SearchManager expose the following search fields

    • "title"
    • "path"
    •  "keywords"
    •  "description"
    Everything else is added to the snippet. The Search method of SearchManager is static and it could not be overridden, so you could extract the additional data you are looking for from the snippet.

    2. The SearcRestults control reads the QueryString IndexCatalogue  and SearchQuery values so that it generates the proper ResultItems. As far as I see it you are building the QueryString in your code by adding queryText.Text, so you should be able to control what is added to the query and what not. You should form the ResultItem based on IndexCatalogue and keep the SearchQuery unchanged.

    Greetings,
    Ivan Dimitrov
    the Telerik team

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
  3. Michael
    Michael avatar
    32 posts
    Registered:
    14 Jan 2010
    18 May 2010
    Link to this post
    Hi Ivan,

    Thanks for the reply, in terms of meta fields, how should they appear in the indexed information? Are they just added to the content? At present I've already got an index but am working on expanding it to include the further functionality required, so am just currently testing. I've implemented GetMetaData(), GetData() and also GetContentToIndex, using code avaliable off the support forum. However as I say when I look at the index in App_Data I see the metafields have been included in the content, is this correct?

    As I say I'm just currently testing so I can see how the functionality will improve our search and have included the code I'm using below: 

    CustomIndexProvider

    using System;
    using System.Collections;
    using System.Collections.Generic;
    using Telerik.Cms.Engine;
    using Telerik.Framework.Search;
      
    namespace CustomIndexMk2
    {
        /// <summary>
        /// Summary description for CustomIndexProvider
        /// </summary>
        public class CusIndexProvider : IIndexingServiceClient
        {
            /// <summary>
            /// Defines the name of the provider. This name is used to mange providers within Indexing Service.
            /// </summary>
            public string Name
            {
                get
                {
                    return "CustomIndexMk2";
                }
            }
      
            /// <summary>
            /// Provides detailed description of the client
            /// </summary>
            public string Description
            {
                get
                {
                    return "Provides indexing for external data.";
                }
            }
      
            /// <summary>
            /// Meta fields for this provider
            /// </summary>
            protected string[] MetaFields
            {
                get
                {
                    return new string[]{
                        "Title",
                        "attendance"
                    };
                }
            }
      
            public IIndexerInfo[] GetContentToIndex()
            {
                //Number or records to be added for indexing
                int count = 10;
      
                //Test content values
                string title = "Test title ";
                string attendance = "FT";
                string text = "Test content ";
      
                List<IIndexerInfo> list = new List<IIndexerInfo>();
      
                for (int i = 0; i < count; i++)
                {
                    Hashtable metaFields = new Hashtable();
                    foreach (string key in this.MetaFields)
                    {
                        metaFields.Add(key, "");
                    }
      
                    metaFields["Title"] = title + i;
                    metaFields["attendance"] = attendance + i;
      
                    list.Add(
                        new CusIndexerInfo(
                        string.Format("http://localhost/details.aspx?id={0}", i),
                        metaFields,
                        text + i)
                        );
                }
      
                return list.ToArray();
            }
      
            public string[] GetUrlsToIndex()
            {
                return new string[0];
            }
      
            public event EventHandler<IndexEventArgs> Index;
      
            public void Initialize(System.Collections.Generic.IDictionary<string, string> settings)
            {
            }
        }
    }

    CustomIndexerInfo

    using System;
    using System.Collections;
    using System.Text;
    using Telerik.Framework.Search;
      
    namespace CustomIndexMk2
    {
        /// <summary>
        /// Summary description for CustomIndexerInfo
        /// </summary>
        public class CusIndexerInfo : IIndexerInfo
        {
            private string _url;
            private Hashtable _metaFields;
            private string _content;
            public Guid ID { get; set; }
      
            public CusIndexerInfo(string url, Hashtable metaFields, string content)
            {
                this._url = url;
                this._metaFields = metaFields;
                this._content = content;
            }
      
            protected string GetMetaData()
            {
                StringBuilder sb = new StringBuilder();
                IDictionaryEnumerator meta = _metaFields.GetEnumerator();
      
                while (meta.MoveNext())
                {
                    sb.AppendLine();
                    sb.Append("<");
                    sb.Append(meta.Key);
                    sb.Append(">");
                    sb.Append(meta.Value.ToString());
                    sb.Append("</");
                    sb.Append(meta.Key);
                    sb.Append(">");
                }
                return sb.ToString();
            }
      
            #region IIndexerInfo Members
      
            public Encoding Encoding
            {
                get { return Encoding.UTF8; }
            }
      
            public byte[] GetData()
            {
                string text = (string)this._content;
      
                //If we have any meta data add it to the index content
                if (this._metaFields.Count > 0)
                {
                    text += this.GetMetaData();
                }
      
                return this.Encoding.GetBytes(text);
            }
      
            public string MimeType
            {
                get { return "text/html"; }
            }
      
            public string Path
            {
                get { return this._url; }
            }
      
            #endregion
      
            #region IIndexerInfo Members
      
            public string Culture
            {
                get { return string.Empty; }
            }
      
            public System.Guid ItemID
            {
                get { return ID; }
            }
      
            public string ResolveIndexPath()
            {
                return Path;
            }
      
            #endregion
        }
    }
  4. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    19 May 2010
    Link to this post
    Hi Michael,

    By default the SearchManager expose the following search fields

    • "title"
    • "path"
    •  "keywords"
    •  "description"
    Everything else is added to the snippet. The Search method of SearchManager is static and it could not be overridden, so you could extract the additional data you are looking for from the snippet.

    Then Lucene itself extracts all data from the snippet. Having the above said you can access only the properties of the ResultItem

    Best wishes,
    Ivan Dimitrov
    the Telerik team

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
    Answered
  5. Michael
    Michael avatar
    32 posts
    Registered:
    14 Jan 2010
    24 May 2010
    Link to this post
    Thank you Ivan I thought so but I just needed some clarification.
Register for webinar
5 posts, 1 answered