Skip to content

Do I define a runtime field in the search request to carry the parameters of the script? #2303

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
cglcl opened this issue Sep 22, 2022 · 3 comments · Fixed by #2677
Closed
Labels
type: enhancement A general enhancement

Comments

@cglcl
Copy link

cglcl commented Sep 22, 2022

Recently, I was using ElasticsearchRestTemplate and found that it seems that the parameters of the script cannot be carried when defining runtime fields in a search request. What should I do to carry the parameters?
Like this script:

double price = 0;
for (int i = 0; i < params._source.pool_quotas.length; ++i) {
            if (params._source.pool_quotas[i].pool_id==params.pool_id){
                       price += params._source.pool_quotas[i].hard_limit;
            }
}
emit(price);

it looks:

{
	"from": 0,
	"size": 10,
	"query": {
		"bool": {
			"should": [{
				"term": {
					"name": {
						"value": "wangfei",
						"boost": 1.0
					}
				}
			}],
			"adjust_pure_negative": true,
			"minimum_should_match": "1",
			"boost": 1.0
		}
	},
	"version": true,
	"explain": false,
	"runtime_mappings": {
		"duration": {
			"type": "double",
			"script": {
				"source": "double price = 0;for (int i = 0; i < params._source.pool_quotas.length; ++i) {if (params._source.pool_quotas[i].pool_id==params.pool_id){price += params._source.pool_quotas[i].hard_limit;}}emit(price);",
				"lang": "painless",
				"params": {
					"pool_id": "cddaca80-6480-45af-85c2-b09577db4396"
				}
			}
		}
	}
}

How can I implement a query like this?Thanks!

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Sep 22, 2022
@cglcl
Copy link
Author

cglcl commented Sep 22, 2022

Maybe I have solved it, I inherited the class RuntimeField.

public class RuntimeFieldParam extends RuntimeField {

    private final String lang;
    private final Map<String, Object> params;

    public RuntimeFieldParam(String name, String type, String script,
                             String lang, Map<String,Object> params) {
        super(name, type, script);
        this.lang = lang;
        this.params = params;
    }

    @Nonnull
    @Override
    public Map<String, Object> getMapping() {
        Map<String, Object> map = super.getMapping();
        Object script = map.get("script");
        Map<String, Object> newScript = new HashMap<>();
        newScript.put("source", script);
        newScript.put("lang", this.lang);
        newScript.put("params", this.params);
        map.put("script", newScript);
        return map;
    }
}

Is there any other way besides this method?

@sothawo
Copy link
Collaborator

sothawo commented Sep 22, 2022

Seems not currently. This would need to be added as an addition to the RuntimeField class.

@sothawo sothawo added type: enhancement A general enhancement and removed status: waiting-for-triage An issue we've not yet triaged labels Sep 22, 2022
@cglcl
Copy link
Author

cglcl commented Sep 22, 2022

OK, so I'll implement it like this for now.

sothawo added a commit to sothawo/spring-data-elasticsearch that referenced this issue Aug 26, 2023
sothawo added a commit that referenced this issue Aug 26, 2023
@sothawo sothawo added this to the 5.2 M3 (2023.1.0) milestone Aug 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: enhancement A general enhancement
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants