gpt4 book ai didi

elasticsearch - 如何从Elasticsearch响应读取距离

转载 作者:行者123 更新时间:2023-12-03 00:52:31 37 4
gpt4 key购买 nike

我正在使用Elasticsearch V6,NEST V6。

我正在如下搜索ES,并且正在使用ScriptFields来计算距离并将其包括在结果中。

var searchResponse = _elasticClient.Search<MyDocument>(new SearchRequest<MyDocument>
{
Query = new BoolQuery
{
Must = new QueryContainer[] { matchQuery },
Filter = new QueryContainer[] { filterQuery },
},
Source = new SourceFilter
{
Includes = resultFields // fields to be included in the result
},
ScriptFields = new ScriptField
{
Script = new InlineScript("doc['geoLocation'].planeDistance(params.lat, params.lng) * 0.001") // divide by 1000 to convert to km
{
Lang = "painless",
Params = new FluentDictionary<string, object>
{
{ "lat", _center.Latitude },
{ "lng", _center.Longitude }
}
}
}
});

现在,我尝试读取搜索结果,但不确定如何读取响应的距离,这就是我尝试过的方法:
// this is how I read the Document, all OK here
var docs = searchResponse.Documents.ToList<MyDocument>();

// this is my attempt to read the distance from the result
var hits = searchResponse.Hits;
foreach (var h in hits)
{
var d = h.Fields["distance"];
// d is of type Nest.LazyDocument
// I am not sure how to get the distance value from object of type LazyDocument
}

在调试时,我可以看到距离值,但是我不确定如何读取该值?

enter image description here

最佳答案

我找到了答案here

要阅读搜索文档和距离:

foreach (var hit in searchResponse.Hits)
{
MyDocument doc = hit.Source;
double distance = hit.Fields.Value<double>("distance");
}

如果您只对距离感兴趣:
foreach (var fieldValues in searchResponse.Fields)
{
var distance = fieldValues.Value<double>("distance");
}

关于elasticsearch - 如何从Elasticsearch响应读取距离,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51152147/

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