Skip to content

Instantly share code, notes, and snippets.

@Yegoroff
Created July 11, 2012 23:27

Revisions

  1. Alexander Yegorov created this gist Jul 11, 2012.
    75 changes: 75 additions & 0 deletions IndexSettings.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,75 @@
    using System;
    using PlainElastic.Net;
    using PlainElastic.Net.IndexSettings;

    namespace IndexSettigsSample
    {
    class Program
    {
    static void Main()
    {
    var connection = new ElasticConnection("localhost", 9200);

    var settings = BuildIndexSettings();
    string result;

    if (IsIndexExists("store", connection))
    {
    // We can't update settings on active index.
    // So we need to close it, then update settings and then open index back.
    connection.Post(new CloseCommand("store"));

    result = connection.Put(new UpdateSettingsCommand("store"), settings);

    connection.Post(new OpenCommand("store"));
    }
    else
    {
    // Create Index with settings.
    result = connection.Put(Commands.Index("store").Refresh(), settings);
    }

    Console.WriteLine("Index Settings: " + settings);
    Console.WriteLine("JSON result: " + result);

    Console.ReadKey();
    }



    private static string BuildIndexSettings()
    {
    return new IndexSettingsBuilder()
    .Analysis(als => als
    .Analyzer(a => a
    .Custom("lowerkey", custom => custom
    .Tokenizer(DefaultTokenizers.keyword)
    .Filter(DefaultTokenFilters.lowercase)
    )
    .Custom("fulltext", custom => custom
    .CharFilter(DefaultCharFilters.html_strip)
    .Tokenizer(DefaultTokenizers.standard)
    .Filter(DefaultTokenFilters.word_delimiter, DefaultTokenFilters.lowercase, DefaultTokenFilters.stop, DefaultTokenFilters.standard)
    )
    )
    )
    .BuildBeautified();
    }


    private static bool IsIndexExists(string indexName, ElasticConnection connection)
    {
    try
    {
    connection.Head(new IndexExistsCommand(indexName));
    return true;
    }
    catch (OperationExeception ex)
    {
    if (ex.HttpStatusCode == 404)
    return false;
    throw;
    }
    }
    }
    }