gpt4 book ai didi

elasticsearch - 如何使用Elasticsearch搜索所有同义词?

转载 作者:行者123 更新时间:2023-12-02 23:20:01 24 4
gpt4 key购买 nike

到目前为止,我已经尝试使用Solr格式的文件进行此操作:

zavesa => Gotove zavese,
zavesa, blago => Blago in dekorativno blago,
zavesa => Dodatki za zavese,
zavesa => Drogi in vodila za zavese


zavesa => Gotove zavese, Blago in dekorativno blago, Drogi in vodila za zavese, Dodatki za zavese

但是我总是只得到与“Drogi in vodila za zavese”匹配的结果。但是,如果我删除了“Drogi in vodila za zavese”:
zavesa => Gotove zavese, Blago in dekorativno blago, Dodatki za zavese

那么我只会得到“Blago in dekorativno blago”的结果。

我也尝试使用所有小写字母:
zavesa => gotove zavese, blago in dekorativno blago, drogi in vodila za zavese, dodatki za zavese

结果是一样的。

当有人搜索“zavesa”时,我想获取“Gotove zavese”,“Blago in dekorativno blago”,“Drogi in vodila za zavese”和“Dodatki za zavese”的结果。

Elasticsearch有可能吗?

我的同义词配置
var indexSettings = new IndexSettings
{
NumberOfReplicas = 0, // If this is set to 1 or more, then the index becomes yellow, because it's running on a single node (development machine).
NumberOfShards = 5
};

indexSettings.Analysis = new Analysis();
indexSettings.Analysis.Analyzers = new Analyzers();
indexSettings.Analysis.TokenFilters = new TokenFilters();

var listOfSynonyms = new[] {
"zavesa => Gotove zavese, Blago in dekorativno blago, Drogi in vodila za zavese, Dodatki za zavese"
};

var customTokenFilterSynonyms = new SynonymTokenFilter
{
Synonyms = listOfSynonyms,
Format = SynonymFormat.Solr,
Tokenizer = "standard"
};

indexSettings.Analysis.TokenFilters.Add("customTokenFilterSynonym", customTokenFilterSynonyms);

CustomAnalyzer customAnalyzer = new CustomAnalyzer
{
Tokenizer = "standard",
Filter = new List<string> { "lowercase", "asciifolding", "customTokenFilterSynonym" }
};

indexSettings.Analysis.Analyzers.Add("customAnalyzerLowercaseSynonymAsciifolding", customAnalyzer);

var indexConfig = new IndexState
{
Settings = indexSettings
};

var request = new IndexExistsRequest(indexName);
var result = ElasticClient.IndexExists(request);

if (!result.Exists)
{
var response = ElasticClient.CreateIndex(indexName, c => c
.InitializeUsing(indexConfig)
.Mappings(m => m
.Map<ChildGroupModel>(mm => mm
.Properties(p => p
.Completion(cp => cp
.Name(elasticsearchModel => elasticsearchModel.TitleAutSuggest)
.Analyzer("customAnalyzerLowercaseSynonymAsciifolding")
.SearchAnalyzer("customAnalyzerLowercaseSynonymAsciifolding")
)
.Completion(cp => cp
.Name(elasticsearchModel => elasticsearchModel.TitleSloSuggest)
.Analyzer("customAnalyzerLowercaseSynonymAsciifolding")
.SearchAnalyzer("customAnalyzerLowercaseSynonymAsciifolding")
)
.Completion(cp => cp
.Name(elasticsearchModel => elasticsearchModel.TitleItaSuggest)
.Analyzer("customAnalyzerLowercaseSynonymAsciifolding")
.SearchAnalyzer("customAnalyzerLowercaseSynonymAsciifolding")
)
.Text(t => t.Name(model => model.TitleAut).Analyzer("customAnalyzerLowercaseSynonymAsciifolding").SearchAnalyzer("customAnalyzerLowercaseSynonymAsciifolding"))
.Text(t => t.Name(model => model.TitleSlo).Analyzer("customAnalyzerLowercaseSynonymAsciifolding").SearchAnalyzer("customAnalyzerLowercaseSynonymAsciifolding"))
.Text(t => t.Name(model => model.TitleIta).Analyzer("customAnalyzerLowercaseSynonymAsciifolding").SearchAnalyzer("customAnalyzerLowercaseSynonymAsciifolding"))
)
)
)
);

}

我正在 TitleSloSuggest字段上对此进行测试。

模型
public class ChildGroupModel
{
[Column("id")]
public int Id { get; set; }

[Column("homepage_groups_id")]
public int GroupId { get; set; }

[Column("title_aut")]
public string TitleAut { get; set; }

public CompletionField TitleAutSuggest
{
get
{
return new CompletionField
{
Input = new[] { TitleAut }
};
}
}

[Column("title_slo")]
public string TitleSlo { get; set; }

public CompletionField TitleSloSuggest
{
get
{
return new CompletionField
{
Input = new[] { TitleSlo }
};
}
}

[Column("title_ita")]
public string TitleIta { get; set; }

public CompletionField TitleItaSuggest
{
get
{
return new CompletionField
{
Input = new[] { TitleIta }
};
}
}




}

这些是索引设置:
// 20180601112924
// http://localhost:9200/child_groups_index/_settings

{
"child_groups_index_temp_1": {
"settings": {
"index": {
"number_of_shards": "5",
"provided_name": "child_groups_index_temp_1",
"creation_date": "1527844777425",
"analysis": {
"filter": {
"customTokenFilterSynonym": {
"format": "solr",
"type": "synonym",
"synonyms": [
"zavesa => gotove zavese, blago in dekorativno blago, drogi in vodila za zavese, dodatki za zavese"
],
"tokenizer": "standard"
}
},
"analyzer": {
"customAnalyzerLowercaseSynonymAsciifolding": {
"filter": [
"lowercase",
"asciifolding",
"customTokenFilterSynonym"
],
"type": "custom",
"tokenizer": "standard"
}
}
},
"number_of_replicas": "0",
"uuid": "WsHzMHm-QSKA4Xzxp02ipQ",
"version": {
"created": "6020399"
}
}
}
}
}

最佳答案

我发现自己在做什么错。该链接帮助:https://www.elastic.co/guide/en/elasticsearch/guide/current/multi-word-synonyms.html#_use_simple_contraction_for_phrase_queries

代替:

"zavesa => gotove zavese, blago in dekorativno blago, dodatki za zavese, drogi in vodila za zavese"

我需要:
"gotove zavese, blago in dekorativno blago, dodatki za zavese, drogi in vodila za zavese => zavesa"

要么:
"gotove zavese => zavesa",
"blago in dekorativno blago => zavesa",
"dodatki za zavese => zavesa",
"drogi in vodila za zavese => zavesa"

关于elasticsearch - 如何使用Elasticsearch搜索所有同义词?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50623115/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com