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
  • 相关阅读:
    第三次作业——for 语句及分支结构else-if
    第二次作业及总结——数据类型和运算符
    .net webapi 过滤器使用(异常日志)
    .net webapi 过滤器使用(记录访问日志)
    how to get state when push notification
    Qt项目下pro文件设置库路径
    配置安装oh-my-bash
    03、工具篇--Maven
    04、JavaEE--SSH整合
    01、JavaEE--Hibernate
  • 原文地址:https://www.cnblogs.com/a-du/p/7278146.html
Copyright © 2011-2022 走看看