-
-
Save cleversolutions/67182459c468da360992f06bae40fa47 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ProductComponent : IComponent | |
{ | |
private readonly IExamineManager _examineManager; | |
private readonly ProductIndexCreator _productIndexCreator; | |
public ProductComponent (IExamineManager examineManager, ProductIndexCreator productIndexCreator) | |
{ | |
_examineManager = examineManager; | |
_productIndexCreator = productIndexCreator; | |
} | |
public void Initialize() | |
{ | |
foreach (var index in _productIndexCreator.Create()){ | |
// ensure the index is unlocked | |
if(index is LuceneIndex) | |
{ | |
var luceneIndex = index as LuceneIndex; | |
var dir = luceneIndex.GetLuceneDirectory(); | |
if (IndexWriter.IsLocked(dir)) | |
{ | |
_logger.Info(typeof(ExamineExtensions), "Forcing index {IndexerName} to be unlocked since it was left in a locked state", luceneIndex.Name); | |
IndexWriter.Unlock(dir); | |
} | |
} | |
_examineManager.AddIndex(index); | |
} | |
} | |
public void Terminate() { } | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[RuntimeLevel(MinLevel = RuntimeLevel.Run)] | |
public class ProductComposer : IUserComposer | |
{ | |
public void Compose(Composition composition) | |
{ | |
composition.Components().Append<ProductComponent>(); | |
composition.RegisterUnique<ProductIndexValueSetBuilder>(); | |
composition.Register<ProductIndexPopulator>(Lifetime.Singleton); | |
composition.RegisterUnique<ProductIndexCreator>(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ProductIndexCreator : LuceneIndexCreator | |
{ | |
public override IEnumerable<IIndex> Create() | |
{ | |
var index = new LuceneIndex("ProductIndex", | |
CreateFileSystemLuceneDirectory("ProductIndex"), | |
new FieldDefinitionCollection( | |
new FieldDefinition("name", FieldDefinitionTypes.FullTextSortable), | |
new FieldDefinition("price", FieldDefinitionTypes.FullText) | |
), | |
new StandardAnalyzer(Version.LUCENE_30)); | |
return new[] { index }; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ProductIndexPopulator : IndexPopulator | |
{ | |
private readonly ProductIndexValueSetBuilder _productValueSetBuilder; | |
private readonly IProductService _productService; | |
public ProductIndexPopulator(ProductIndexValueSetBuilder productValueSetBuilder, IProductService productService) | |
{ | |
_productValueSetBuilder = productValueSetBuilder; | |
_productService = productService; | |
RegisterIndex("ProductIndex"); | |
} | |
protected override void PopulateIndexes(IReadOnlyList<IIndex> indexes) | |
{ | |
var products = _productService.GetAll(); | |
if (products != null && products.Any()) | |
{ | |
foreach (var index in indexes) | |
{ | |
index.IndexItems(_productValueSetBuilder.GetValueSets(products)); | |
} | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ProductIndexValueSetBuilder : IValueSetBuilder<Product> | |
{ | |
public IEnumerable<ValueSet> GetValueSets(params Product[] products) | |
{ | |
foreach (var product in products) | |
{ | |
var indexValues = new Dictionary<string, object> | |
{ | |
["name"] = product.Name, | |
["price"] = product.Price | |
}; | |
var valueSet = new ValueSet(product.Id.ToString(), "product", indexValues); | |
yield return valueSet; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment