我很难使用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与同义词对齐。
任何想法肯定会受到欢迎。
谢谢,
废话
最佳答案
您已正确创建 token 过滤器,但未将其添加到自定义分析器的过滤器中。
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"
});
希望能帮助到你。
关于c# - 通过Nest在Elasticsearch中使用同义词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37835700/