简体   繁体   English

通过 Nest 在 Elasticsearch 中使用同义词

[英]Using Synonyms in Elasticsearch via Nest

I'm having real trouble getting synonyms to work in Elastic using the Nest API.我在使用 Nest API 使同义词在 Elastic 中工作时遇到了真正的麻烦。

I've set up my index and all of the appropriate settings, but when I query based a term that should be a synonym, the results appear as if they haven't been applied at all.我已经设置了我的索引和所有适当的设置,但是当我基于一个应该是同义词的术语进行查询时,结果看起来好像根本没有应用它们。 Here is my set up:这是我的设置:

m_objNode = new Uri(Properties.Settings.Default.strLocalElasticSearchURL);
m_objConnectionSettings = new ConnectionSettings(m_objNode, defaultIndex: "myIndex");
m_objElasticClient = new ElasticClient(m_objConnectionSettings);

IndexSettings indexSettings = new IndexSettings();
indexSettings.NumberOfReplicas = 1;
indexSettings.NumberOfShards = 1;

CustomAnalyzer exclamation = new CustomAnalyzer();
exclamation.Tokenizer = "exclamationTokenizer";

indexSettings.Analysis.Tokenizers.Add("exclamationTokenizer", new PatternTokenizer {
    Pattern = @"!"
});

indexSettings.Analysis.Analyzers.Add("exclamation", exclamation);
indexSettings.Analysis.TokenFilters.Add("synonym", new SynonymTokenFilter { Synonyms = new[] { "tire => tyre", "aluminum => aluminium" }, IgnoreCase = true, Tokenizer = "whitespace" });

m_objElasticClient.CreateIndex(c => c
    .Index("myIndex")
    .InitializeUsing(indexSettings)
    .AddMapping<myClass>(m => m
        .MapFromAttributes()
        .IndexAnalyzer("english")
        .SearchAnalyzer("english")                              
        ));

And the objects I'm indexing look like this:我索引的对象如下所示:

[ElasticType(IdProperty = "JAUniqueKey")]
public class myClass {

    public string JAUniqueKey { get; set; }
    public int JAItemID { get; set; }
    public string JATitle { get; set; }
    public string JABody { get; set; }
}

I'm trying to get the fields JATitle and JABody to be aligned with the synonyms.我试图让字段 JTitle 和 JABody 与同义词对齐。

Any ideas sure would be welcome.任何想法肯定会受到欢迎。

Thanks, ScrappyT谢谢, ScrappyT

You've created token filters correctly but you didn't add it into filters for your custom analyzer.您已正确创建令牌过滤器,但未将其添加到自定义分析器的过滤器中。

IndexSettings indexSettings = new IndexSettings();
indexSettings.NumberOfReplicas = 1;
indexSettings.NumberOfShards = 1;

CustomAnalyzer exclamation = new CustomAnalyzer();
exclamation.Tokenizer = "exclamationTokenizer";
exclamation.Filter = new List<string> {"synonym"};
indexSettings.Analysis.Tokenizers.Add(
   "exclamationTokenizer",
    new PatternTokenizer { });

indexSettings.Analysis.Analyzers.Add("exclamation", exclamation);
indexSettings.Analysis.TokenFilters.Add(
    "synonym",
    new SynonymTokenFilter
    {
        Synonyms = new[] { "tire => tyre", "aluminum => aluminium" },
        IgnoreCase = true,
        Tokenizer = "whitespace"
    });

Hope it helps.希望它有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM