我正在使用 Elastic Search 开发一个 .NET 应用程序。我使用ES River对数据进行索引。 结果(在 Sense 中)看起来有点像这样:
{
"_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 相同的结果,但是当我搜索年份、地点、度量等时,查询不会返回任何结果,但是它应该(Sense 确实并且应该)。 像这样的查询有效(并返回正确的结果):
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一起使用,这未分析
,这意味着它们只会找到完全匹配的内容。如果您没有在映射中显式指定分析器,Elasticsearch 默认为 standard analyzer .
当您使用查询字符串在 Elasticsearch 中执行搜索时,就像您在 Sense 中所做的那样:GET _search/?q=Place:London
,a query 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/