question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How can I access script fields in queries ?

See original GitHub issue

NEST/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:closed
  • Created 3 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
russcamcommented, Jul 30, 2020

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

var client = new ElasticClient();
	
var searchResponse = client.Search<Document>(s => s
	.Query(q => q.MatchAll())
);

foreach(var hit in searchResponse.Hits)
{
	var fields = hit.Fields;
	var myDistance = fields.Value<double>("mydistance");
}

or using the shorthand

var client = new ElasticClient();
	
var searchResponse = client.Search<Document>(s => s
	.Query(q => q.MatchAll())
);

foreach(var fields in searchResponse.Fields)
{
	var myDistance = fields.Value<double>("mydistance");
}

Take a look at the Script fields docs for more info.

0reactions
TanujChauhan07commented, Aug 3, 2022

@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 }

Read more comments on GitHub >

github_iconTop Results From Across the Web

Accessing document fields and special variables - Scripting
Field values can be accessed from a script using doc-values, the _source field, or stored fields, each of which is explained below. Accessing...
Read more >
How can I access script fields in queries made with NEST ...
While running this query directly against elasticsearch seems fine, when I do it NEST I have a few issues: I cannot access the...
Read more >
Script query | Elasticsearch Guide [8.9]
You write a script to create field values and they are available everywhere, such as fields , all queries, and aggregations. Filters documents...
Read more >
Elasticsearch Runtime Fields- How to Use Them ...
Script fields, moreover, have nothing to do with the mappings. Script fields only exist in the query context in a very rigid way....
Read more >
Script Fields
Script fields can work on fields that are not stored ( my_field_name in the above case), and allow to return custom values to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found