• 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解释器
    js子节点children和childnodes的用法
    添加jar包需注意
    Class.forName("com.mysql.jdbc.driver");
    java集合类总结
    interface思考练习一
    java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
    Struts2的配置文件中, <package>的作用,<action><result>重名?
    在Struts2的Action中获得request response session几种方法
    学习一直都是一个相见恨晚的过程,我希望我的相见恨晚不会太晚。
  • 原文地址:https://www.cnblogs.com/a-du/p/7278146.html
Copyright © 2020-2023  润新知