Click or drag to resize

ISearch Interface

IT Hit WebDAV Classes Reference
Represents an item that supports search according to DASL standard.

Namespace:  ITHit.WebDAV.Server.Search
Assembly:  ITHit.WebDAV.Server (in ITHit.WebDAV.Server.dll) Version: 13.3.13068
Syntax
public interface ISearch : IItemCollection, 
	IHierarchyItem, IHierarchyItemBase

The ISearch type exposes the following members.

Properties
  NameDescription
Public propertyCode exampleCreated
Gets the creation date of the item in repository expressed as the coordinated universal time (UTC).
(Inherited from IHierarchyItemBase.)
Public propertyCode exampleModified
Gets the last modification date of the item in repository expressed as the coordinated universal time (UTC).
(Inherited from IHierarchyItemBase.)
Public propertyCode exampleName
Gets the name of the item in repository.
(Inherited from IHierarchyItemBase.)
Public propertyCode examplePath
Unique item path in the repository relative to storage root.
(Inherited from IHierarchyItemBase.)
Top
Methods
  NameDescription
Public methodCode exampleCopyToAsync
Creates a copy of this item with a new name in the destination folder.
(Inherited from IHierarchyItem.)
Public methodCode exampleDeleteAsync
Deletes this item.
(Inherited from IHierarchyItem.)
Public methodCode exampleGetChildrenAsync
Gets direct children of this folder.
(Inherited from IItemCollection.)
Public methodCode exampleGetPropertiesAsync
Gets values of all properties or selected properties for this item.
(Inherited from IHierarchyItem.)
Public methodCode exampleGetPropertyNamesAsync
Gets names of all properties for this item.
(Inherited from IHierarchyItem.)
Public methodCode exampleMoveToAsync
Moves this item to the destination folder under a new name.
(Inherited from IHierarchyItem.)
Public methodCode exampleSearchAsync
Returns a list of items that correspond to a search request.
Public methodCode exampleUpdatePropertiesAsync
Adds, modifies and removes properties for this item.
(Inherited from IHierarchyItem.)
Top
Remarks

Implement this interface on folders that suppoort search. When search request is recived the DavEngineAsync calls SearchAsync(String, SearchOptions, ListPropertyName, NullableInt64, NullableInt64) method.

If this interface is found on folder items, your server will include DASL: <DAV:basicsearch> header and SEARCH token in Allow header in response to OPTIONS requests. The WebDAV clients that support DASL search, including IT Hit Ajax File Browser, may rely on this header and token to display search user interface.

Examples

The code below is part of 'WebDAVServer.FileSystemStorage.AspNet' C# & VB samples provided with the SDK.

public async Task<PageResults> SearchAsync(string searchString, SearchOptions options, List<PropertyName> propNames, long? offset, long? nResults)
{
    bool includeSnippet = propNames.Any(s => s.Name == snippetProperty);

    // search both in file name and content
    string commandText =
        @"SELECT System.ItemPathDisplay" + (includeSnippet ? " ,System.Search.AutoSummary" : string.Empty) + " FROM SystemIndex " +
        @"WHERE scope ='file:@Path' AND (System.ItemNameDisplay LIKE '@Name' OR FREETEXT('""@Content""')) " +
        @"ORDER BY System.Search.Rank DESC";

    commandText = PrepareCommand(commandText,
        "@Path", this.dirInfo.FullName,
        "@Name", searchString,
        "@Content", searchString);

    Dictionary<string, string> foundItems = new Dictionary<string, string>();
    try
    {
        // Sending SQL request to Windows Search. To get search results file system indexing must be enabled.
        // To find how to enable indexing follow this link: http://windows.microsoft.com/en-us/windows/improve-windows-searches-using-index-faq
        using (OleDbConnection connection = new OleDbConnection(windowsSearchProvider))
        using(OleDbCommand command = new OleDbCommand(commandText, connection))
        {
            connection.Open();
            using(OleDbDataReader reader = command.ExecuteReader())
            {
                while (await reader.ReadAsync())
                {
                    string snippet = string.Empty;
                    if (includeSnippet)
                    {
                        snippet = reader.GetValue(1) != DBNull.Value ? reader.GetString(1) : null;
                        // XML does not support control characters or permanently undefined Unicode characters. Removing them from snippet. https://www.w3.org/TR/xml/#charsets
                        if (!string.IsNullOrEmpty(snippet) && invalidXmlCharsPattern.IsMatch(snippet))
                        {
                            snippet = invalidXmlCharsPattern.Replace(snippet, String.Empty);
                        }
                    }
                    foundItems.Add(reader.GetString(0), snippet);
                }
            }
        }
    }
    catch (OleDbException ex) // explaining OleDbException
    {
        context.Logger.LogError(ex.Message, ex);
        switch (ex.ErrorCode)
        {
            case -2147217900: throw new DavException("Illegal symbols in search phrase.", DavStatus.CONFLICT);
            default: throw new DavException("Unknown error.", DavStatus.INTERNAL_ERROR);
        }
    }

    IList<IHierarchyItem> subtreeItems = new List<IHierarchyItem>();
    foreach (string path in foundItems.Keys)
    {
        IHierarchyItem item = await context.GetHierarchyItemAsync(GetRelativePath(path)) as IHierarchyItem;
        if (item == null)
        {
            continue;
        }

        if (includeSnippet && item is DavFile)
            (item as DavFile).Snippet = HighlightKeywords(searchString.Trim('%'), foundItems[path]);

        subtreeItems.Add(item);
    }

    return new PageResults(offset.HasValue && nResults.HasValue ? subtreeItems.Skip((int)offset.Value).Take((int)nResults.Value) : subtreeItems, subtreeItems.Count);

}
Examples

