gpt4 book ai didi

c# - 在查询某些字段时,使用 NEST 进行搜索不会返回结果

转载 作者:太空宇宙 更新时间:2023-11-03 10:42:06 24 4
gpt4 key购买 nike

我正在使用 Elastic Search 开发 .NET 应用程序。我使用 ES River 来索引数据。结果(从某种意义上说)看起来有点像这样:

 {
"_index": "musicstore",
"_type": "songs",
"_id": "J2k-NjXjRa-mgWKAq0RMlw",
"_score": 1,
"_source": {
"songID": 42,
"tempo": "andante",
"metrum": "3/4 E8",
"intonation": "F",
"title": "Song",
"archiveSongNumber": "3684",
"Year": 2000,
"Place": "London"
}
},

要访问索引数据,我使用与此类似的 NEST 查询:

var result = ElasticClient.Search<Song>(s => s.Query(q => q.Term(p => p.title, "Song")));

我遇到一个问题,当我搜索某个字段时,查询没有返回任何结果。例如,当我搜索标题、歌曲 ID、节奏或 archiveSongNumber 时,查询工作正常并且返回与 Sense 相同的结果,但是当我搜索 Year、Place、metrum 等时,查询不返回任何结果,但是它应该(感觉确实应该)。这些查询有效(并返回正确的结果):

var result = ElasticClient.Search<Song>(s => s.Query(q => q.Term(p => p.title, "Song")));        
var result = ElasticClient.Search<Song>(s => s.Query(q => q.Term(p => p.songID, 42)));
var result = ElasticClient.Search<Song>(s => s.Query(q => q.Term(p => p.archiveSongNumber , "3684")));

像这样的查询不会返回任何结果(但它们应该):

var result = ElasticClient.Search<Song>(s => s.Query(q => q.Term(p => p.Place, "London")));
var result = ElasticClient.Search<Song>(s => s.Query(q => q.Term(p => p.Year, 2000)));

我做错了什么?我在索引数据时搞砸了吗?

更新:映射看起来像这样:

{
"musicstore": {
"mappings": {
"songs": {
"properties": {
"Year": {
"type": "long"
},
"Place": {
"type": "string"
},
"archiveSongNumber": {
"type": "string"
},
"songID": {
"type": "long"
},
"intonation": {
"type": "string"
},
"metrum": {
"type": "string"
},
"title": {
"type": "string"
},
"tempo": {
"type": "string"
}
}
}
}
}
}

更新 2:

ES 河流请求如下所示:

PUT /_river/songs_river/_meta
{
"type":"jdbc",
"jdbc": {
"driver":"com.microsoft.sqlserver.jdbc.SQLServerDriver",
"url":"jdbc:sqlserver://ip_address:1433;databaseName=database",
"user":"user",
"password":"password",
"strategy":"simple",
"poll":"300s",
"autocommit":true,
"fetchsize":10,
"max_retries":3,
"max_retries_wait":"10s",
"index":"musicstore",
"type":"songs",
"analysis": {
"analyzer" :{
"whitespace" :{
"type" : "whitespace",
"filter":"lowercase"
}
}
},
"sql":"some_sql_query"
}
}

ES 客户端配置如下所示:

private static ElasticClient ElasticClient
{
get
{
Uri localhost = new Uri("http://localhost:9200");
var setting = new ConnectionSettings(localhost);
setting.SetDefaultIndex("musicstore").MapDefaultTypeNames(d => d.Add(typeof(Song), "songs"));
setting.SetConnectionStatusHandler(c =>
{
if (!c.Success)
throw new Exception(c.ToString());
});
return new ElasticClient(setting);
}
}

最佳答案

从您的映射来看,这里的问题很可能是您的所有字段在索引时都被分析,但是您正在使用带有 NEST 的 term queries,这未分析,这意味着它们只会找到完全匹配的结果。如果您没有在映射中明确指定分析器,Elasticsearch 默认为 standard analyzer .

当您使用查询字符串在 Elasticsearch 中执行搜索时,就像您在 Sense 中所做的那样:GET _search/?q=Place:Londonquery string query是由 Elasticsearch 运行的,它不同于 term query .

但是从您的示例来看,您实际上并没有使用 query string syntax .你可能想要一个 match query相反:

client.Search<Song>(s => s
.Query(q => q
.Match(m => m
.OnField(p => p.Place)
.Query("London")
)
)
);

如果您确实想要一个查询字符串查询,就像您正在使用 Sense 执行的那样,那么您可以使用 QueryString:

client.Search<Song>(s => s
.Query(q => q
.QueryString(qs => qs
.OnFields(p => p.Place)
.Query("London")
)
)
);

希望对您有所帮助。我建议查看 getting started guide ,特别是关于 exact values vs. full text 的部分.

关于c# - 在查询某些字段时,使用 NEST 进行搜索不会返回结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24984071/

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