zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    Python基础篇【第十一篇】:正则表达式之计算器
    Python基础篇【第十篇】:正则表达式
    Python基础篇【第九篇】:剖析装饰器
    Python基础篇【第八篇】:剖析递归函数
    Python基础篇【第七篇】:文件操作
    Python基础篇【第六篇】:函数补充
    Python基础篇【第五篇】:sed和函数
    TCP/IP详解之:Ping程序、Traceroute程序
    TCP/IP详解之:ICMP协议
    TCP/IP详解之:ARP协议 和 RARP协议
  • 原文地址:https://www.cnblogs.com/a-du/p/7278146.html
Copyright © 2011-2022 走看看