# Programmatically overriding query parameters from external source (setSource/setExtraSource)

**URL:** <https://discuss.elastic.co/t/programmatically-overriding-query-parameters-from-external-source-setsource-setextrasource/19911>\
**Category:** Elasticsearch\
**Created:** [September 22, 2014, 12:07pm UTC](https://discuss.elastic.co/t/programmatically-overriding-query-parameters-from-external-source-setsource-setextrasource/19911 "2014-09-22T12:07:30Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![CoSee](https://avatars.discourse-cdn.com/v4/letter/c/b38774/32.png) [@CoSee](https://discuss.elastic.co/u/CoSee)\
**Post date:** [September 22, 2014, 12:07pm UTC](https://discuss.elastic.co/t/programmatically-overriding-query-parameters-from-external-source-setsource-setextrasource/19911/1 "2014-09-22T12:07:30Z")

</div>

Hello,

I am using the Java API and loading a complex JSON formatted query from a  
text file and using setSource() of the SearchRequestBuilder  
to prepare the search. What I like to do now is programmatically overwrite  
or change values loaded form source using the Java API, i.e.

json query source:  
{  
...  
"sort" : [  
"\_score"  
],  
....  
}

SearchRequestBuilder search =  
client.prepareSearch("indexname").setSource(json.toString());  
search.addSort(SortBuilders.fieldSort("somefield").order(SortOrder.ASC);

I would expect an effective query like:

{  
...  
"sort" : [  
"\_score",  
{ "somefield" : "asc"}  
],  
....  
}

However, it does not seem to work this way. Debugging the request object  
yields something like:

* * *

## indexname!🙂 �sort��somefield�orderBasc���� { ... "sort" : ["\_score"], ....

Which I would interpret as that the programmatically set values for "sort"  
being overwritten by the setSource JSON following after.  
I have since found that setting the field "sort" programmatically only  
works as I expect it if the JSON source does not contain it at all.

I've tried to use setExtraSource instead of setSource without success.

Is it even possible what I am trying to accomplish here? Or do I need to  
find another approach (like using templates, or building the full query  
programmatically?)

Thanks  
Andreas

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/6a6d683b-ac50-4abe-9141-af60f7a810c4%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/6a6d683b-ac50-4abe-9141-af60f7a810c4%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 6, 2017, 1:00am UTC](https://discuss.elastic.co/t/programmatically-overriding-query-parameters-from-external-source-setsource-setextrasource/19911/2 "2017-07-06T01:00:41Z")

</div>


