RG-3
RG-3

Reputation: 6188

Creating Custom Index through C# in Sitecore 6.5

I want to create custom index in Sitecore 6.5 through C#. I read this link: Creating Index through .Config

But I want to create custom index through C#, not any .config files. Any help?

Upvotes: 0

Views: 408

Answers (2)

RomanO
RomanO

Reputation: 1

Lucene.Net.Index.IndexWriter writer = new IndexWriter(_path, new StandardAnalyzer(), true);
writer.SetUseCompoundFile(true);
Lucene.NET.Documents.Document doc = new Document();
doc.Add(new Lucene.Net.Documents.Field("Field1Name", yourField1Value, Lucene.Net.Documents.Field.Store.YES, Lucene.Net.Documents.Field.Index.TOKENIZED));
doc.Add(new Lucene.Net.Documents.Field("Field2Name", yourField2Value, Lucene.Net.Documents.Field.Store.YES, Lucene.Net.Documents.Field.Index.TOKENIZED));
writer.AddDocument(doc);
writer.Optimize();
writer.Close();

"yourFieldsValue" will probably come from Sitecore Item[] but it doesn;t have to. If you point _path to the non-system index existing in web.config or add it there, you will be able to see results in Sitecore content any index viewer tool. To utilize this index use Lucene.Net.Search.IndexSearcher.Search() method.

Upvotes: 0

Mark Ursino
Mark Ursino

Reputation: 31435

You cannot "create" the index via C#, you can only query and work with it in C#. In order to "have" an index it must be defined via configuration which defines the properties of how the index should exist, e.g. item types to include, fields to include, root path to begin indexing at, etc.

Upvotes: 2

Related Questions