• Creating a custom analyzer in ElasticSearch Nest client


     Creating a custom analyzer in ElasticSearch Nest client

    Question: Im very very new to elasticsearch using the nest client, I am creating an index with a custom analyzer, however when testing using analyze it does not seem to use the custom analyzer. Mainly no edgengram tokens appear. Is there anything I am missing that would make my custom analyser the default for the index? When I check my mappings using elastichq they show my custom analyzer.

    ConnectionSettings settings = new ConnectionSettings(new Uri("http://localhost:9200"),    defaultIndex: "forum-app");
    
     IndexSettings indsettings = new IndexSettings();
    
       var an = new CustomAnalyzer();
    
       an.CharFilter = new List<string>();
       an.CharFilter.Add("html_strip");
       an.Tokenizer = "edgeNGram";
       an.Filter = new List<string>();
       an.Filter.Add("standard");
       an.Filter.Add("lowercase");
       an.Filter.Add("stop");
    
    indsettings.Analysis.Tokenizers.Add("edgeNGram", new Nest.EdgeNGramTokenizer
    {
        MaxGram = 15,
        MinGram = 3
    });
    
    indsettings.Analysis.Analyzers.Add("forumanalyzer", an);
    
    ElasticClient client = new ElasticClient(settings);
    
    client.CreateIndex("forum-app", c => c
    .NumberOfReplicas(0)
    .NumberOfShards(1)
    .AddMapping<Forum>(e => e.MapFromAttributes())
    .Analysis(analysis => analysis
    .Analyzers(a => a
    .Add("forumanalyzer", an) 
    )));        
    
    //To index I just do this       
    client.Index(aForum);

    Answer

    You've added your custom analyzer to your index, but now you need to apply it your fields. You can do this on a field mapping level:

    client.CreateIndex("forum-app", c => c
        .NumberOfReplicas(0)
        .NumberOfShards(1)
        .AddMapping<Forum>(e => e
            .MapFromAttributes()
            .Properties(p => p
                .String(s => s.Name(f => f.SomeProperty).Analyzer("formanalyzer")))
        )
        .Analysis(analysis => analysis
            .Analyzers(a => a
                .Add("forumanalyzer", an)
            )
        )
    );
    

    Or you can apply it to all fields by default by setting it as the default analyzer of your index:

    client.CreateIndex("forum-app", c => c
        .NumberOfReplicas(0)
        .NumberOfShards(1)
        .AddMapping<Forum>(e => e.MapFromAttributes())
        .Analysis(analysis => analysis
            .Analyzers(a => a
                .Add("default", an)
            )
        )
    );
    

    More info here in regards to analyzer defaults.

    源文:https://stackoverflow.com/questions/25193800/creating-a-custom-analyzer-in-elasticsearch-nest-client
  • 相关阅读:
    set<char*>s
    sscanf()函数。
    C语言函数sscanf()的用法 (转载
    zjut 1179 平均数
    C++数据间隔
    C++ 保留小数
    c++ 保留小数
    c语言 保留两位小数
    c++ 如何实现保留小数并 且 不进行四舍五入
    uva-657-搜索
  • 原文地址:https://www.cnblogs.com/a-du/p/7278146.html
Copyright © 2020-2023  润新知