Global variable in script?

Hi all,
I want to count the number of elements that have distance greater than 10. I don't know how to save the variable that counts. I can't initialize a variable in the script because at each iteration of elements, the variable will be reset.

here is a part of my query:
"script_fields" : {
"distance" : {
"script" : {
"source": "doc['location'].arcDistance(params.lat,params.lon>10) * 0.001",
"params": {
"lat": 50,
"lon": 50
}
}
}
}
I tried with type_count too but I don't know how to keep only true values of distance.
How can I solve my problem ?

Scripts are "stateless" by design. There is no guaranteed order of iterating over documents in lucene, and this can change even if the same documents match a query just by merges happening in the background. So stateful scripts could have weird and hard to debug consequences.

With that said, let's see if we can find a way for you to solve your problem. When you say "the number off elements" do you mean the number of documents? If so, then it seems like script field will not help you, since this is field generated for each document returned in a search response. Instead, have you considered doing a geo distance query on your location field and looking at the total number of hits?

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.