The code below is part of 'WebDAVServer.FileSystemSynchronization.AspNetCore' C# & VB samples provided with the SDK.

C#
public async Task<PageResults> SearchAsync(string searchString, SearchOptions options, List<PropertyName> propNames, long? offset, long? nResults)
{
    bool includeSnippet = propNames.Any(s => s.Name == snippetProperty);

    // search both in file name and content
    string commandText =
        @"SELECT System.ItemPathDisplay" + (includeSnippet ? " ,System.Search.AutoSummary" : string.Empty) + " FROM SystemIndex " +
        @"WHERE scope ='file:@Path' AND (System.ItemNameDisplay LIKE '@Name' OR FREETEXT('""@Content""')) " +
        @"ORDER BY System.Search.Rank DESC";

    commandText = PrepareCommand(commandText,
        "@Path", this.dirInfo.FullName,
        "@Name", searchString,
        "@Content", searchString);

    Dictionary<string, string> foundItems = new Dictionary<string, string>();
    try
    {
        // Sending SQL request to Windows Search. To get search results file system indexing must be enabled.
        // To find how to enable indexing follow this link: http://windows.microsoft.com/en-us/windows/improve-windows-searches-using-index-faq
        await using (OleDbConnection connection = new OleDbConnection(context.Config.WindowsSearchProvider))
        await using(OleDbCommand command = new OleDbCommand(commandText, connection))
        {
            connection.Open();
            await using(OleDbDataReader reader = command.ExecuteReader())
            {
                while (await reader.ReadAsync())
                {
                    string snippet = string.Empty;
                    if (includeSnippet)
                    {
                        snippet = reader.GetValue(1) != DBNull.Value ? reader.GetString(1) : null;
                        // XML does not support control characters or permanently undefined Unicode characters. Removing them from snippet. https://www.w3.org/TR/xml/#charsets
                        if (!string.IsNullOrEmpty(snippet) && invalidXmlCharsPattern.IsMatch(snippet))
                        {
                            snippet = invalidXmlCharsPattern.Replace(snippet, String.Empty);
                        }
                    }
                    foundItems.Add(reader.GetString(0), snippet);
                }
            }
        }
    }
    catch (OleDbException ex) // explaining OleDbException
    {
        context.Logger.LogError(ex.Message, ex);
        switch (ex.ErrorCode)
        {
            case -2147217900: throw new DavException("Illegal symbols in search phrase.", DavStatus.CONFLICT);
            default: throw new DavException("Unknown error.", DavStatus.INTERNAL_ERROR);
        }
    }

    IList<IHierarchyItem> subtreeItems = new List<IHierarchyItem>();
    foreach (string path in foundItems.Keys)
    {
        IHierarchyItem item = await context.GetHierarchyItemAsync(GetRelativePath(path)) as IHierarchyItem;
        if (item == null)
        {
            continue;
        }

        if (includeSnippet && item is DavFile)
            (item as DavFile).Snippet = HighlightKeywords(searchString.Trim('%'), foundItems[path]);

        subtreeItems.Add(item);
    }

    return new PageResults(offset.HasValue && nResults.HasValue ? subtreeItems.Skip((int)offset.Value).Take((int)nResults.Value) : subtreeItems, subtreeItems.Count);

}
See Also