这是我的客户代码:
设置
var defaultSettings = new ConnectionSettings(
uri: new System.Uri("http://localhost:9200")
);
defaultSettings.SetJsonSerializerSettingsModifier(s => {
s.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
});
defaultSettings.SetConnectionStatusHandler(c => {
if(!c.Success) throw new Exception(c.ToString());
});
defaultSettings.SetDefaultIndex("projects");
执行代码
public ActionResult Search(String searchTerm) {
var result = this.searchClient.Search<ProjectIndexModel>(
descriptor: new SearchDescriptor<ProjectIndexModel>().Index("projects").AllTypes().Query(
query: q => q.QueryString(qs => qs.Query(searchTerm)
)
));
// or
/*
var results = this.searchClient.Search<ProjectIndexModel>(s =>
s.Index("projects").Type("project").Query(q =>
q.Term(f => f.ProblemDefinition, searchTerm) ||
q.Term(f => f.Name, searchTerm) ||
q.Term(f => f.SuggestedSolution, searchTerm) ||
q.Term(f => f.Initiator, searchTerm)
)
);
*/
return Json(result.Documents.ToList());
}
索引在应用程序启动时启动:
foreach(var project in this.dbContext.Projects) {
var indexModel = Mapper.Map<ProjectIndexModel>(project);
searchClient.Index(indexModel, "projects", "project", indexModel.Id.ToString());
}
Indexes存在于数据库中(这与我现在的不完全相同,但是架构保持不变)。
我尝试了什么:
Controller 操作返回(默认) 10次命中11个文档的。就像搜索被完全忽略,没有可见错误。
Fiddler
对{host:9200}/_search
和{host:9200}/projects/project/_search
都给出了肯定的结果(命中1个结果)带有查询的POST 请求:{
"query": {
"query_string": {
"query": "original"
}
}
}
有什么问题?
最佳答案
问题不在NEST
中。探索了results.ConnectionStatus.ToString()
,发现它像Fiddler
一样成功后,我发现问题出在我的客户代码中。我忽略了我在未指定POST
的情况下发送{searchTerm: $scope.searchTerm}
的方法:
$http({
url: '/projects/search',
method: "POST",
data: { searchTerm: $scope.searchTerm }
})
.success(function(data, status, headers, config) {
$scope.projects = data.documents;
}).error(function(data, status, headers, config) {
console.log('error: ' + data);
});
关于c# - 巢状查询会传回奇怪的结果。 Elasticsearch ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24283543/