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

Forums / Developing with Sitefinity / Custom Index Provider

Custom Index Provider

8 posts, 0 answered
  1. Matthew
    Matthew avatar
    28 posts
    Registered:
    07 Mar 2011
    14 Mar 2011
    Link to this post
    Hi Support,

    I have created a custom index provider to query an external database, I have a couple of questions:
    1) Currently the snippet is "..." as opposed to my description, I can search by the descripiton but the results control does not display it.

    2) Evertime a record is added or modified or deleted I would like to update the indes automatically how can this be done?

    Thanks in advance

    Dan

    EDIT: Point 1 is not very clear so let me try again.

    Currently the snippet is displayed like so "..." as opposed to my description. I can search by the description and the correct results are displayed but agin the description is not.

    Hope that clarifies that point
  2. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    09 Dec 2016
    15 Mar 2011
    Link to this post
    Hi Daniel,

    The dots you see are added by the SearchManager class. These dots are SEARCH_RESULTS_SEPARATOR. If the best fragments are empty then you see only dots which are set to the snippet explicitly.

    Note that the the fields below are not added to the search result

    • "title"
    • "path"
    • "keywords"
    • "description"

    To auto update the the index you need to have events that are thrown when the item is created, edited or deleted. You can take a look at this post where I show how to do this with Libraries provider

    http://www.sitefinity.com/devnet/forums/sitefinity-3-x/developing-with-sitefinity/custom-search-index-breaks-generic-content-module.aspx

    Best wishes,
    Ivan Dimitrov
    the Telerik team
    Registration for Q1 2011 What’s New Webinar Week is now open. Mark your calendar for the week starting March 21st and book your seat for a walk through all the exciting stuff we ship with the new release!
  3. Matthew
    Matthew avatar
    28 posts
    Registered:
    07 Mar 2011
    16 Mar 2011
    Link to this post
    Hi Ivan,

    Thanks for your informative reply. I do have a few more questions

    1) How can i display the description or How can i change the code so that the description was the snippet instead?

    Thanks for the link I have had a quick look and it looks very detailed.

    Thanks again

    Dan
  4. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    09 Dec 2016
    16 Mar 2011
    Link to this post
    Hi Daniel,

    1. You can change the result item when you return in from the search results
    2. You can create your own manager.

    Regards,
    Ivan Dimitrov
    the Telerik team
    Registration for Q1 2011 What’s New Webinar Week is now open. Mark your calendar for the week starting March 21st and book your seat for a walk through all the exciting stuff we ship with the new release!
  5. Matthew
    Matthew avatar
    28 posts
    Registered:
    07 Mar 2011
    16 Mar 2011
    Link to this post
    Hi Ivan,

    I have tried both of these methods -

    Method 1) gives me an error stating that the description is not part of the result item

    Method 2) Hasnt made a difference to the control do i need to modify the code in anyway?

    Thanks for your help

    Dan
  6. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    09 Dec 2016
    16 Mar 2011
    Link to this post
    Hello Daniel,


    1. The description is injected in the snippet
    2. You need a custom control to make this working.

    Kind regards,
    Ivan Dimitrov
    the Telerik team
    Registration for Q1 2011 What’s New Webinar Week is now open. Mark your calendar for the week starting March 21st and book your seat for a walk through all the exciting stuff we ship with the new release!
  7. Matthew
    Matthew avatar
    28 posts
    Registered:
    07 Mar 2011
    16 Mar 2011
    Link to this post
    Hi Ivan

    I am little confused, I have tried to do the following -
    Imports Telerik.Search.Engine
    Imports System.Collections.Generic
    Imports System.Globalization
    Imports Telerik.Lucene.Net.Analysis.Standard
    Imports Telerik.Search.Configuration
    Imports Telerik.Lucene.Net.Documents
    Imports Telerik.Lucene.Net.Search
    Imports Telerik.Lucene.Net.Highlight
    Imports Telerik.Web
    Imports Telerik.Search.Common
    Imports Telerik.Lucene.Net.QueryParsers
     
    Namespace Telerik.Search.WebControls
     
        ''' <summary>
        ''' Summary description for CustomSearchResult
        ''' </summary>
        Public Class CustomSearchResult
            Inherits SearchResult
     
            Public Sub New()
            End Sub
            Private Shared Function ResolveUrl(ByVal p As String) As String
                If p.StartsWith("/") Then
                    ' this is fix for the external pages when page is located in the same site(ex. /Files/Sample.htm)
                    Return p
                End If
                Dim culture As CultureInfo = Nothing
                Dim url As String = UnresolveIndexPath(p, culture)
     
                Dim langSvc As IUrlService = UrlServices.GetService("LanguageService")
     
                If langSvc IsNot Nothing Then
                    Return langSvc.ResolveLanguageUrl(UrlPath.ResolveUrl(url), culture)
                End If
                Return UrlPath.ResolveUrl(url)
            End Function
     
            Public Function Search(ByVal searchQuery As String, ByVal indexName As String, ByVal startIndex As Integer, ByVal max As Integer, ByVal mode As String, ByVal escapeChars As Boolean, _
             ByRef totalItems As Integer) As System.Collections.IList()
     
                Dim result As System.Collections.IList = New List(Of ResultItem)()
                Dim fields As String() = GetFields(indexName)
                Dim indexDirectory As String = PathUtil.GetIndexPhysicalPath(indexName)
                Dim searcher As New IndexSearcher(indexDirectory)
     
                Dim searchMode As QueryParser.Operator = QueryParser.AND_OPERATOR
     
                Dim parser As New MultiFieldQueryParser(fields, New StandardAnalyzer())
     
                If Not [String].IsNullOrEmpty(mode) AndAlso mode = "AnyWord" Then
                    searchMode = QueryParser.OR_OPERATOR
                End If
                parser.SetDefaultOperator(searchMode)
     
                If escapeChars Then
                    searchQuery = QueryParser.Escape(searchQuery)
                End If
     
                Dim query As Query = searcher.Rewrite(parser.Parse(searchQuery))
                Dim highlighter As New Lucene.Net.Highlight.Highlighter(New QueryScorer(query))
                Dim hits As Lucene.Net.Search.Hits = searcher.Search(query, Sort.INDEXORDER)
     
                totalItems = hits.Length()
                If max = 0 Then
                    max = totalItems
                ElseIf startIndex + max > totalItems Then
                    max = totalItems
                Else
                    max = startIndex + max
                End If
     
                For i As Integer = startIndex To max - 1
                    Dim doc As Document = hits.Doc(i)
                    Dim item As New ResultItem()
                    item.Title = doc.[Get]("title")
                    item.Url = ResolveUrl(doc.[Get]("path"))
                    'item.Snippet = doc.[Get]("Description")
                    item.Score = hits.Score(i)
     
                    Dim snippet As New StringBuilder()
                    Dim lastAppendedIsSeparator As Boolean = False
                    For k As Integer = 0 To fields.Length - 1
                        Dim field As String = fields(k)
                        Dim values As String() = doc.GetValues(field)
                        If values IsNot Nothing AndAlso values.Length > 0 Then
                            For j As Integer = 0 To values.Length - 1
                                If values(j).Length > 0 Then
                                    snippet.Append(values(j))
                                    lastAppendedIsSeparator = False
                                    If j < values.Length - 1 Then
                                        'snippet.AppendLine(SearchManager.SEARCH_RESULTS_SEPARATOR)
                                        snippet.AppendLine(doc.[Get]("description"))
                                        lastAppendedIsSeparator = True
                                    End If
                                End If
                            Next
                        End If
                        If k < fields.Length - 1 AndAlso Not lastAppendedIsSeparator Then
                            snippet.AppendLine(doc.[Get]("description"))
                            lastAppendedIsSeparator = True
                        End If
                    Next
                    Dim bestFragments As String() = highlighter.GetBestFragments(New StandardAnalyzer(), fields(0), snippet.ToString(), 5)
                    item.Snippet = [String].Join(SearchManager.SEARCH_RESULTS_SEPARATOR, bestFragments)
                    item.Snippet.TrimEnd(" "c, "."c)
                    item.Snippet += SearchManager.SEARCH_RESULTS_SEPARATOR
                    result.Add(item)
                Next
                searcher.Close()
                Return result
            End Function
     
            Private Shared idxFlds As New Dictionary(Of String, String())()
     
            Private Shared CulterRegex As New Regex("^{(\S+)}(.*)", RegexOptions.Compiled)
     
            Private Shared Function UnresolveIndexPath(ByVal path As String, ByRef culture As CultureInfo) As String
                Dim match As Match = CulterRegex.Match(path)
                If match.Success Then
                    culture = CultureInfo.GetCultureInfo(match.Groups(1).Value)
                    Return match.Groups(2).Value
                End If
                culture = CultureInfo.InvariantCulture
                Return path
            End Function
     
            Private Shared Function GetFields(ByVal provider As String) As String()
                Dim arr As String()
                If Not idxFlds.TryGetValue(provider, arr) Then
                    Dim list As New List(Of String)()
                    For Each element As IndexingTypeElement In ConfigHelper.Handler.Indexers
                        Dim indexer As BaseIndexer = DirectCast(Activator.CreateInstance(Type.[GetType](element.Type), HttpContext.Current.Server.MapPath(PathUtil.GetIndexVirtualPath(provider)), provider, True), BaseIndexer)
                        Try
                            For Each dtls As IIndexFieldDetails In indexer.IndexFields
                                If Not list.Contains(dtls.FieldName) Then
                                    list.Add(dtls.FieldName)
                                End If
                            Next
                        Catch exc As Exception
                            Dim [error] As String = exc.Message
                        Finally
                            If indexer IsNot Nothing Then
                                indexer.Close()
                            End If
                        End Try
                    Next
                    arr = list.ToArray()
                    If list.Count > 0 Then
                        idxFlds.Add(provider, arr)
                    End If
                End If
                Return arr
            End Function
        End Class
    End Namespace

    Unfortunatly this is still causing the "..." to appear.

    What am I doing wrong?

    Thanks

    Dan
  8. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    09 Dec 2016
    18 Mar 2011
    Link to this post
    Hello Daniel,

    Pleas echeck result.Add(item) and what is returned at your end.

    Greetings,
    Ivan Dimitrov
    the Telerik team
Register for webinar
8 posts, 0 answered