gpt4 book ai didi

c# - Elasticsearch - 排序搜索结果 ASC

转载 作者:行者123 更新时间:2023-11-30 17:43:26 27 4
gpt4 key购买 nike

我的 elasticsearch 有问题。

设置:拥有一个带有数据字段“companyName”的公司类。我的搜索将搜索并响应所有具有搜索词的公司。

如果我尝试通过排序

.Sort(x=> x.OnField(x => x.CompanyName).Descending())

数据未正确排序 - 引用 stackOverflow

我尝试了给定的解决方案,但如果我将公司名称设置为“not_analyzed”,我什至无法再搜索公司名称或类似开头的“goo”(谷歌)所以我尝试建立一个多字段映射,一个后缀,一个不分析,一个分析。

我的索引设置如下:

  client.CreateIndex(IndexName, c => c       
.AddMapping<Exhibitor>(m =>m
.MapFromAttributes()
.Properties(o => o
.MultiField(mf=>mf
.Name(x=>x.CompanyName)
.Fields(fs => fs
.String(s=>s.Name(t=>t.CompanyName).Index(FieldIndexOption.Analyzed).Analyzer("standard"))
.String(s=>s.Name(t=>t.CompanyName.Suffix("raw")).Index(FieldIndexOption.NotAnalyzed))))
)

)
)
);

我的搜索看起来像:

string SearchTerm ="my search term"
results = GetClient().Search<Company>(s => s
.Query(qa => qa
.MatchPhrasePrefix(m => m
.OnField(f=>f.CompanyName)
.Query(SearchTerm)
))

.Sort(x => x.OnField(x => x.CompanyName.Suffix("raw")).Descending())

.Size(maxResults).Skip(page * pageSize).Take(pageSize)

);

但是还是不行。有什么想法吗?

提前致谢。

更新 1:

对于不区分大小写的排序,我添加了一个自定义分析器:

var companyAnalyzer = new CustomAnalyzer
{
Filter = new List<string> { "standard", "lowercase" },
Tokenizer = "keyword"
};
client.CreateIndex(IndexName, c => c
.Analysis(analysis => analysis
.Analyzers(a => a
.Add("companyanalyzer", companyAnalyzer)
)
)
.AddMapping<Exhibitor>(m => m
.MapFromAttributes()
.Properties(o => o
.MultiField(mf => mf
.Name(x => x.CompanyName)
.Fields(fs => fs
.String(s => s.Name(t => t.CompanyName).Index(FieldIndexOption.Analyzed).Analyzer("standard"))
.String(s => s.Name(t => t.CompanyName.Suffix("raw")).Index(FieldIndexOption.Analyzed).Analyzer("companyanalyzer"))))
)

)
);

最佳答案

这个例子是有效的,也许它会阐明你的问题。

var indicesResponse = client.DeleteIndex(descriptor => descriptor.Index(indexName));

client.CreateIndex(indexName, c => c
.AddMapping<Exhibitor>(m => m
.MapFromAttributes()
.Properties(o => o
.MultiField(mf => mf
.Name(x => x.CompanyName)
.Fields(fs => fs
.String(s => s.Name(t => t.CompanyName).Index(FieldIndexOption.Analyzed).Analyzer("standard"))
.String(s => s.Name(t => t.CompanyName.Suffix("raw")).Index(FieldIndexOption.NotAnalyzed))))
)));


client.Index(new Exhibitor { Id = 1, CompanyName = "a test" });
client.Index(new Exhibitor { Id = 2, CompanyName = "b test" });
client.Index(new Exhibitor { Id = 3, CompanyName = "c test" });

client.Refresh();

string SearchTerm = "test";
var results = client.Search<Exhibitor>(s => s
.Query(qa => qa
.MatchPhrasePrefix(m => m
.OnField(f => f.CompanyName)
.Query(SearchTerm)
))
.Sort(x => x.OnField(f => f.CompanyName.Suffix("raw")).Descending())
);

查询结果:

{
"took": 2,
"timed_out": false,
"_shards": {..}
"hits": {
"total": 3,
"max_score": null,
"hits": [
{
"_index": "my_index",
"_type": "exhibitor",
"_id": "3",
"_score": null,
"_source": {
"id": 3,
"companyName": "c test"
},
"sort": [
"c test"
]
},
{
"_index": "my_index",
"_type": "exhibitor",
"_id": "2",
"_score": null,
"_source": {
"id": 2,
"companyName": "b test"
},
"sort": [
"b test"
]
},
{
"_index": "my_index",
"_type": "exhibitor",
"_id": "1",
"_score": null,
"_source": {
"id": 1,
"companyName": "a test"
},
"sort": [
"a test"
]
}
]
}
}

索引映射:

{
"my_index" : {
"mappings" : {
"exhibitor" : {
"properties" : {
"companyName" : {
"type" : "string",
"analyzer" : "standard",
"fields" : {
"raw" : {
"type" : "string",
"index" : "not_analyzed"
}
}
},
"id" : {
"type" : "integer"
}
}
}
}
}
}

希望对您有所帮助。

关于c# - Elasticsearch - 排序搜索结果 ASC,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30910395/

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