Indexing

Create a new index

Create a new class for the index, that inherit from IIndex interface.

 

public class NewIndex : IIndex
{
    public string GetName()
    {
        throw new NotImplementedException();
    }

    public bool IndexExists()
    {
        throw new NotImplementedException();
    }

    public bool IsEnabled()
    {
        throw new NotImplementedException();
    }

    public bool CreateIndex()
    {
        throw new NotImplementedException();
    }

    public bool DeleteIndex()
    {
        throw new NotImplementedException();
    }

    public bool ReIndex(RunningStatus status)
    {
        throw new NotImplementedException();
    }

    public bool Add(List<KeyValuePair<string, object>> fields)
    {
        throw new NotImplementedException();
    }

    public bool Update(string byQuery, string byField, List<KeyValuePair<string, object>> fields)
    {
        throw new NotImplementedException();
    }

    public bool Delete(string byQuery, string byField)
    {
        throw new NotImplementedException();
    }

    public IEnumerable<ISearchResult> Search(string query)
    {
        throw new NotImplementedException();
    }

    public int Count()
    {
        throw new NotImplementedException();
    }

    public int Count(string query)
    {
        throw new NotImplementedException();
    }

    public ISearchResult Get(int number)
    {
        throw new NotImplementedException();
    }

    public ISearchResult? Get(int number, string query)
    {
        throw new NotImplementedException();
    }
}

// add to services in startup.cs file
services.AddSingleton<IIndex, NewIndex>();

⚠️ Do not use names multiple times. 

 

Use helping BaseIndex class to use features like "IndexExists" method.

public class UserIndex : BaseIndex<DefaultSearchResult>
{
    public UserIndex(IOptions<NLessSettings> nLessSettings, IWebHostEnvironment webHostEnvironment) 
: base(nLessSettings, webHostEnvironment)
    {
    }

    public override string GetName()
    {
        return "Users";
    }
}

Add index to appsettings.json

To register the index, add the new index to the "Indexing" section in appsettings.json file.

 

Pipelines

To update the index by runtime, without reindex everything. Pipelines are a good way to update index documents.

"Indexing": {
      "Enabled": true,
      "Indexes": [
        {
          "Name": "Preview",
          "Path": "index/preview",
          "IsEnabled": true
        },
        {
          "Name": "Public",
          "Path": "index/public",
          "IsEnabled": true
        },
       {...add here...}
      ]
    },

Accessing an index

Use the IndexerService to get an index.

_indexerService.GetIndex("NewIndex");