我在使用Nest API在Elastic中使用同义词时遇到了麻烦。
我已经设置了我的索引和所有相应的设置,但是当我查询一个应该是同义词的术语时,结果看起来好像根本没有被应用。这是我的设置:
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")
));
我索引的对象看起来像这样:
[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; }
}
我试图让字段JATitle和JABody与同义词对齐。
任何想法都会受到欢迎。
谢谢, ScrappyT
答案 0 :(得分:1)
您已正确创建了令牌过滤器,但未将其添加到自定义分析器的过滤器中。
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" });
希望它有所帮助。