How can I add a content type to an index programmatically?

I am trying to programmatically create indexes corresponding to several types, so I can search for elements of a certain type in my controllers. (I tried using the type keyword in Lucene, but that doesn't seem to work at all no matter what I do, so I changed my approach.)

However, I cannot understand how to tell Orchard about the inclusion of a certain type in the index during the migration process. I tried using:

 var typeIndexing = ContentDefinitionManager.GetTypeDefinition(contentType) .Settings.GetModel<TypeIndexing>(); typeIndexing.List = typeIndexing.List.Concat(indexName.Yield()).ToArray(); 

but this returns null as a result of GetTypeDefinition() .

I use:

 ContentDefinitionManager.AlterTypeDefinition(contentType, builder => { builder.WithSetting("TypeIndexing.Indexes", indexName); }); 

but it looks like it replaces the previous configured index, if it works at all ( EDIT: nope), and I don't want to compress the existing settings. (The other person on the team is processing our recipe settings.)

Is there a place where I could touch this setting and save it and actually use Orchard outside the recipe file?


To illustrate what I'm trying to accomplish using similar changes to the admin user interface, under Content Definition> [Content Type Name]> Change :

Front

Determining the type of content before adding to the index.

After:

Determining the type of content after adding to the index.

+5
source share
1 answer

What you are looking for is the Indexed () extension method. It accepts the indexes you want to use in the content type.

 ContentDefinitionManager.AlterTypeDefinition(nameof(contentType),type => type .Indexed("FirstIndex", "SecondIndex")); 
+3
source

Source: https://habr.com/ru/post/1237369/


All Articles