Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

orchardcms

I'm trying to programmatically create indexes corresponding to several types, so I can search through items of specific types in my controllers. (I tried using the type keyword in Lucene, but that seems to not work at all no matter what I do, so I changed my approach.)

However, I can't figure out how to tell Orchard to include a specific type in an index in a migration. I tried using:

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

but that just returns null as the result of GetTypeDefinition().

I'm looking at using:

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

but that seems like it replaces the previous configured index, if it works at all (EDIT: nope), and I don't want to clobber the existing setting. (A different person on the team is handling our setup recipe.)

Is there any place where I could touch that setting and have it be stored and actually used by Orchard outside the recipe file?


To illustrate what I'm trying to accomplish using the analogous admin UI changes, under Content Definition > [Content Type Name] > Edit:

Before:

Content type definition before adding to index.

After:

Content type definition after adding to index.

like image 824
millimoose Avatar asked Sep 27 '22 01:09

millimoose


1 Answers

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

ContentDefinitionManager.AlterTypeDefinition(nameof(contentType),type => 
type
.Indexed("FirstIndex", "SecondIndex"));
like image 185
humanoidcreature Avatar answered Nov 15 '22 12:11

humanoidcreature