By looking at det core-code, I managed to get something working, but I do not know if I am on the right track here. I seems a bit like killing mosquitos with a canon, comparet to the old config-files.
I created my own "CustomIndexCreator"-class, registered it as a composer and the created a component adding the index to the Examine-manager.
public class CustomIndexCreator : LuceneIndexCreator, IUmbracoIndexesCreator
{
public CustomIndexCreator(IProfilingLogger profilingLogger,
ILocalizationService languageService)
{
ProfilingLogger = profilingLogger ?? throw new System.ArgumentNullException(nameof(profilingLogger));
LanguageService = languageService ?? throw new System.ArgumentNullException(nameof(languageService));
}
protected IProfilingLogger ProfilingLogger { get; }
protected ILocalizationService LanguageService { get; }
public override IEnumerable<IIndex> Create()
{
return new[]
{
CreateCustomIndex()
};
}
private IIndex CreateCustomIndex()
{
var index = new UmbracoContentIndex(
"CustomIndex",
CreateFileSystemLuceneDirectory("Custom"),
new UmbracoFieldDefinitionCollection(),
new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_30),
ProfilingLogger,
LanguageService,
new ContentValueSetValidator(true, excludeItemTypes: new string[] { "DocTypeAliasToExclude",}));
return index;
}
}
The composer
[RuntimeLevel(MinLevel = RuntimeLevel.Run)]
public class CustomExamineComposer : IUserComposer
{
public void Compose(Composition composition)
{
composition.Register(typeof(CustomIndexCreator));
composition.Components().Append<CustomExamineComponent>();
}
}
And the component
public class CustomExamineComponent : IComponent
{
private readonly IExamineManager _examineManager;
private readonly CustomIndexCreator _indexCreator;
public CustomExamineComponent(IExamineManager examineManager, CustomIndexCreator indexCreator)
{
_examineManager = examineManager;
_indexCreator = indexCreator;
}
public void Initialize()
{
//we want to tell examine to use a different fs lock instead of the default NativeFSFileLock which could cause problems if the AppDomain
//terminates and in some rare cases would only allow unlocking of the file if IIS is forcefully terminated. Instead we'll rely on the simplefslock
//which simply checks the existence of the lock file
DirectoryFactory.DefaultLockFactory = d =>
{
var simpleFsLockFactory = new NoPrefixSimpleFsLockFactory(d);
return simpleFsLockFactory;
};
foreach (var index in _indexCreator.Create())
_examineManager.AddIndex(index);
}
public void Terminate()
{
}
}
Edit 29.04.2019
Added code to avoid write-lock on indexfile
When I implement this, I get errors on successive restarts from visual studio because the write.lock file inside the custom directory is not being deleted.
According to the behaviour have observed, the lock file normally isn't present when Umbraco first starts up, so perhaps the file can be removed on startup? It works for me, but I cannot say that is the proper solution. The only reason for its existence was for performance. if you delete the lock file on startup, it will get re-created the first time you utilize the search.
I haven't tried this my self as I went with the built in indexes for now. But I found this in the source code for Umbraco.
Try adding this first in the Initialize-method of the CustomExamineComposer, before the foreach(var index in _indexCreator...
//we want to tell examine to use a different fs lock instead of the default NativeFSFileLock which could cause problems if the AppDomain
//terminates and in some rare cases would only allow unlocking of the file if IIS is forcefully terminated. Instead we'll rely on the simplefslock
//which simply checks the existence of the lock file
DirectoryFactory.DefaultLockFactory = d =>
{
var simpleFsLockFactory = new NoPrefixSimpleFsLockFactory(d);
return simpleFsLockFactory;
};
I tried creating a custom examine index both with Ole's example and with the official example (https://our.umbraco.com/documentation/Reference/Searching/Examine/indexing/) , but im getting a write.lock file all the time.
And changing the DefaultLockFactory doesn't work unfortunately.
Any reason why this is now done in code? Something that was pretty simple to edit and update is now a but is now a complex task. I appreciate that this gives more flexibility but config files are useful for basic settings.
Any way to exclude specific content types from the examine results?
Sure you can do this as a filter within the results but is there a way to do this from within the examine query?
Seems a bit overkill to create a custom search index just to omit 1 content type? (I am using tabbed content items which do not have a URL, these still appear in the results and then go to 404's)
Umbraco 8 Examine Indexes
Anyone got any examples or code i can take a look at for implementing a custom index set / searcher
I was just about to ask this as well. In the previous version I added custom indexers using ExamineSettings.config. Is there a different way now?
Thanks in advance
Seb
Did you find any examples? Need to create a custom index set without all doc.types.
Unfortunately not, no
By looking at det core-code, I managed to get something working, but I do not know if I am on the right track here. I seems a bit like killing mosquitos with a canon, comparet to the old config-files.
I created my own "CustomIndexCreator"-class, registered it as a composer and the created a component adding the index to the Examine-manager.
The composer
And the component
Edit 29.04.2019 Added code to avoid write-lock on indexfile
When I implement this, I get errors on successive restarts from visual studio because the write.lock file inside the custom directory is not being deleted.
I'm getting write lock all over aswel... Have you found a solution?
According to the behaviour have observed, the lock file normally isn't present when Umbraco first starts up, so perhaps the file can be removed on startup? It works for me, but I cannot say that is the proper solution. The only reason for its existence was for performance. if you delete the lock file on startup, it will get re-created the first time you utilize the search.
I haven't tried this my self as I went with the built in indexes for now. But I found this in the source code for Umbraco.
Try adding this first in the Initialize-method of the CustomExamineComposer, before the foreach(var index in _indexCreator...
Third edit: I got the write lock once more...
Yes this definetly fixes the write.lock!
edit: and i added it after the foreach, so that seems fine
Does this work for you?
I tried creating a custom examine index both with Ole's example and with the official example (https://our.umbraco.com/documentation/Reference/Searching/Examine/indexing/) , but im getting a write.lock file all the time. And changing the DefaultLockFactory doesn't work unfortunately.
The offical way to create custom indexes can be found in this Gist: https://gist.github.com/hartviglarsen/44743b32d4eea8eda3fb6d49b60dd547
Note that you of course have to register the component in a composer too.
(via Twitter: https://twitter.com/meisterhartvig/status/1136184529734049792)
Creating custom indexes can now be found in the Umbraco documentation: https://our.umbraco.com/documentation/reference/searching/examine/indexing/ (Scroll down to ''Overriding Index Creation")
Tested and working in 8.4.1
Any reason why this is now done in code? Something that was pretty simple to edit and update is now a but is now a complex task. I appreciate that this gives more flexibility but config files are useful for basic settings.
Any way to exclude specific content types from the examine results?
Sure you can do this as a filter within the results but is there a way to do this from within the examine query?
Seems a bit overkill to create a custom search index just to omit 1 content type? (I am using tabbed content items which do not have a URL, these still appear in the results and then go to 404's)
Hi Adrian.
Yes you can do that in your Index or Searcher search query. My example is with a Searcher that can search over multiple Indexes.
is working on a reply...