简体   繁体   English

巢式查询会传回奇怪的结果。 elasticsearch

[英]nest query returns weird results. elasticsearch

This is my client code: 这是我的客户代码:

settings 设置

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");

executing code 执行代码

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());
}

Indexing is launched at application start: 索引在应用程序启动时启动:

foreach(var project in this.dbContext.Projects) {
    var indexModel = Mapper.Map<ProjectIndexModel>(project);
    searchClient.Index(indexModel, "projects", "project", indexModel.Id.ToString());
}

Indexes are present in database (that's not quite the same as I have now, but the schema stayed the same). 索引存在于数据库中(这与我现在的不完全相同,但是架构保持不变)。

what have I tried : 我尝试了什么

Controller action returns (default) 10 hits of 11 documents. 控制器操作将返回(默认)11个文档的10个匹配项。 It's like searching is ignored entirely, without visible errors. 就像搜索被完全忽略,没有可见错误。

Fiddler gave positive result (1 hit) both for {host:9200}/_search and {host:9200}/projects/project/_search POST requests with query: 对于{host:9200}/_search{host:9200}/projects/project/_search POST请求, Fiddler给出了积极的结果(1个命中结果):

{
    "query": {
        "query_string": {
            "query": "original"
        }
    }
}

What's the problem? 有什么问题?

Problem was not in NEST . 问题不在NEST After exploring the results.ConnectionStatus.ToString() , which showed a hit as Fiddler did, I found that problem was in my client code. 探索了results.ConnectionStatus.ToString() (显示了Fiddler成功后)后,我发现问题出在我的客户端代码中。 I overlooked that I was sending POST without {searchTerm: $scope.searchTerm} specified: 我忽略了我未指定{searchTerm: $scope.searchTerm}发送POST情况:

$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);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM