Skip to content

Instantly share code, notes, and snippets.

@hartviglarsen
Last active March 10, 2022 12:18
  • Star 7 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save hartviglarsen/44743b32d4eea8eda3fb6d49b60dd547 to your computer and use it in GitHub Desktop.
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())
_examineManager.AddIndex(index);
}
public void Terminate() { }
}
[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>();
}
}
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 };
}
}
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));
}
}
}
}
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;
}
}
}
@cleversolutions
Copy link

Thanks Morten, I’ve updated Umbraco.PDF for V8 so I’m sure I’ll get some feedback when Shannon reviews that PR. Thanks for this gist, it’s been super helpful.

@Shazwazza
Copy link

Please don't manually unlock the index. There should be no need to do this and if it's locked then we need to figure out why since this will be covering up another issue. The usage of CreateFileSystemLuceneDirectory makes sure that the correct lucene lock policy is used and then we already unlock all LuceneIndex's on startup based on this locking policy. The above code abides by this.

With all my tests locally I cannot replicate indexes being locked on restart or anything.

Am currently trying to figure this out with @hartviglarsen if he can replicate.

@AndersBrohus-MySupport
Copy link

Any news on this with index's not getting unlocked? :)

@cleversolutions
Copy link

cleversolutions commented Aug 29, 2019 via email

@cmwalolo
Copy link

cmwalolo commented Aug 9, 2021

Following the example... I got System.InvalidOperationException: 'Unable to resolve type: Umbraco.Web.Search.ExamineComponent, service name: OMG.... I'm getting mad to migrate to V8 ! :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment