|
using System; |
|
using System.Collections.Generic; |
|
using Telerik.Sitefinity.Services; |
|
using Telerik.Sitefinity.Services.Search; |
|
using Telerik.Sitefinity.Services.Search.Data; |
|
|
|
namespace Telerik.Sitefinity.TestIntegration.Services.Search |
|
{ |
|
/// <summary> |
|
/// This class demonstrates the CRUD operations with Search Indexes using the search service. |
|
/// </summary> |
|
public class SearchServiceApiSample |
|
{ |
|
/// <summary> |
|
/// Creates the search index that will store the data. |
|
/// </summary> |
|
/// <param name="indexName">The name of the search index.</param> |
|
public void CreateSearchIndex(string indexName) |
|
{ |
|
// create the index metadata |
|
var identityDefinition = Telerik.Sitefinity.Abstractions.ObjectFactory.Resolve<IFieldDefinition>(); |
|
identityDefinition.Name = IdentityField; |
|
identityDefinition.Type = typeof(string); |
|
|
|
var languageDefinition = Telerik.Sitefinity.Abstractions.ObjectFactory.Resolve<IFieldDefinition>(); |
|
languageDefinition.Name = Telerik.Sitefinity.Publishing.PublishingConstants.LanguageField; |
|
languageDefinition.Type = typeof(string); |
|
|
|
var contentFieldDefinition = Telerik.Sitefinity.Abstractions.ObjectFactory.Resolve<IFieldDefinition>(); |
|
contentFieldDefinition.Name = ContentField; |
|
contentFieldDefinition.Type = typeof(string); |
|
|
|
var fieldDefinitions = new List<IFieldDefinition>() { identityDefinition, languageDefinition, contentFieldDefinition }; |
|
|
|
ServiceBus.ResolveService<ISearchService>().CreateIndex(indexName, fieldDefinitions); |
|
} |
|
|
|
/// <summary> |
|
/// Updates the search index with the given content. |
|
/// </summary> |
|
/// <param name="indexName">The index name.</param> |
|
/// <param name="content">The content.</param> |
|
public void UpdateSearchIndex(string indexName, string content) |
|
{ |
|
// fill the index - add documents in it |
|
IList<Telerik.Sitefinity.Services.Search.Data.IField> fields = new List<Telerik.Sitefinity.Services.Search.Data.IField>(); |
|
fields.Add(new Telerik.Sitefinity.Services.Search.Publishing.Field() { Name = IdentityField, Value = new Guid().ToString() }); |
|
fields.Add(new Telerik.Sitefinity.Services.Search.Publishing.Field() |
|
{ |
|
Name = Telerik.Sitefinity.Publishing.PublishingConstants.LanguageField, |
|
Value = Telerik.Sitefinity.Model.Localization.LocalizationHelper.GetLanguageKey(System.Globalization.CultureInfo.CurrentCulture) |
|
}); |
|
fields.Add(new Telerik.Sitefinity.Services.Search.Publishing.Field() { Name = ContentField, Value = content }); |
|
|
|
Telerik.Sitefinity.Services.Search.Data.IDocument document = |
|
new Telerik.Sitefinity.Services.Search.Model.Document(fields, IdentityField); |
|
|
|
ServiceBus.ResolveService<ISearchService>().UpdateIndex(indexName, new[] { document }); |
|
} |
|
|
|
/// <summary> |
|
/// Performs the search operation. |
|
/// </summary> |
|
/// <param name="indexName">Name of the index.</param> |
|
/// <param name="search">The search.</param> |
|
/// <returns>The search results.</returns> |
|
public IResultSet ReadSearchIndex(string indexName, string search) |
|
{ |
|
// prepare query and run the search (read the index) |
|
var queryBuilder = Telerik.Sitefinity.Abstractions.ObjectFactory.Resolve<IQueryBuilder>(); |
|
var searchQuery = queryBuilder.BuildQuery(search, new string[] { ContentField }); |
|
searchQuery.IndexName = indexName; |
|
searchQuery.Skip = 0; |
|
searchQuery.Take = 10; |
|
searchQuery.HighlightedFields = new List<string>() { ContentField }; |
|
|
|
var searchResult = ServiceBus.ResolveService<ISearchService>().Search(searchQuery); |
|
return searchResult; |
|
} |
|
|
|
/// <summary> |
|
/// Deletes the search index. |
|
/// </summary> |
|
/// <param name="indexName">Name of the index.</param> |
|
public void DeleteSearchIndex(string indexName) |
|
{ |
|
ServiceBus.ResolveService<ISearchService>().DeleteIndex(indexName); |
|
} |
|
|
|
/// <summary> |
|
/// The name of the filed that will store the search content. |
|
/// </summary> |
|
public const string ContentField = "content"; |
|
|
|
private const string IdentityField = "IdentityField"; |
|
} |
|
} |