How can I access script fields in queries ?
See original GitHub issueNEST/Elasticsearch.Net version: 7.8.0
Elasticsearch version: 7.6.1 (Docker)
Description of the problem including expected versus actual behavior: Hi, I simply would like to get access to a script field computed to get the arcDistance from a Geopoint mapped field and a supplied coordinates.
Steps to reproduce: Here is the raw query generated by my NEST search (it works well)
GET offices/_search
{
"size": 100,
"_source": [
"name",
],
"script_fields": {
"mydistance": {
"script": "doc['location'].arcDistance(48.858, 2.21)"
}
},
"query": {
"bool": {
"must": [
...
],
"filter": {
"geo_distance": {
"distance": "5000m",
"location": {
"lat": 48.858,
"lon": 2.21
}
}
}
}
},
"sort": [
{
"_geo_distance": {
"location": {
"lat": 48.858,
"lon": 2.21
},
"order": "asc"
}
}
]
}
Results in (mydistance field does appear as expected)
"hits" : [
{
"_index" : "offices",
"_type" : "_doc",
"_id" : "off1",
"_score" : null,
"_source" : {
"name" : "OFFICE 1"
},
"fields" : {
"mydistance" : [
265.0396545087881
]
},
"sort" : [
265.0396545087881
]
...
}
]
My POCO used to index document
public class Offices
{
public string Id{ get; set; }
public string Name { get; set; }
public GeoLocation Location { get; set; }
}
When the query is performed, the script field is returned (raw JSON), but how to declare this extra field so that it can be serialized into my POCO but ignored when I index a document (as it’s computed by Elastic, not by me) ?
Expected behavior Get the way to serialize a script field in a POCO.
Issue Analytics
- State:
- Created 3 years ago
- Comments:5 (1 by maintainers)
Top GitHub Comments
Hi @NicolasReyDotNet,
TDocument
maps to the_source
returned for each search hit, which is the verbatim JSON that was sent to Elasticsearch.Script fields can be accessed on the response in a similar way to stored fields, with
or using the shorthand
Take a look at the Script fields docs for more info.
@NicolasReyDotNet
I am stuck in Nest Query.
I need below json response in NEST Query, Please help me out Thanks in advance. { “from”: 0, “script_fields”: { “distance”: { “script”: “doc[‘location’].distanceInKm(28.2487,77.0635)” } }, “size”: 100 }