# Sorting on a custom script field in Java

**URL:** <https://discuss.elastic.co/t/sorting-on-a-custom-script-field-in-java/10912>\
**Category:** Elasticsearch\
**Created:** [February 26, 2013, 10:30pm UTC](https://discuss.elastic.co/t/sorting-on-a-custom-script-field-in-java/10912 "2013-02-26T22:30:46Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![echin1999\_2](https://avatars.discourse-cdn.com/v4/letter/e/f4b2a3/32.png) [@echin1999\_2](https://discuss.elastic.co/u/echin1999_2)\
**Post date:** [February 26, 2013, 10:30pm UTC](https://discuss.elastic.co/t/sorting-on-a-custom-script-field-in-java/10912/1 "2013-02-26T22:30:46Z")

</div>

Hi,

I was wondering if someone could point me to some sample Java code that  
specifically shows how to sort on a custom script field.

I see that the SearchRequestBuilder has a method called: addScriptField(name,  
script, params);

but i'm not sure how to define the "script" argument. I assume the "name"  
is the reference to the script field that i would pass into the  
construction of a FieldSortBuilder()?

I want to be able to sort my results based on whether or not a property (or  
multiple properties) of type long[] contains a long value specified at  
search time (ie. documents where the criteria holds true will be returned  
ahead of those documents where the criteria is false).

I've attached my sample code which does basic sorting on an existing field

- that seems to work fine. I now just want to enhance it to be able to  
sort on a custom script field coded as i describe above. Any pointers are  
greatly appreciated!

thanks

Ed

--  
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).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Igor\_Motov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/igor_motov/32/45193_2.png) [@Igor\_Motov](https://discuss.elastic.co/u/Igor_Motov)\
**Post date:** [February 27, 2013, 3:11pm UTC](https://discuss.elastic.co/t/sorting-on-a-custom-script-field-in-java/10912/2 "2013-02-27T15:11:52Z")

</div>

You cannot sort on a script field, but you can use a script to provide a  
value to sort on. For example the following script will bring the record  
with the value 42 in the field my\_field to the first position.

.addSort(SortBuilders  
.scriptSort("doc["my\_field"].value == my\_val? 1 : 0", "number")  
.order(SortOrder.DESC)  
.setParams(MapBuilder.\<String, Object\>newMapBuilder().put("my\_val",  
42).map()))

While you can do it with a script sort, if all you need is to boost records  
with certain values, you might achieve much better performance using a Custom  
Filters Score Query[http://www.elasticsearch.org/guide/reference/query-dsl/custom-filters-score-query.html](http://www.elasticsearch.org/guide/reference/query-dsl/custom-filters-score-query.html) and  
adding \_score as one of the sort value.

On Tuesday, February 26, 2013 5:30:46 PM UTC-5, echin1999 wrote:

> Hi,
> 
> I was wondering if someone could point me to some sample Java code that  
> specifically shows how to sort on a custom script field.
> 
> I see that the SearchRequestBuilder has a method called: addScriptField(name,  
> script, params);
> 
> but i'm not sure how to define the "script" argument. I assume the  
> "name" is the reference to the script field that i would pass into the  
> construction of a FieldSortBuilder()?
> 
> I want to be able to sort my results based on whether or not a property  
> (or multiple properties) of type long contains a long value specified at  
> search time (ie. documents where the criteria holds true will be returned  
> ahead of those documents where the criteria is false).
> 
> I've attached my sample code which does basic sorting on an existing field
> 
> - that seems to work fine. I now just want to enhance it to be able to  
> sort on a custom script field coded as i describe above. Any pointers are  
> greatly appreciated!
> 
> thanks
> 
> Ed

--  
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).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![echin1999\_2](https://avatars.discourse-cdn.com/v4/letter/e/f4b2a3/32.png) [@echin1999\_2](https://discuss.elastic.co/u/echin1999_2)\
**Post date:** [February 27, 2013, 11:13pm UTC](https://discuss.elastic.co/t/sorting-on-a-custom-script-field-in-java/10912/3 "2013-02-27T23:13:00Z")

</div>

Hi Igor,

thanks for the tip.. I added the following code to my test program  
(essentially wrapping the filter i already had with a custom fiilter score):

qb =  
QueryBuilders.customFiltersScoreQuery(qb).add(FilterBuilders.termFilter("ln",  
someValue\_2), 100);

and added a sort on the \_score, and it seems to work. (boost the score to  
100 if field "ln" contained someValue\_2, otherwise not). Is that the  
correct usage, or is there a better way that doesn't require wrapping the  
existing filter?

Another thing i noticed is that when I iterated through the hits, and  
printed the score, I would get a NaN for the score if I added a secondary  
sort against another field. If just a single sort by score, then the score  
printed fine. Is that expected behavior?

Thanks  
Ed

On Wednesday, February 27, 2013 10:11:52 AM UTC-5, Igor Motov wrote:

> You cannot sort on a script field, but you can use a script to provide a  
> value to sort on. For example the following script will bring the record  
> with the value 42 in the field my\_field to the first position.
> 
> .addSort(SortBuilders  
> .scriptSort("doc["my\_field"].value == my\_val? 1 : 0", "number")  
> .order(SortOrder.DESC)  
> .setParams(MapBuilder.\<String,  
> Object\>newMapBuilder().put("my\_val", 42).map()))
> 
> While you can do it with a script sort, if all you need is to boost  
> records with certain values, you might achieve much better performance  
> using a Custom Filters Score Query[http://www.elasticsearch.org/guide/reference/query-dsl/custom-filters-score-query.html](http://www.elasticsearch.org/guide/reference/query-dsl/custom-filters-score-query.html) and  
> adding \_score as one of the sort value.
> 
> On Tuesday, February 26, 2013 5:30:46 PM UTC-5, echin1999 wrote:
> 
> > Hi,
> > 
> > I was wondering if someone could point me to some sample Java code that  
> > specifically shows how to sort on a custom script field.
> > 
> > I see that the SearchRequestBuilder has a method called: addScriptField(name,  
> > script, params);
> > 
> > but i'm not sure how to define the "script" argument. I assume the  
> > "name" is the reference to the script field that i would pass into the  
> > construction of a FieldSortBuilder()?
> > 
> > I want to be able to sort my results based on whether or not a property  
> > (or multiple properties) of type long contains a long value specified at  
> > search time (ie. documents where the criteria holds true will be returned  
> > ahead of those documents where the criteria is false).
> > 
> > I've attached my sample code which does basic sorting on an existing  
> > field - that seems to work fine. I now just want to enhance it to be  
> > able to sort on a custom script field coded as i describe above. Any  
> > pointers are greatly appreciated!
> > 
> > thanks
> > 
> > Ed

--  
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).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Igor\_Motov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/igor_motov/32/45193_2.png) [@Igor\_Motov](https://discuss.elastic.co/u/Igor_Motov)\
**Post date:** [February 28, 2013, 1:15am UTC](https://discuss.elastic.co/t/sorting-on-a-custom-script-field-in-java/10912/4 "2013-02-28T01:15:04Z")

</div>

Yes, that's correct usage. Could you provide a complete example of the  
issue with NaN?

On Wednesday, February 27, 2013 6:13:00 PM UTC-5, echin1999 wrote:

> Hi Igor,
> 
> thanks for the tip.. I added the following code to my test program  
> (essentially wrapping the filter i already had with a custom fiilter score):
> 
> qb =  
> QueryBuilders.customFiltersScoreQuery(qb).add(FilterBuilders.termFilter(  
> "ln", someValue\_2), 100);
> 
> and added a sort on the \_score, and it seems to work. (boost the score to  
> 100 if field "ln" contained someValue\_2, otherwise not). Is that the  
> correct usage, or is there a better way that doesn't require wrapping the  
> existing filter?
> 
> Another thing i noticed is that when I iterated through the hits, and  
> printed the score, I would get a NaN for the score if I added a secondary  
> sort against another field. If just a single sort by score, then the score  
> printed fine. Is that expected behavior?
> 
> Thanks  
> Ed
> 
> On Wednesday, February 27, 2013 10:11:52 AM UTC-5, Igor Motov wrote:
> 
> > You cannot sort on a script field, but you can use a script to provide a  
> > value to sort on. For example the following script will bring the record  
> > with the value 42 in the field my\_field to the first position.
> > 
> > .addSort(SortBuilders  
> > .scriptSort("doc["my\_field"].value == my\_val? 1 : 0", "number")  
> > .order(SortOrder.DESC)  
> > .setParams(MapBuilder.\<String,  
> > Object\>newMapBuilder().put("my\_val", 42).map()))
> > 
> > While you can do it with a script sort, if all you need is to boost  
> > records with certain values, you might achieve much better performance  
> > using a Custom Filters Score Query[http://www.elasticsearch.org/guide/reference/query-dsl/custom-filters-score-query.html](http://www.elasticsearch.org/guide/reference/query-dsl/custom-filters-score-query.html) and  
> > adding \_score as one of the sort value.
> > 
> > On Tuesday, February 26, 2013 5:30:46 PM UTC-5, echin1999 wrote:
> > 
> > > Hi,
> > > 
> > > I was wondering if someone could point me to some sample Java code that  
> > > specifically shows how to sort on a custom script field.
> > > 
> > > I see that the SearchRequestBuilder has a method called: addScriptField(name,  
> > > script, params);
> > > 
> > > but i'm not sure how to define the "script" argument. I assume the  
> > > "name" is the reference to the script field that i would pass into the  
> > > construction of a FieldSortBuilder()?
> > > 
> > > I want to be able to sort my results based on whether or not a property  
> > > (or multiple properties) of type long contains a long value specified at  
> > > search time (ie. documents where the criteria holds true will be returned  
> > > ahead of those documents where the criteria is false).
> > > 
> > > I've attached my sample code which does basic sorting on an existing  
> > > field - that seems to work fine. I now just want to enhance it to be  
> > > able to sort on a custom script field coded as i describe above. Any  
> > > pointers are greatly appreciated!
> > > 
> > > thanks
> > > 
> > > Ed

--  
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).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![echin1999\_2](https://avatars.discourse-cdn.com/v4/letter/e/f4b2a3/32.png) [@echin1999\_2](https://discuss.elastic.co/u/echin1999_2)\
**Post date:** [February 28, 2013, 2:59am UTC](https://discuss.elastic.co/t/sorting-on-a-custom-script-field-in-java/10912/5 "2013-02-28T02:59:48Z")

</div>

Here's a downsized version of the code (see attachment on original post for  
complete version except that attachment only has one sort):

qb =  
QueryBuilders.filteredQuery(QueryBuilders.matchAllQuery(),FilterBuilders.termFilter(  
"cmp", "1"));

SearchRequestBuilder srb = client.prepareSearch().setQuery(qb);

I create two sortBuilders...

FieldSortBuilder sortBuilder = new FieldSortBuilder("\_score");

sortBuilder.ignoreUnmapped(true);

sortBuilder.order(SortOrder.DESC);

FieldSortBuilder sortBuilder2 = new FieldSortBuilder("ln");

sortBuilder2.ignoreUnmapped(true);

sortBuilder2.order(SortOrder.ASC);

If I only use the first sortBuilder, i get no issues… (score = 1)

but if i use both (uncomment the second line here:)

srb.addSort(sortBuilder);

// srb.addSort(sortBuilder2);

// execute

SearchResponse response = srb.execute().actionGet();

Then I get NaN for the hit.getScore() - please note, however, the sorting  
appears to be fine - hard to tell for sure though.

// print response

SearchHits hits = response.getHits();

for(SearchHit hit: hits)

{

```
System.out.println(hit.getScore()+":"+hit.getSourceAsString()); 

```

}

The document looks like:

{"tgl":["pension","fUNd","funkychickeEN"],"ln":"Chin","gui":[5,6],"d":"my  
description at Sat Feb 16 18:11:20 EST  
2013","dur":180,"nui":[5],"n":"MyPublishTest","pdt":1361056280000,"cmp":1,"fls":{"mvd":1361056280761,"pvd":1361056280762,"ort":3,"ori":1361056280763},"own":5,"by":"Chin  
Ed","cid":28,"fn":"Ed"}

Thanks

Ed

On Wednesday, February 27, 2013 8:15:04 PM UTC-5, Igor Motov wrote:

> Yes, that's correct usage. Could you provide a complete example of the  
> issue with NaN?
> 
> On Wednesday, February 27, 2013 6:13:00 PM UTC-5, echin1999 wrote:
> 
> > Hi Igor,
> > 
> > thanks for the tip.. I added the following code to my test program  
> > (essentially wrapping the filter i already had with a custom fiilter score):
> > 
> > qb =  
> > QueryBuilders.customFiltersScoreQuery(qb).add(FilterBuilders.termFilter(  
> > "ln", someValue\_2), 100);
> > 
> > and added a sort on the \_score, and it seems to work. (boost the score to  
> > 100 if field "ln" contained someValue\_2, otherwise not). Is that the  
> > correct usage, or is there a better way that doesn't require wrapping the  
> > existing filter?
> > 
> > Another thing i noticed is that when I iterated through the hits, and  
> > printed the score, I would get a NaN for the score if I added a secondary  
> > sort against another field. If just a single sort by score, then the score  
> > printed fine. Is that expected behavior?
> > 
> > Thanks  
> > Ed
> > 
> > On Wednesday, February 27, 2013 10:11:52 AM UTC-5, Igor Motov wrote:
> > 
> > > You cannot sort on a script field, but you can use a script to provide a  
> > > value to sort on. For example the following script will bring the record  
> > > with the value 42 in the field my\_field to the first position.
> > > 
> > > .addSort(SortBuilders  
> > > .scriptSort("doc["my\_field"].value == my\_val? 1 : 0", "number")  
> > > .order(SortOrder.DESC)  
> > > .setParams(MapBuilder.\<String,  
> > > Object\>newMapBuilder().put("my\_val", 42).map()))
> > > 
> > > While you can do it with a script sort, if all you need is to boost  
> > > records with certain values, you might achieve much better performance  
> > > using a Custom Filters Score Query[http://www.elasticsearch.org/guide/reference/query-dsl/custom-filters-score-query.html](http://www.elasticsearch.org/guide/reference/query-dsl/custom-filters-score-query.html) and  
> > > adding \_score as one of the sort value.
> > > 
> > > On Tuesday, February 26, 2013 5:30:46 PM UTC-5, echin1999 wrote:
> > > 
> > > > Hi,
> > > > 
> > > > I was wondering if someone could point me to some sample Java code that  
> > > > specifically shows how to sort on a custom script field.
> > > > 
> > > > I see that the SearchRequestBuilder has a method called: addScriptField(name,  
> > > > script, params);
> > > > 
> > > > but i'm not sure how to define the "script" argument. I assume the  
> > > > "name" is the reference to the script field that i would pass into the  
> > > > construction of a FieldSortBuilder()?
> > > > 
> > > > I want to be able to sort my results based on whether or not a property  
> > > > (or multiple properties) of type long contains a long value specified at  
> > > > search time (ie. documents where the criteria holds true will be returned  
> > > > ahead of those documents where the criteria is false).
> > > > 
> > > > I've attached my sample code which does basic sorting on an existing  
> > > > field - that seems to work fine. I now just want to enhance it to be  
> > > > able to sort on a custom script field coded as i describe above. Any  
> > > > pointers are greatly appreciated!
> > > > 
> > > > thanks
> > > > 
> > > > Ed

--  
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).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Igor\_Motov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/igor_motov/32/45193_2.png) [@Igor\_Motov](https://discuss.elastic.co/u/Igor_Motov)\
**Post date:** [February 28, 2013, 2:44pm UTC](https://discuss.elastic.co/t/sorting-on-a-custom-script-field-in-java/10912/6 "2013-02-28T14:44:25Z")

</div>

I cannot reproduce it for some reason. Here's what I tried:

```
@Test
public void searchSort() {
    Node node = startNode("node1");

    try {

```

node.client().admin().indices().prepareDelete("test").execute().actionGet();  
} catch (Exception e) {  
// ignore  
}  
node.client().admin().indices().prepareCreate("test")

.setSettings(settingsBuilder().put("index.number\_of\_shards",  
1)).execute().actionGet();

```
    for (int i = 0; i < 20; i++) {
        node.client().prepareIndex("test", "type1", Integer.toString(i))
                .setSource("field", "test " + i, "num", 

```

i).execute().actionGet();

```
    }

```

node.client().admin().indices().prepareRefresh().execute().actionGet();

```
    SearchResponse searchResponse = node.client().prepareSearch("test")
            .setQuery(QueryBuilders.matchQuery("field", "test 10"))

```

.addSort(SortBuilders.fieldSort("\_score").ignoreUnmapped(true).order(SortOrder.DESC))

.addSort(SortBuilders.fieldSort("num").ignoreUnmapped(true).order(SortOrder.ASC))  
.execute().actionGet();  
assertThat(searchResponse.getHits().totalHits(), equalTo(20l));

```
    assertThat(searchResponse.getHits().hits()[0].getScore(), 

```

greaterThan(0.0f));

```
}

```

Could you modify this test in such a way that it reproduces the issue?  
Which version of elasticsearch are you using?

On Wednesday, February 27, 2013 9:59:48 PM UTC-5, echin1999 wrote:

> Here's a downsized version of the code (see attachment on original post  
> for complete version except that attachment only has one sort):
> 
> qb =  
> QueryBuilders.filteredQuery(QueryBuilders.matchAllQuery(),FilterBuilders.termFilter(  
> "cmp", "1"));
> 
> SearchRequestBuilder srb = client.prepareSearch().setQuery(qb);
> 
> I create two sortBuilders...
> 
> FieldSortBuilder sortBuilder = new FieldSortBuilder("\_score");
> 
> sortBuilder.ignoreUnmapped(true);
> 
> sortBuilder.order(SortOrder.DESC);
> 
> FieldSortBuilder sortBuilder2 = new FieldSortBuilder("ln");
> 
> sortBuilder2.ignoreUnmapped(true);
> 
> sortBuilder2.order(SortOrder.ASC);
> 
> If I only use the first sortBuilder, i get no issues… (score = 1)
> 
> but if i use both (uncomment the second line here:)
> 
> srb.addSort(sortBuilder);
> 
> // srb.addSort(sortBuilder2);
> 
> // execute
> 
> SearchResponse response = srb.execute().actionGet();
> 
> Then I get NaN for the hit.getScore() - please note, however, the  
> sorting appears to be fine - hard to tell for sure though.
> 
> // print response
> 
> SearchHits hits = response.getHits();
> 
> for(SearchHit hit: hits)
> 
> {
> 
> ```
> System.out.println(hit.getScore()+":"+hit.getSourceAsString()); 
> 
> ```
> 
> }
> 
> The document looks like:
> 
> {"tgl":["pension","fUNd","funkychickeEN"],"ln":"Chin","gui":[5,6],"d":"my  
> description at Sat Feb 16 18:11:20 EST  
> 2013","dur":180,"nui":[5],"n":"MyPublishTest","pdt":1361056280000,"cmp":1,"fls":{"mvd":1361056280761,"pvd":1361056280762,"ort":3,"ori":1361056280763},"own":5,"by":"Chin  
> Ed","cid":28,"fn":"Ed"}
> 
> Thanks
> 
> Ed
> 
> On Wednesday, February 27, 2013 8:15:04 PM UTC-5, Igor Motov wrote:
> 
> > Yes, that's correct usage. Could you provide a complete example of the  
> > issue with NaN?
> > 
> > On Wednesday, February 27, 2013 6:13:00 PM UTC-5, echin1999 wrote:
> > 
> > > Hi Igor,
> > > 
> > > thanks for the tip.. I added the following code to my test program  
> > > (essentially wrapping the filter i already had with a custom fiilter score):
> > > 
> > > qb =  
> > > QueryBuilders.customFiltersScoreQuery(qb).add(FilterBuilders.termFilter(  
> > > "ln", someValue\_2), 100);
> > > 
> > > and added a sort on the \_score, and it seems to work. (boost the score  
> > > to 100 if field "ln" contained someValue\_2, otherwise not). Is that the  
> > > correct usage, or is there a better way that doesn't require wrapping the  
> > > existing filter?
> > > 
> > > Another thing i noticed is that when I iterated through the hits, and  
> > > printed the score, I would get a NaN for the score if I added a secondary  
> > > sort against another field. If just a single sort by score, then the score  
> > > printed fine. Is that expected behavior?
> > > 
> > > Thanks  
> > > Ed
> > > 
> > > On Wednesday, February 27, 2013 10:11:52 AM UTC-5, Igor Motov wrote:
> > > 
> > > > You cannot sort on a script field, but you can use a script to provide  
> > > > a value to sort on. For example the following script will bring the record  
> > > > with the value 42 in the field my\_field to the first position.
> > > > 
> > > > .addSort(SortBuilders  
> > > > .scriptSort("doc["my\_field"].value == my\_val? 1 : 0",  
> > > > "number")  
> > > > .order(SortOrder.DESC)  
> > > > .setParams(MapBuilder.\<String,  
> > > > Object\>newMapBuilder().put("my\_val", 42).map()))
> > > > 
> > > > While you can do it with a script sort, if all you need is to boost  
> > > > records with certain values, you might achieve much better performance  
> > > > using a Custom Filters Score Query[http://www.elasticsearch.org/guide/reference/query-dsl/custom-filters-score-query.html](http://www.elasticsearch.org/guide/reference/query-dsl/custom-filters-score-query.html) and  
> > > > adding \_score as one of the sort value.
> > > > 
> > > > On Tuesday, February 26, 2013 5:30:46 PM UTC-5, echin1999 wrote:
> > > > 
> > > > > Hi,
> > > > > 
> > > > > I was wondering if someone could point me to some sample Java code  
> > > > > that specifically shows how to sort on a custom script field.
> > > > > 
> > > > > I see that the SearchRequestBuilder has a method called: addScriptField(name,  
> > > > > script, params);
> > > > > 
> > > > > but i'm not sure how to define the "script" argument. I assume the  
> > > > > "name" is the reference to the script field that i would pass into the  
> > > > > construction of a FieldSortBuilder()?
> > > > > 
> > > > > I want to be able to sort my results based on whether or not a  
> > > > > property (or multiple properties) of type long contains a long value  
> > > > > specified at search time (ie. documents where the criteria holds true will  
> > > > > be returned ahead of those documents where the criteria is false).
> > > > > 
> > > > > I've attached my sample code which does basic sorting on an existing  
> > > > > field - that seems to work fine. I now just want to enhance it to be  
> > > > > able to sort on a custom script field coded as i describe above. Any  
> > > > > pointers are greatly appreciated!
> > > > > 
> > > > > thanks
> > > > > 
> > > > > Ed

--  
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).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![echin1999\_2](https://avatars.discourse-cdn.com/v4/letter/e/f4b2a3/32.png) [@echin1999\_2](https://discuss.elastic.co/u/echin1999_2)\
**Post date:** [February 28, 2013, 3:08pm UTC](https://discuss.elastic.co/t/sorting-on-a-custom-script-field-in-java/10912/7 "2013-02-28T15:08:16Z")

</div>

I'm currently using Version: 0.20.2. Wow. I see there is 0.20.5  
available - i will upgrade to that to see if there's a difference.

as for trying to reproduce it using your test code, could you provide me a  
pointer to the rest of the code that has some of the supporting convenience  
methods (ie. startNode, etc).

thanks

On Thursday, February 28, 2013 9:44:25 AM UTC-5, Igor Motov wrote:

> I cannot reproduce it for some reason. Here's what I tried:
> 
> ```
> @Test
> public void searchSort() {
> Node node = startNode("node1");
> 
> try {
> 
> ```
> 
> node.client().admin().indices().prepareDelete("test").execute().actionGet();  
> } catch (Exception e) {  
> // ignore  
> }  
> node.client().admin().indices().prepareCreate("test")
> 
> .setSettings(settingsBuilder().put("index.number\_of\_shards",  
> 1)).execute().actionGet();
> 
> ```
> for (int i = 0; i < 20; i++) {
> node.client().prepareIndex("test", "type1", 
> 
> ```
> 
> Integer.toString(i))  
> .setSource("field", "test " + i, "num",  
> i).execute().actionGet();
> 
> ```
> }
> 
> ```
> 
> node.client().admin().indices().prepareRefresh().execute().actionGet();
> 
> ```
> SearchResponse searchResponse = node.client().prepareSearch("test")
> .setQuery(QueryBuilders.matchQuery("field", "test 10"))
> 
> ```
> 
> .addSort(SortBuilders.fieldSort("\_score").ignoreUnmapped(true).order(SortOrder.DESC))
> 
> .addSort(SortBuilders.fieldSort("num").ignoreUnmapped(true).order(SortOrder.ASC))  
> .execute().actionGet();  
> assertThat(searchResponse.getHits().totalHits(), equalTo(20l));
> 
> ```
> assertThat(searchResponse.getHits().hits()[0].getScore(), 
> 
> ```
> 
> greaterThan(0.0f));
> 
> ```
> }
> 
> ```
> 
> Could you modify this test in such a way that it reproduces the issue?  
> Which version of elasticsearch are you using?
> 
> On Wednesday, February 27, 2013 9:59:48 PM UTC-5, echin1999 wrote:
> 
> > Here's a downsized version of the code (see attachment on original post  
> > for complete version except that attachment only has one sort):
> > 
> > qb =  
> > QueryBuilders.filteredQuery(QueryBuilders.matchAllQuery(),FilterBuilders.termFilter(  
> > "cmp", "1"));
> > 
> > SearchRequestBuilder srb = client.prepareSearch().setQuery(qb);
> > 
> > I create two sortBuilders...
> > 
> > FieldSortBuilder sortBuilder = new FieldSortBuilder("\_score");
> > 
> > sortBuilder.ignoreUnmapped(true);
> > 
> > sortBuilder.order(SortOrder.DESC);
> > 
> > FieldSortBuilder sortBuilder2 = new FieldSortBuilder("ln");
> > 
> > sortBuilder2.ignoreUnmapped(true);
> > 
> > sortBuilder2.order(SortOrder.ASC);
> > 
> > If I only use the first sortBuilder, i get no issues… (score = 1)
> > 
> > but if i use both (uncomment the second line here:)
> > 
> > srb.addSort(sortBuilder);
> > 
> > // srb.addSort(sortBuilder2);
> > 
> > // execute
> > 
> > SearchResponse response = srb.execute().actionGet();
> > 
> > Then I get NaN for the hit.getScore() - please note, however, the  
> > sorting appears to be fine - hard to tell for sure though.
> > 
> > // print response
> > 
> > SearchHits hits = response.getHits();
> > 
> > for(SearchHit hit: hits)
> > 
> > {
> > 
> > ```
> > System.out.println(hit.getScore()+":"+hit.getSourceAsString()); 
> > 
> > ```
> > 
> > }
> > 
> > The document looks like:
> > 
> > {"tgl":["pension","fUNd","funkychickeEN"],"ln":"Chin","gui":[5,6],"d":"my  
> > description at Sat Feb 16 18:11:20 EST  
> > 2013","dur":180,"nui":[5],"n":"MyPublishTest","pdt":1361056280000,"cmp":1,"fls":{"mvd":1361056280761,"pvd":1361056280762,"ort":3,"ori":1361056280763},"own":5,"by":"Chin  
> > Ed","cid":28,"fn":"Ed"}
> > 
> > Thanks
> > 
> > Ed
> > 
> > On Wednesday, February 27, 2013 8:15:04 PM UTC-5, Igor Motov wrote:
> > 
> > > Yes, that's correct usage. Could you provide a complete example of the  
> > > issue with NaN?
> > > 
> > > On Wednesday, February 27, 2013 6:13:00 PM UTC-5, echin1999 wrote:
> > > 
> > > > Hi Igor,
> > > > 
> > > > thanks for the tip.. I added the following code to my test program  
> > > > (essentially wrapping the filter i already had with a custom fiilter score):
> > > > 
> > > > qb =  
> > > > QueryBuilders.customFiltersScoreQuery(qb).add(FilterBuilders.termFilter(  
> > > > "ln", someValue\_2), 100);
> > > > 
> > > > and added a sort on the \_score, and it seems to work. (boost the score  
> > > > to 100 if field "ln" contained someValue\_2, otherwise not). Is that the  
> > > > correct usage, or is there a better way that doesn't require wrapping the  
> > > > existing filter?
> > > > 
> > > > Another thing i noticed is that when I iterated through the hits, and  
> > > > printed the score, I would get a NaN for the score if I added a secondary  
> > > > sort against another field. If just a single sort by score, then the score  
> > > > printed fine. Is that expected behavior?
> > > > 
> > > > Thanks  
> > > > Ed
> > > > 
> > > > On Wednesday, February 27, 2013 10:11:52 AM UTC-5, Igor Motov wrote:
> > > > 
> > > > > You cannot sort on a script field, but you can use a script to provide  
> > > > > a value to sort on. For example the following script will bring the record  
> > > > > with the value 42 in the field my\_field to the first position.
> > > > > 
> > > > > .addSort(SortBuilders  
> > > > > .scriptSort("doc["my\_field"].value == my\_val? 1 : 0",  
> > > > > "number")  
> > > > > .order(SortOrder.DESC)  
> > > > > .setParams(MapBuilder.\<String,  
> > > > > Object\>newMapBuilder().put("my\_val", 42).map()))
> > > > > 
> > > > > While you can do it with a script sort, if all you need is to boost  
> > > > > records with certain values, you might achieve much better performance  
> > > > > using a Custom Filters Score Query[http://www.elasticsearch.org/guide/reference/query-dsl/custom-filters-score-query.html](http://www.elasticsearch.org/guide/reference/query-dsl/custom-filters-score-query.html) and  
> > > > > adding \_score as one of the sort value.
> > > > > 
> > > > > On Tuesday, February 26, 2013 5:30:46 PM UTC-5, echin1999 wrote:
> > > > > 
> > > > > > Hi,
> > > > > > 
> > > > > > I was wondering if someone could point me to some sample Java code  
> > > > > > that specifically shows how to sort on a custom script field.
> > > > > > 
> > > > > > I see that the SearchRequestBuilder has a method called: addScriptField(name,  
> > > > > > script, params);
> > > > > > 
> > > > > > but i'm not sure how to define the "script" argument. I assume the  
> > > > > > "name" is the reference to the script field that i would pass into the  
> > > > > > construction of a FieldSortBuilder()?
> > > > > > 
> > > > > > I want to be able to sort my results based on whether or not a  
> > > > > > property (or multiple properties) of type long contains a long value  
> > > > > > specified at search time (ie. documents where the criteria holds true will  
> > > > > > be returned ahead of those documents where the criteria is false).
> > > > > > 
> > > > > > I've attached my sample code which does basic sorting on an existing  
> > > > > > field - that seems to work fine. I now just want to enhance it to be  
> > > > > > able to sort on a custom script field coded as i describe above. Any  
> > > > > > pointers are greatly appreciated!
> > > > > > 
> > > > > > thanks
> > > > > > 
> > > > > > Ed

--  
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).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![echin1999\_2](https://avatars.discourse-cdn.com/v4/letter/e/f4b2a3/32.png) [@echin1999\_2](https://discuss.elastic.co/u/echin1999_2)\
**Post date:** [February 28, 2013, 4:05pm UTC](https://discuss.elastic.co/t/sorting-on-a-custom-script-field-in-java/10912/8 "2013-02-28T16:05:57Z")

</div>

so.. upgrading to 0.20.5 didn't seem to help. However I've observed an  
unusual pattern.

I am running a single elasticsearch process locally. If I restart the  
Elasticsearch process (kill and start), the problem goes away. If i  
restart again, the problem comes back. This alternating behavior repeats  
for each restart. It makes no difference if i run in the foreground (-f)  
or not. Also, if i run in the foreground, i don't see any errors.

Are there specific debug settings to tweak to perhaps get more information  
as to what is going on?

On Thursday, February 28, 2013 9:44:25 AM UTC-5, Igor Motov wrote:

> I cannot reproduce it for some reason. Here's what I tried:
> 
> ```
> @Test
> public void searchSort() {
> Node node = startNode("node1");
> 
> try {
> 
> ```
> 
> node.client().admin().indices().prepareDelete("test").execute().actionGet();  
> } catch (Exception e) {  
> // ignore  
> }  
> node.client().admin().indices().prepareCreate("test")
> 
> .setSettings(settingsBuilder().put("index.number\_of\_shards",  
> 1)).execute().actionGet();
> 
> ```
> for (int i = 0; i < 20; i++) {
> node.client().prepareIndex("test", "type1", 
> 
> ```
> 
> Integer.toString(i))  
> .setSource("field", "test " + i, "num",  
> i).execute().actionGet();
> 
> ```
> }
> 
> ```
> 
> node.client().admin().indices().prepareRefresh().execute().actionGet();
> 
> ```
> SearchResponse searchResponse = node.client().prepareSearch("test")
> .setQuery(QueryBuilders.matchQuery("field", "test 10"))
> 
> ```
> 
> .addSort(SortBuilders.fieldSort("\_score").ignoreUnmapped(true).order(SortOrder.DESC))
> 
> .addSort(SortBuilders.fieldSort("num").ignoreUnmapped(true).order(SortOrder.ASC))  
> .execute().actionGet();  
> assertThat(searchResponse.getHits().totalHits(), equalTo(20l));
> 
> ```
> assertThat(searchResponse.getHits().hits()[0].getScore(), 
> 
> ```
> 
> greaterThan(0.0f));
> 
> ```
> }
> 
> ```
> 
> Could you modify this test in such a way that it reproduces the issue?  
> Which version of elasticsearch are you using?
> 
> On Wednesday, February 27, 2013 9:59:48 PM UTC-5, echin1999 wrote:
> 
> > Here's a downsized version of the code (see attachment on original post  
> > for complete version except that attachment only has one sort):
> > 
> > qb =  
> > QueryBuilders.filteredQuery(QueryBuilders.matchAllQuery(),FilterBuilders.termFilter(  
> > "cmp", "1"));
> > 
> > SearchRequestBuilder srb = client.prepareSearch().setQuery(qb);
> > 
> > I create two sortBuilders...
> > 
> > FieldSortBuilder sortBuilder = new FieldSortBuilder("\_score");
> > 
> > sortBuilder.ignoreUnmapped(true);
> > 
> > sortBuilder.order(SortOrder.DESC);
> > 
> > FieldSortBuilder sortBuilder2 = new FieldSortBuilder("ln");
> > 
> > sortBuilder2.ignoreUnmapped(true);
> > 
> > sortBuilder2.order(SortOrder.ASC);
> > 
> > If I only use the first sortBuilder, i get no issues… (score = 1)
> > 
> > but if i use both (uncomment the second line here:)
> > 
> > srb.addSort(sortBuilder);
> > 
> > // srb.addSort(sortBuilder2);
> > 
> > // execute
> > 
> > SearchResponse response = srb.execute().actionGet();
> > 
> > Then I get NaN for the hit.getScore() - please note, however, the  
> > sorting appears to be fine - hard to tell for sure though.
> > 
> > // print response
> > 
> > SearchHits hits = response.getHits();
> > 
> > for(SearchHit hit: hits)
> > 
> > {
> > 
> > ```
> > System.out.println(hit.getScore()+":"+hit.getSourceAsString()); 
> > 
> > ```
> > 
> > }
> > 
> > The document looks like:
> > 
> > {"tgl":["pension","fUNd","funkychickeEN"],"ln":"Chin","gui":[5,6],"d":"my  
> > description at Sat Feb 16 18:11:20 EST  
> > 2013","dur":180,"nui":[5],"n":"MyPublishTest","pdt":1361056280000,"cmp":1,"fls":{"mvd":1361056280761,"pvd":1361056280762,"ort":3,"ori":1361056280763},"own":5,"by":"Chin  
> > Ed","cid":28,"fn":"Ed"}
> > 
> > Thanks
> > 
> > Ed
> > 
> > On Wednesday, February 27, 2013 8:15:04 PM UTC-5, Igor Motov wrote:
> > 
> > > Yes, that's correct usage. Could you provide a complete example of the  
> > > issue with NaN?
> > > 
> > > On Wednesday, February 27, 2013 6:13:00 PM UTC-5, echin1999 wrote:
> > > 
> > > > Hi Igor,
> > > > 
> > > > thanks for the tip.. I added the following code to my test program  
> > > > (essentially wrapping the filter i already had with a custom fiilter score):
> > > > 
> > > > qb =  
> > > > QueryBuilders.customFiltersScoreQuery(qb).add(FilterBuilders.termFilter(  
> > > > "ln", someValue\_2), 100);
> > > > 
> > > > and added a sort on the \_score, and it seems to work. (boost the score  
> > > > to 100 if field "ln" contained someValue\_2, otherwise not). Is that the  
> > > > correct usage, or is there a better way that doesn't require wrapping the  
> > > > existing filter?
> > > > 
> > > > Another thing i noticed is that when I iterated through the hits, and  
> > > > printed the score, I would get a NaN for the score if I added a secondary  
> > > > sort against another field. If just a single sort by score, then the score  
> > > > printed fine. Is that expected behavior?
> > > > 
> > > > Thanks  
> > > > Ed
> > > > 
> > > > On Wednesday, February 27, 2013 10:11:52 AM UTC-5, Igor Motov wrote:
> > > > 
> > > > > You cannot sort on a script field, but you can use a script to provide  
> > > > > a value to sort on. For example the following script will bring the record  
> > > > > with the value 42 in the field my\_field to the first position.
> > > > > 
> > > > > .addSort(SortBuilders  
> > > > > .scriptSort("doc["my\_field"].value == my\_val? 1 : 0",  
> > > > > "number")  
> > > > > .order(SortOrder.DESC)  
> > > > > .setParams(MapBuilder.\<String,  
> > > > > Object\>newMapBuilder().put("my\_val", 42).map()))
> > > > > 
> > > > > While you can do it with a script sort, if all you need is to boost  
> > > > > records with certain values, you might achieve much better performance  
> > > > > using a Custom Filters Score Query[http://www.elasticsearch.org/guide/reference/query-dsl/custom-filters-score-query.html](http://www.elasticsearch.org/guide/reference/query-dsl/custom-filters-score-query.html) and  
> > > > > adding \_score as one of the sort value.
> > > > > 
> > > > > On Tuesday, February 26, 2013 5:30:46 PM UTC-5, echin1999 wrote:
> > > > > 
> > > > > > Hi,
> > > > > > 
> > > > > > I was wondering if someone could point me to some sample Java code  
> > > > > > that specifically shows how to sort on a custom script field.
> > > > > > 
> > > > > > I see that the SearchRequestBuilder has a method called: addScriptField(name,  
> > > > > > script, params);
> > > > > > 
> > > > > > but i'm not sure how to define the "script" argument. I assume the  
> > > > > > "name" is the reference to the script field that i would pass into the  
> > > > > > construction of a FieldSortBuilder()?
> > > > > > 
> > > > > > I want to be able to sort my results based on whether or not a  
> > > > > > property (or multiple properties) of type long contains a long value  
> > > > > > specified at search time (ie. documents where the criteria holds true will  
> > > > > > be returned ahead of those documents where the criteria is false).
> > > > > > 
> > > > > > I've attached my sample code which does basic sorting on an existing  
> > > > > > field - that seems to work fine. I now just want to enhance it to be  
> > > > > > able to sort on a custom script field coded as i describe above. Any  
> > > > > > pointers are greatly appreciated!
> > > > > > 
> > > > > > thanks
> > > > > > 
> > > > > > Ed

--  
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).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Igor\_Motov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/igor_motov/32/45193_2.png) [@Igor\_Motov](https://discuss.elastic.co/u/Igor_Motov)\
**Post date:** [February 28, 2013, 9:06pm UTC](https://discuss.elastic.co/t/sorting-on-a-custom-script-field-in-java/10912/9 "2013-02-28T21:06:49Z")

</div>

You can include the the test that I posted class that I sent you into a  
class inherited from AbstractNodesTests[https://github.com/elasticsearch/elasticsearch/blob/master/src/test/java/org/elasticsearch/test/integration/AbstractNodesTests.java](https://github.com/elasticsearch/elasticsearch/blob/master/src/test/java/org/elasticsearch/test/integration/AbstractNodesTests.java).  
Can you enable TRACE logging on your elasticsearch server, run your test  
and post the log somewhere?

On Thursday, February 28, 2013 11:05:57 AM UTC-5, echin1999 wrote:

> so.. upgrading to 0.20.5 didn't seem to help. However I've observed an  
> unusual pattern.
> 
> I am running a single elasticsearch process locally. If I restart the  
> Elasticsearch process (kill and start), the problem goes away. If i  
> restart again, the problem comes back. This alternating behavior repeats  
> for each restart. It makes no difference if i run in the foreground (-f)  
> or not. Also, if i run in the foreground, i don't see any errors.
> 
> Are there specific debug settings to tweak to perhaps get more information  
> as to what is going on?
> 
> On Thursday, February 28, 2013 9:44:25 AM UTC-5, Igor Motov wrote:
> 
> > I cannot reproduce it for some reason. Here's what I tried:
> > 
> > ```
> > @Test
> > public void searchSort() {
> > Node node = startNode("node1");
> > 
> > try {
> > 
> > ```
> > 
> > node.client().admin().indices().prepareDelete("test").execute().actionGet();  
> > } catch (Exception e) {  
> > // ignore  
> > }  
> > node.client().admin().indices().prepareCreate("test")
> > 
> > .setSettings(settingsBuilder().put("index.number\_of\_shards",  
> > 1)).execute().actionGet();
> > 
> > ```
> > for (int i = 0; i < 20; i++) {
> > node.client().prepareIndex("test", "type1", 
> > 
> > ```
> > 
> > Integer.toString(i))  
> > .setSource("field", "test " + i, "num",  
> > i).execute().actionGet();
> > 
> > ```
> > }
> > 
> > ```
> > 
> > node.client().admin().indices().prepareRefresh().execute().actionGet();
> > 
> > ```
> > SearchResponse searchResponse = 
> > 
> > ```
> > 
> > node.client().prepareSearch("test")  
> > .setQuery(QueryBuilders.matchQuery("field", "test 10"))
> > 
> > .addSort(SortBuilders.fieldSort("\_score").ignoreUnmapped(true).order(SortOrder.DESC))
> > 
> > .addSort(SortBuilders.fieldSort("num").ignoreUnmapped(true).order(SortOrder.ASC))  
> > .execute().actionGet();  
> > assertThat(searchResponse.getHits().totalHits(), equalTo(20l));
> > 
> > ```
> > assertThat(searchResponse.getHits().hits()[0].getScore(), 
> > 
> > ```
> > 
> > greaterThan(0.0f));
> > 
> > ```
> > }
> > 
> > ```
> > 
> > Could you modify this test in such a way that it reproduces the issue?  
> > Which version of elasticsearch are you using?
> > 
> > On Wednesday, February 27, 2013 9:59:48 PM UTC-5, echin1999 wrote:
> > 
> > > Here's a downsized version of the code (see attachment on original post  
> > > for complete version except that attachment only has one sort):
> > > 
> > > qb =  
> > > QueryBuilders.filteredQuery(QueryBuilders.matchAllQuery(),FilterBuilders.termFilter(  
> > > "cmp", "1"));
> > > 
> > > SearchRequestBuilder srb = client.prepareSearch().setQuery(qb);
> > > 
> > > I create two sortBuilders...
> > > 
> > > FieldSortBuilder sortBuilder = new FieldSortBuilder("\_score");
> > > 
> > > sortBuilder.ignoreUnmapped(true);
> > > 
> > > sortBuilder.order(SortOrder.DESC);
> > > 
> > > FieldSortBuilder sortBuilder2 = new FieldSortBuilder("ln");
> > > 
> > > sortBuilder2.ignoreUnmapped(true);
> > > 
> > > sortBuilder2.order(SortOrder.ASC);
> > > 
> > > If I only use the first sortBuilder, i get no issues… (score = 1)
> > > 
> > > but if i use both (uncomment the second line here:)
> > > 
> > > srb.addSort(sortBuilder);
> > > 
> > > // srb.addSort(sortBuilder2);
> > > 
> > > // execute
> > > 
> > > SearchResponse response = srb.execute().actionGet();
> > > 
> > > Then I get NaN for the hit.getScore() - please note, however, the  
> > > sorting appears to be fine - hard to tell for sure though.
> > > 
> > > // print response
> > > 
> > > SearchHits hits = response.getHits();
> > > 
> > > for(SearchHit hit: hits)
> > > 
> > > {
> > > 
> > > ```
> > > System.out.println(hit.getScore()+":"+hit.getSourceAsString()); 
> > > 
> > > ```
> > > 
> > > }
> > > 
> > > The document looks like:
> > > 
> > > {"tgl":["pension","fUNd","funkychickeEN"],"ln":"Chin","gui":[5,6],"d":"my  
> > > description at Sat Feb 16 18:11:20 EST  
> > > 2013","dur":180,"nui":[5],"n":"MyPublishTest","pdt":1361056280000,"cmp":1,"fls":{"mvd":1361056280761,"pvd":1361056280762,"ort":3,"ori":1361056280763},"own":5,"by":"Chin  
> > > Ed","cid":28,"fn":"Ed"}
> > > 
> > > Thanks
> > > 
> > > Ed
> > > 
> > > On Wednesday, February 27, 2013 8:15:04 PM UTC-5, Igor Motov wrote:
> > > 
> > > > Yes, that's correct usage. Could you provide a complete example of the  
> > > > issue with NaN?
> > > > 
> > > > On Wednesday, February 27, 2013 6:13:00 PM UTC-5, echin1999 wrote:
> > > > 
> > > > > Hi Igor,
> > > > > 
> > > > > thanks for the tip.. I added the following code to my test program  
> > > > > (essentially wrapping the filter i already had with a custom fiilter score):
> > > > > 
> > > > > qb =  
> > > > > QueryBuilders.customFiltersScoreQuery(qb).add(FilterBuilders.termFilter(  
> > > > > "ln", someValue\_2), 100);
> > > > > 
> > > > > and added a sort on the \_score, and it seems to work. (boost the score  
> > > > > to 100 if field "ln" contained someValue\_2, otherwise not). Is that the  
> > > > > correct usage, or is there a better way that doesn't require wrapping the  
> > > > > existing filter?
> > > > > 
> > > > > Another thing i noticed is that when I iterated through the hits, and  
> > > > > printed the score, I would get a NaN for the score if I added a secondary  
> > > > > sort against another field. If just a single sort by score, then the score  
> > > > > printed fine. Is that expected behavior?
> > > > > 
> > > > > Thanks  
> > > > > Ed
> > > > > 
> > > > > On Wednesday, February 27, 2013 10:11:52 AM UTC-5, Igor Motov wrote:
> > > > > 
> > > > > > You cannot sort on a script field, but you can use a script to  
> > > > > > provide a value to sort on. For example the following script will bring the  
> > > > > > record with the value 42 in the field my\_field to the first position.
> > > > > > 
> > > > > > .addSort(SortBuilders  
> > > > > > .scriptSort("doc["my\_field"].value == my\_val? 1 : 0",  
> > > > > > "number")  
> > > > > > .order(SortOrder.DESC)  
> > > > > > .setParams(MapBuilder.\<String,  
> > > > > > Object\>newMapBuilder().put("my\_val", 42).map()))
> > > > > > 
> > > > > > While you can do it with a script sort, if all you need is to boost  
> > > > > > records with certain values, you might achieve much better performance  
> > > > > > using a Custom Filters Score Query[http://www.elasticsearch.org/guide/reference/query-dsl/custom-filters-score-query.html](http://www.elasticsearch.org/guide/reference/query-dsl/custom-filters-score-query.html) and  
> > > > > > adding \_score as one of the sort value.
> > > > > > 
> > > > > > On Tuesday, February 26, 2013 5:30:46 PM UTC-5, echin1999 wrote:
> > > > > > 
> > > > > > > Hi,
> > > > > > > 
> > > > > > > I was wondering if someone could point me to some sample Java code  
> > > > > > > that specifically shows how to sort on a custom script field.
> > > > > > > 
> > > > > > > I see that the SearchRequestBuilder has a method called: addScriptField(name,  
> > > > > > > script, params);
> > > > > > > 
> > > > > > > but i'm not sure how to define the "script" argument. I assume the  
> > > > > > > "name" is the reference to the script field that i would pass into the  
> > > > > > > construction of a FieldSortBuilder()?
> > > > > > > 
> > > > > > > I want to be able to sort my results based on whether or not a  
> > > > > > > property (or multiple properties) of type long contains a long value  
> > > > > > > specified at search time (ie. documents where the criteria holds true will  
> > > > > > > be returned ahead of those documents where the criteria is false).
> > > > > > > 
> > > > > > > I've attached my sample code which does basic sorting on an existing  
> > > > > > > field - that seems to work fine. I now just want to enhance it to be  
> > > > > > > able to sort on a custom script field coded as i describe above. Any  
> > > > > > > pointers are greatly appreciated!
> > > > > > > 
> > > > > > > thanks
> > > > > > > 
> > > > > > > Ed

--  
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).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![echin1999\_2](https://avatars.discourse-cdn.com/v4/letter/e/f4b2a3/32.png) [@echin1999\_2](https://discuss.elastic.co/u/echin1999_2)\
**Post date:** [March 1, 2013, 12:57am UTC](https://discuss.elastic.co/t/sorting-on-a-custom-script-field-in-java/10912/10 "2013-03-01T00:57:09Z")

</div>

For the server, how do i set the log level? I see in the elasticsearch.yml  
file settings for the slowlog and gc, but nothing for generic logging.

Also, is AbstractNodesTests a class that comes with the release  
distribution? I don't see it in any of the jars. Or do i need to go to  
github and download the src?

thanks.

On Thursday, February 28, 2013 4:06:49 PM UTC-5, Igor Motov wrote:

> You can include the the test that I posted class that I sent you into a  
> class inherited from AbstractNodesTests[https://github.com/elasticsearch/elasticsearch/blob/master/src/test/java/org/elasticsearch/test/integration/AbstractNodesTests.java](https://github.com/elasticsearch/elasticsearch/blob/master/src/test/java/org/elasticsearch/test/integration/AbstractNodesTests.java).  
> Can you enable TRACE logging on your elasticsearch server, run your test  
> and post the log somewhere?
> 
> On Thursday, February 28, 2013 11:05:57 AM UTC-5, echin1999 wrote:
> 
> > so.. upgrading to 0.20.5 didn't seem to help. However I've observed an  
> > unusual pattern.
> > 
> > I am running a single elasticsearch process locally. If I restart the  
> > Elasticsearch process (kill and start), the problem goes away. If i  
> > restart again, the problem comes back. This alternating behavior repeats  
> > for each restart. It makes no difference if i run in the foreground (-f)  
> > or not. Also, if i run in the foreground, i don't see any errors.
> > 
> > Are there specific debug settings to tweak to perhaps get more  
> > information as to what is going on?
> > 
> > On Thursday, February 28, 2013 9:44:25 AM UTC-5, Igor Motov wrote:
> > 
> > > I cannot reproduce it for some reason. Here's what I tried:
> > > 
> > > ```
> > > @Test
> > > public void searchSort() {
> > > Node node = startNode("node1");
> > > 
> > > try {
> > > 
> > > ```
> > > 
> > > node.client().admin().indices().prepareDelete("test").execute().actionGet();  
> > > } catch (Exception e) {  
> > > // ignore  
> > > }  
> > > node.client().admin().indices().prepareCreate("test")
> > > 
> > > .setSettings(settingsBuilder().put("index.number\_of\_shards",  
> > > 1)).execute().actionGet();
> > > 
> > > ```
> > > for (int i = 0; i < 20; i++) {
> > > node.client().prepareIndex("test", "type1", 
> > > 
> > > ```
> > > 
> > > Integer.toString(i))  
> > > .setSource("field", "test " + i, "num",  
> > > i).execute().actionGet();
> > > 
> > > ```
> > > }
> > > 
> > > ```
> > > 
> > > node.client().admin().indices().prepareRefresh().execute().actionGet();
> > > 
> > > ```
> > > SearchResponse searchResponse = 
> > > 
> > > ```
> > > 
> > > node.client().prepareSearch("test")  
> > > .setQuery(QueryBuilders.matchQuery("field", "test 10"))
> > > 
> > > .addSort(SortBuilders.fieldSort("\_score").ignoreUnmapped(true).order(SortOrder.DESC))
> > > 
> > > .addSort(SortBuilders.fieldSort("num").ignoreUnmapped(true).order(SortOrder.ASC))  
> > > .execute().actionGet();  
> > > assertThat(searchResponse.getHits().totalHits(), equalTo(20l));
> > > 
> > > ```
> > > assertThat(searchResponse.getHits().hits()[0].getScore(), 
> > > 
> > > ```
> > > 
> > > greaterThan(0.0f));
> > > 
> > > ```
> > > }
> > > 
> > > ```
> > > 
> > > Could you modify this test in such a way that it reproduces the issue?  
> > > Which version of elasticsearch are you using?
> > > 
> > > On Wednesday, February 27, 2013 9:59:48 PM UTC-5, echin1999 wrote:
> > > 
> > > > Here's a downsized version of the code (see attachment on original post  
> > > > for complete version except that attachment only has one sort):
> > > > 
> > > > qb =  
> > > > QueryBuilders.filteredQuery(QueryBuilders.matchAllQuery(),FilterBuilders.termFilter(  
> > > > "cmp", "1"));
> > > > 
> > > > SearchRequestBuilder srb = client.prepareSearch().setQuery(qb);
> > > > 
> > > > I create two sortBuilders...
> > > > 
> > > > FieldSortBuilder sortBuilder = new FieldSortBuilder("\_score");
> > > > 
> > > > sortBuilder.ignoreUnmapped(true);
> > > > 
> > > > sortBuilder.order(SortOrder.DESC);
> > > > 
> > > > FieldSortBuilder sortBuilder2 = new FieldSortBuilder("ln");
> > > > 
> > > > sortBuilder2.ignoreUnmapped(true);
> > > > 
> > > > sortBuilder2.order(SortOrder.ASC);
> > > > 
> > > > If I only use the first sortBuilder, i get no issues… (score = 1)
> > > > 
> > > > but if i use both (uncomment the second line here:)
> > > > 
> > > > srb.addSort(sortBuilder);
> > > > 
> > > > // srb.addSort(sortBuilder2);
> > > > 
> > > > // execute
> > > > 
> > > > SearchResponse response = srb.execute().actionGet();
> > > > 
> > > > Then I get NaN for the hit.getScore() - please note, however, the  
> > > > sorting appears to be fine - hard to tell for sure though.
> > > > 
> > > > // print response
> > > > 
> > > > SearchHits hits = response.getHits();
> > > > 
> > > > for(SearchHit hit: hits)
> > > > 
> > > > {
> > > > 
> > > > ```
> > > > System.out.println(hit.getScore()+":"+hit.getSourceAsString()); 
> > > > 
> > > > ```
> > > > 
> > > > }
> > > > 
> > > > The document looks like:
> > > > 
> > > > {"tgl":["pension","fUNd","funkychickeEN"],"ln":"Chin","gui":[5,6],"d":"my  
> > > > description at Sat Feb 16 18:11:20 EST  
> > > > 2013","dur":180,"nui":[5],"n":"MyPublishTest","pdt":1361056280000,"cmp":1,"fls":{"mvd":1361056280761,"pvd":1361056280762,"ort":3,"ori":1361056280763},"own":5,"by":"Chin  
> > > > Ed","cid":28,"fn":"Ed"}
> > > > 
> > > > Thanks
> > > > 
> > > > Ed
> > > > 
> > > > On Wednesday, February 27, 2013 8:15:04 PM UTC-5, Igor Motov wrote:
> > > > 
> > > > > Yes, that's correct usage. Could you provide a complete example of the  
> > > > > issue with NaN?
> > > > > 
> > > > > On Wednesday, February 27, 2013 6:13:00 PM UTC-5, echin1999 wrote:
> > > > > 
> > > > > > Hi Igor,
> > > > > > 
> > > > > > thanks for the tip.. I added the following code to my test program  
> > > > > > (essentially wrapping the filter i already had with a custom fiilter score):
> > > > > > 
> > > > > > qb =  
> > > > > > QueryBuilders.customFiltersScoreQuery(qb).add(FilterBuilders.termFilter(  
> > > > > > "ln", someValue\_2), 100);
> > > > > > 
> > > > > > and added a sort on the \_score, and it seems to work. (boost the  
> > > > > > score to 100 if field "ln" contained someValue\_2, otherwise not). Is that  
> > > > > > the correct usage, or is there a better way that doesn't require wrapping  
> > > > > > the existing filter?
> > > > > > 
> > > > > > Another thing i noticed is that when I iterated through the hits, and  
> > > > > > printed the score, I would get a NaN for the score if I added a secondary  
> > > > > > sort against another field. If just a single sort by score, then the score  
> > > > > > printed fine. Is that expected behavior?
> > > > > > 
> > > > > > Thanks  
> > > > > > Ed
> > > > > > 
> > > > > > On Wednesday, February 27, 2013 10:11:52 AM UTC-5, Igor Motov wrote:
> > > > > > 
> > > > > > > You cannot sort on a script field, but you can use a script to  
> > > > > > > provide a value to sort on. For example the following script will bring the  
> > > > > > > record with the value 42 in the field my\_field to the first position.
> > > > > > > 
> > > > > > > .addSort(SortBuilders  
> > > > > > > .scriptSort("doc["my\_field"].value == my\_val? 1 : 0",  
> > > > > > > "number")  
> > > > > > > .order(SortOrder.DESC)  
> > > > > > > .setParams(MapBuilder.\<String,  
> > > > > > > Object\>newMapBuilder().put("my\_val", 42).map()))
> > > > > > > 
> > > > > > > While you can do it with a script sort, if all you need is to boost  
> > > > > > > records with certain values, you might achieve much better performance  
> > > > > > > using a Custom Filters Score Query[http://www.elasticsearch.org/guide/reference/query-dsl/custom-filters-score-query.html](http://www.elasticsearch.org/guide/reference/query-dsl/custom-filters-score-query.html) and  
> > > > > > > adding \_score as one of the sort value.
> > > > > > > 
> > > > > > > On Tuesday, February 26, 2013 5:30:46 PM UTC-5, echin1999 wrote:
> > > > > > > 
> > > > > > > > Hi,
> > > > > > > > 
> > > > > > > > I was wondering if someone could point me to some sample Java code  
> > > > > > > > that specifically shows how to sort on a custom script field.
> > > > > > > > 
> > > > > > > > I see that the SearchRequestBuilder has a method called: addScriptField(name,  
> > > > > > > > script, params);
> > > > > > > > 
> > > > > > > > but i'm not sure how to define the "script" argument. I assume  
> > > > > > > > the "name" is the reference to the script field that i would pass into the  
> > > > > > > > construction of a FieldSortBuilder()?
> > > > > > > > 
> > > > > > > > I want to be able to sort my results based on whether or not a  
> > > > > > > > property (or multiple properties) of type long contains a long value  
> > > > > > > > specified at search time (ie. documents where the criteria holds true will  
> > > > > > > > be returned ahead of those documents where the criteria is false).
> > > > > > > > 
> > > > > > > > I've attached my sample code which does basic sorting on an  
> > > > > > > > existing field - that seems to work fine. I now just want to enhance it  
> > > > > > > > to be able to sort on a custom script field coded as i describe above. Any  
> > > > > > > > pointers are greatly appreciated!
> > > > > > > > 
> > > > > > > > thanks
> > > > > > > > 
> > > > > > > > Ed

--  
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).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Igor\_Motov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/igor_motov/32/45193_2.png) [@Igor\_Motov](https://discuss.elastic.co/u/Igor_Motov)\
**Post date:** [March 1, 2013, 3:32am UTC](https://discuss.elastic.co/t/sorting-on-a-custom-script-field-in-java/10912/11 "2013-03-01T03:32:17Z")

</div>

The log level can be set in logging.yml file which can be found in the same  
directory as elasticsearch.yml. Just replace INFO in the first line with  
TRACE.

The AbstractNodesTests is part of elasticsearch tests, so it's not included  
in the release distribution.

On Thursday, February 28, 2013 7:57:09 PM UTC-5, echin1999 wrote:

> For the server, how do i set the log level? I see in the elasticsearch.yml  
> file settings for the slowlog and gc, but nothing for generic logging.
> 
> Also, is AbstractNodesTests a class that comes with the release  
> distribution? I don't see it in any of the jars. Or do i need to go to  
> github and download the src?
> 
> thanks.
> 
> On Thursday, February 28, 2013 4:06:49 PM UTC-5, Igor Motov wrote:
> 
> > You can include the the test that I posted class that I sent you into a  
> > class inherited from AbstractNodesTests[https://github.com/elasticsearch/elasticsearch/blob/master/src/test/java/org/elasticsearch/test/integration/AbstractNodesTests.java](https://github.com/elasticsearch/elasticsearch/blob/master/src/test/java/org/elasticsearch/test/integration/AbstractNodesTests.java).  
> > Can you enable TRACE logging on your elasticsearch server, run your test  
> > and post the log somewhere?
> > 
> > On Thursday, February 28, 2013 11:05:57 AM UTC-5, echin1999 wrote:
> > 
> > > so.. upgrading to 0.20.5 didn't seem to help. However I've observed an  
> > > unusual pattern.
> > > 
> > > I am running a single elasticsearch process locally. If I restart the  
> > > Elasticsearch process (kill and start), the problem goes away. If i  
> > > restart again, the problem comes back. This alternating behavior repeats  
> > > for each restart. It makes no difference if i run in the foreground (-f)  
> > > or not. Also, if i run in the foreground, i don't see any errors.
> > > 
> > > Are there specific debug settings to tweak to perhaps get more  
> > > information as to what is going on?
> > > 
> > > On Thursday, February 28, 2013 9:44:25 AM UTC-5, Igor Motov wrote:
> > > 
> > > > I cannot reproduce it for some reason. Here's what I tried:
> > > > 
> > > > ```
> > > > @Test
> > > > public void searchSort() {
> > > > Node node = startNode("node1");
> > > > 
> > > > try {
> > > > 
> > > > ```
> > > > 
> > > > node.client().admin().indices().prepareDelete("test").execute().actionGet();  
> > > > } catch (Exception e) {  
> > > > // ignore  
> > > > }  
> > > > node.client().admin().indices().prepareCreate("test")
> > > > 
> > > > .setSettings(settingsBuilder().put("index.number\_of\_shards",  
> > > > 1)).execute().actionGet();
> > > > 
> > > > ```
> > > > for (int i = 0; i < 20; i++) {
> > > > node.client().prepareIndex("test", "type1", 
> > > > 
> > > > ```
> > > > 
> > > > Integer.toString(i))  
> > > > .setSource("field", "test " + i, "num",  
> > > > i).execute().actionGet();
> > > > 
> > > > ```
> > > > }
> > > > 
> > > > ```
> > > > 
> > > > node.client().admin().indices().prepareRefresh().execute().actionGet();
> > > > 
> > > > ```
> > > > SearchResponse searchResponse = 
> > > > 
> > > > ```
> > > > 
> > > > node.client().prepareSearch("test")  
> > > > .setQuery(QueryBuilders.matchQuery("field", "test 10"))
> > > > 
> > > > .addSort(SortBuilders.fieldSort("\_score").ignoreUnmapped(true).order(SortOrder.DESC))
> > > > 
> > > > .addSort(SortBuilders.fieldSort("num").ignoreUnmapped(true).order(SortOrder.ASC))  
> > > > .execute().actionGet();  
> > > > assertThat(searchResponse.getHits().totalHits(), equalTo(20l));
> > > > 
> > > > ```
> > > > assertThat(searchResponse.getHits().hits()[0].getScore(), 
> > > > 
> > > > ```
> > > > 
> > > > greaterThan(0.0f));
> > > > 
> > > > ```
> > > > }
> > > > 
> > > > ```
> > > > 
> > > > Could you modify this test in such a way that it reproduces the issue?  
> > > > Which version of elasticsearch are you using?
> > > > 
> > > > On Wednesday, February 27, 2013 9:59:48 PM UTC-5, echin1999 wrote:
> > > > 
> > > > > Here's a downsized version of the code (see attachment on original  
> > > > > post for complete version except that attachment only has one sort):
> > > > > 
> > > > > qb =  
> > > > > QueryBuilders.filteredQuery(QueryBuilders.matchAllQuery(),FilterBuilders.termFilter(  
> > > > > "cmp", "1"));
> > > > > 
> > > > > SearchRequestBuilder srb = client.prepareSearch().setQuery(qb);
> > > > > 
> > > > > I create two sortBuilders...
> > > > > 
> > > > > FieldSortBuilder sortBuilder = new FieldSortBuilder("\_score");
> > > > > 
> > > > > sortBuilder.ignoreUnmapped(true);
> > > > > 
> > > > > sortBuilder.order(SortOrder.DESC);
> > > > > 
> > > > > FieldSortBuilder sortBuilder2 = new FieldSortBuilder("ln");
> > > > > 
> > > > > sortBuilder2.ignoreUnmapped(true);
> > > > > 
> > > > > sortBuilder2.order(SortOrder.ASC);
> > > > > 
> > > > > If I only use the first sortBuilder, i get no issues… (score = 1)
> > > > > 
> > > > > but if i use both (uncomment the second line here:)
> > > > > 
> > > > > srb.addSort(sortBuilder);
> > > > > 
> > > > > // srb.addSort(sortBuilder2);
> > > > > 
> > > > > // execute
> > > > > 
> > > > > SearchResponse response = srb.execute().actionGet();
> > > > > 
> > > > > Then I get NaN for the hit.getScore() - please note, however, the  
> > > > > sorting appears to be fine - hard to tell for sure though.
> > > > > 
> > > > > // print response
> > > > > 
> > > > > SearchHits hits = response.getHits();
> > > > > 
> > > > > for(SearchHit hit: hits)
> > > > > 
> > > > > {
> > > > > 
> > > > > ```
> > > > > System.out.println(hit.getScore()+":"+hit.getSourceAsString()); 
> > > > > 
> > > > > ```
> > > > > 
> > > > > }
> > > > > 
> > > > > The document looks like:
> > > > > 
> > > > > {"tgl":["pension","fUNd","funkychickeEN"],"ln":"Chin","gui":[5,6],"d":"my  
> > > > > description at Sat Feb 16 18:11:20 EST  
> > > > > 2013","dur":180,"nui":[5],"n":"MyPublishTest","pdt":1361056280000,"cmp":1,"fls":{"mvd":1361056280761,"pvd":1361056280762,"ort":3,"ori":1361056280763},"own":5,"by":"Chin  
> > > > > Ed","cid":28,"fn":"Ed"}
> > > > > 
> > > > > Thanks
> > > > > 
> > > > > Ed
> > > > > 
> > > > > On Wednesday, February 27, 2013 8:15:04 PM UTC-5, Igor Motov wrote:
> > > > > 
> > > > > > Yes, that's correct usage. Could you provide a complete example of  
> > > > > > the issue with NaN?
> > > > > > 
> > > > > > On Wednesday, February 27, 2013 6:13:00 PM UTC-5, echin1999 wrote:
> > > > > > 
> > > > > > > Hi Igor,
> > > > > > > 
> > > > > > > thanks for the tip.. I added the following code to my test program  
> > > > > > > (essentially wrapping the filter i already had with a custom fiilter score):
> > > > > > > 
> > > > > > > qb =  
> > > > > > > QueryBuilders.customFiltersScoreQuery(qb).add(FilterBuilders.termFilter(  
> > > > > > > "ln", someValue\_2), 100);
> > > > > > > 
> > > > > > > and added a sort on the \_score, and it seems to work. (boost the  
> > > > > > > score to 100 if field "ln" contained someValue\_2, otherwise not). Is that  
> > > > > > > the correct usage, or is there a better way that doesn't require wrapping  
> > > > > > > the existing filter?
> > > > > > > 
> > > > > > > Another thing i noticed is that when I iterated through the hits,  
> > > > > > > and printed the score, I would get a NaN for the score if I added a  
> > > > > > > secondary sort against another field. If just a single sort by score, then  
> > > > > > > the score printed fine. Is that expected behavior?
> > > > > > > 
> > > > > > > Thanks  
> > > > > > > Ed
> > > > > > > 
> > > > > > > On Wednesday, February 27, 2013 10:11:52 AM UTC-5, Igor Motov wrote:
> > > > > > > 
> > > > > > > > You cannot sort on a script field, but you can use a script to  
> > > > > > > > provide a value to sort on. For example the following script will bring the  
> > > > > > > > record with the value 42 in the field my\_field to the first position.
> > > > > > > > 
> > > > > > > > .addSort(SortBuilders  
> > > > > > > > .scriptSort("doc["my\_field"].value == my\_val? 1 : 0",  
> > > > > > > > "number")  
> > > > > > > > .order(SortOrder.DESC)  
> > > > > > > > .setParams(MapBuilder.\<String,  
> > > > > > > > Object\>newMapBuilder().put("my\_val", 42).map()))
> > > > > > > > 
> > > > > > > > While you can do it with a script sort, if all you need is to boost  
> > > > > > > > records with certain values, you might achieve much better performance  
> > > > > > > > using a Custom Filters Score Query[http://www.elasticsearch.org/guide/reference/query-dsl/custom-filters-score-query.html](http://www.elasticsearch.org/guide/reference/query-dsl/custom-filters-score-query.html) and  
> > > > > > > > adding \_score as one of the sort value.
> > > > > > > > 
> > > > > > > > On Tuesday, February 26, 2013 5:30:46 PM UTC-5, echin1999 wrote:
> > > > > > > > 
> > > > > > > > > Hi,
> > > > > > > > > 
> > > > > > > > > I was wondering if someone could point me to some sample Java code  
> > > > > > > > > that specifically shows how to sort on a custom script field.
> > > > > > > > > 
> > > > > > > > > I see that the SearchRequestBuilder has a method called: addScriptField(name,  
> > > > > > > > > script, params);
> > > > > > > > > 
> > > > > > > > > but i'm not sure how to define the "script" argument. I assume  
> > > > > > > > > the "name" is the reference to the script field that i would pass into the  
> > > > > > > > > construction of a FieldSortBuilder()?
> > > > > > > > > 
> > > > > > > > > I want to be able to sort my results based on whether or not a  
> > > > > > > > > property (or multiple properties) of type long contains a long value  
> > > > > > > > > specified at search time (ie. documents where the criteria holds true will  
> > > > > > > > > be returned ahead of those documents where the criteria is false).
> > > > > > > > > 
> > > > > > > > > I've attached my sample code which does basic sorting on an  
> > > > > > > > > existing field - that seems to work fine. I now just want to enhance it  
> > > > > > > > > to be able to sort on a custom script field coded as i describe above. Any  
> > > > > > > > > pointers are greatly appreciated!
> > > > > > > > > 
> > > > > > > > > thanks
> > > > > > > > > 
> > > > > > > > > Ed

--  
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).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![echin1999\_2](https://avatars.discourse-cdn.com/v4/letter/e/f4b2a3/32.png) [@echin1999\_2](https://discuss.elastic.co/u/echin1999_2)\
**Post date:** [March 1, 2013, 4:44am UTC](https://discuss.elastic.co/t/sorting-on-a-custom-script-field-in-java/10912/12 "2013-03-01T04:44:28Z")

</div>

I prepared two log files: 1) log file when the NaN  
occurs: [http://abbyandally.com/echin1999/log\_with\_NaN.log](http://abbyandally.com/echin1999/log_with_NaN.log)  
and 2) log file when the program worked  
fine: [http://abbyandally.com/echin1999/log\_when\_it\_works.log](http://abbyandally.com/echin1999/log_when_it_works.log)

thanks

On Thursday, February 28, 2013 10:32:17 PM UTC-5, Igor Motov wrote:

> The log level can be set in logging.yml file which can be found in the  
> same directory as elasticsearch.yml. Just replace INFO in the first line  
> with TRACE.
> 
> The AbstractNodesTests is part of elasticsearch tests, so it's not  
> included in the release distribution.
> 
> On Thursday, February 28, 2013 7:57:09 PM UTC-5, echin1999 wrote:
> 
> > For the server, how do i set the log level? I see in the  
> > elasticsearch.yml file settings for the slowlog and gc, but nothing for  
> > generic logging.
> > 
> > Also, is AbstractNodesTests a class that comes with the release  
> > distribution? I don't see it in any of the jars. Or do i need to go to  
> > github and download the src?
> > 
> > thanks.
> > 
> > On Thursday, February 28, 2013 4:06:49 PM UTC-5, Igor Motov wrote:
> > 
> > > You can include the the test that I posted class that I sent you into a  
> > > class inherited from AbstractNodesTests[https://github.com/elasticsearch/elasticsearch/blob/master/src/test/java/org/elasticsearch/test/integration/AbstractNodesTests.java](https://github.com/elasticsearch/elasticsearch/blob/master/src/test/java/org/elasticsearch/test/integration/AbstractNodesTests.java).  
> > > Can you enable TRACE logging on your elasticsearch server, run your test  
> > > and post the log somewhere?
> > > 
> > > On Thursday, February 28, 2013 11:05:57 AM UTC-5, echin1999 wrote:
> > > 
> > > > so.. upgrading to 0.20.5 didn't seem to help. However I've observed an  
> > > > unusual pattern.
> > > > 
> > > > I am running a single elasticsearch process locally. If I restart the  
> > > > Elasticsearch process (kill and start), the problem goes away. If i  
> > > > restart again, the problem comes back. This alternating behavior repeats  
> > > > for each restart. It makes no difference if i run in the foreground (-f)  
> > > > or not. Also, if i run in the foreground, i don't see any errors.
> > > > 
> > > > Are there specific debug settings to tweak to perhaps get more  
> > > > information as to what is going on?
> > > > 
> > > > On Thursday, February 28, 2013 9:44:25 AM UTC-5, Igor Motov wrote:
> > > > 
> > > > > I cannot reproduce it for some reason. Here's what I tried:
> > > > > 
> > > > > ```
> > > > > @Test
> > > > > public void searchSort() {
> > > > > Node node = startNode("node1");
> > > > > 
> > > > > try {
> > > > > 
> > > > > ```
> > > > > 
> > > > > node.client().admin().indices().prepareDelete("test").execute().actionGet();  
> > > > > } catch (Exception e) {  
> > > > > // ignore  
> > > > > }  
> > > > > node.client().admin().indices().prepareCreate("test")
> > > > > 
> > > > > .setSettings(settingsBuilder().put("index.number\_of\_shards",  
> > > > > 1)).execute().actionGet();
> > > > > 
> > > > > ```
> > > > > for (int i = 0; i < 20; i++) {
> > > > > node.client().prepareIndex("test", "type1", 
> > > > > 
> > > > > ```
> > > > > 
> > > > > Integer.toString(i))  
> > > > > .setSource("field", "test " + i, "num",  
> > > > > i).execute().actionGet();
> > > > > 
> > > > > ```
> > > > > }
> > > > > 
> > > > > ```
> > > > > 
> > > > > node.client().admin().indices().prepareRefresh().execute().actionGet();
> > > > > 
> > > > > ```
> > > > > SearchResponse searchResponse = 
> > > > > 
> > > > > ```
> > > > > 
> > > > > node.client().prepareSearch("test")  
> > > > > .setQuery(QueryBuilders.matchQuery("field", "test 10"))
> > > > > 
> > > > > .addSort(SortBuilders.fieldSort("\_score").ignoreUnmapped(true).order(SortOrder.DESC))
> > > > > 
> > > > > .addSort(SortBuilders.fieldSort("num").ignoreUnmapped(true).order(SortOrder.ASC))  
> > > > > .execute().actionGet();  
> > > > > assertThat(searchResponse.getHits().totalHits(), equalTo(20l));
> > > > > 
> > > > > ```
> > > > > assertThat(searchResponse.getHits().hits()[0].getScore(), 
> > > > > 
> > > > > ```
> > > > > 
> > > > > greaterThan(0.0f));
> > > > > 
> > > > > ```
> > > > > }
> > > > > 
> > > > > ```
> > > > > 
> > > > > Could you modify this test in such a way that it reproduces the issue?  
> > > > > Which version of elasticsearch are you using?
> > > > > 
> > > > > On Wednesday, February 27, 2013 9:59:48 PM UTC-5, echin1999 wrote:
> > > > > 
> > > > > > Here's a downsized version of the code (see attachment on original  
> > > > > > post for complete version except that attachment only has one sort):
> > > > > > 
> > > > > > qb =  
> > > > > > QueryBuilders.filteredQuery(QueryBuilders.matchAllQuery(),FilterBuilders.termFilter(  
> > > > > > "cmp", "1"));
> > > > > > 
> > > > > > SearchRequestBuilder srb = client.prepareSearch().setQuery(qb);
> > > > > > 
> > > > > > I create two sortBuilders...
> > > > > > 
> > > > > > FieldSortBuilder sortBuilder = new FieldSortBuilder("\_score");
> > > > > > 
> > > > > > sortBuilder.ignoreUnmapped(true);
> > > > > > 
> > > > > > sortBuilder.order(SortOrder.DESC);
> > > > > > 
> > > > > > FieldSortBuilder sortBuilder2 = new FieldSortBuilder("ln");
> > > > > > 
> > > > > > sortBuilder2.ignoreUnmapped(true);
> > > > > > 
> > > > > > sortBuilder2.order(SortOrder.ASC);
> > > > > > 
> > > > > > If I only use the first sortBuilder, i get no issues… (score = 1)
> > > > > > 
> > > > > > but if i use both (uncomment the second line here:)
> > > > > > 
> > > > > > srb.addSort(sortBuilder);
> > > > > > 
> > > > > > // srb.addSort(sortBuilder2);
> > > > > > 
> > > > > > // execute
> > > > > > 
> > > > > > SearchResponse response = srb.execute().actionGet();
> > > > > > 
> > > > > > Then I get NaN for the hit.getScore() - please note, however, the  
> > > > > > sorting appears to be fine - hard to tell for sure though.
> > > > > > 
> > > > > > // print response
> > > > > > 
> > > > > > SearchHits hits = response.getHits();
> > > > > > 
> > > > > > for(SearchHit hit: hits)
> > > > > > 
> > > > > > {
> > > > > > 
> > > > > > ```
> > > > > > System.out.println(hit.getScore()+":"+hit.getSourceAsString()); 
> > > > > > 
> > > > > > ```
> > > > > > 
> > > > > > }
> > > > > > 
> > > > > > The document looks like:
> > > > > > 
> > > > > > {"tgl":["pension","fUNd","funkychickeEN"],"ln":"Chin","gui":[5,6],"d":"my  
> > > > > > description at Sat Feb 16 18:11:20 EST  
> > > > > > 2013","dur":180,"nui":[5],"n":"MyPublishTest","pdt":1361056280000,"cmp":1,"fls":{"mvd":1361056280761,"pvd":1361056280762,"ort":3,"ori":1361056280763},"own":5,"by":"Chin  
> > > > > > Ed","cid":28,"fn":"Ed"}
> > > > > > 
> > > > > > Thanks
> > > > > > 
> > > > > > Ed
> > > > > > 
> > > > > > On Wednesday, February 27, 2013 8:15:04 PM UTC-5, Igor Motov wrote:
> > > > > > 
> > > > > > > Yes, that's correct usage. Could you provide a complete example of  
> > > > > > > the issue with NaN?
> > > > > > > 
> > > > > > > On Wednesday, February 27, 2013 6:13:00 PM UTC-5, echin1999 wrote:
> > > > > > > 
> > > > > > > > Hi Igor,
> > > > > > > > 
> > > > > > > > thanks for the tip.. I added the following code to my test program  
> > > > > > > > (essentially wrapping the filter i already had with a custom fiilter score):
> > > > > > > > 
> > > > > > > > qb =  
> > > > > > > > QueryBuilders.customFiltersScoreQuery(qb).add(FilterBuilders.termFilter(  
> > > > > > > > "ln", someValue\_2), 100);
> > > > > > > > 
> > > > > > > > and added a sort on the \_score, and it seems to work. (boost the  
> > > > > > > > score to 100 if field "ln" contained someValue\_2, otherwise not). Is that  
> > > > > > > > the correct usage, or is there a better way that doesn't require wrapping  
> > > > > > > > the existing filter?
> > > > > > > > 
> > > > > > > > Another thing i noticed is that when I iterated through the hits,  
> > > > > > > > and printed the score, I would get a NaN for the score if I added a  
> > > > > > > > secondary sort against another field. If just a single sort by score, then  
> > > > > > > > the score printed fine. Is that expected behavior?
> > > > > > > > 
> > > > > > > > Thanks  
> > > > > > > > Ed
> > > > > > > > 
> > > > > > > > On Wednesday, February 27, 2013 10:11:52 AM UTC-5, Igor Motov wrote:
> > > > > > > > 
> > > > > > > > > You cannot sort on a script field, but you can use a script to  
> > > > > > > > > provide a value to sort on. For example the following script will bring the  
> > > > > > > > > record with the value 42 in the field my\_field to the first position.
> > > > > > > > > 
> > > > > > > > > .addSort(SortBuilders  
> > > > > > > > > .scriptSort("doc["my\_field"].value == my\_val? 1 : 0",  
> > > > > > > > > "number")  
> > > > > > > > > .order(SortOrder.DESC)  
> > > > > > > > > .setParams(MapBuilder.\<String,  
> > > > > > > > > Object\>newMapBuilder().put("my\_val", 42).map()))
> > > > > > > > > 
> > > > > > > > > While you can do it with a script sort, if all you need is to  
> > > > > > > > > boost records with certain values, you might achieve much better  
> > > > > > > > > performance using a Custom Filters Score Query[http://www.elasticsearch.org/guide/reference/query-dsl/custom-filters-score-query.html](http://www.elasticsearch.org/guide/reference/query-dsl/custom-filters-score-query.html) and  
> > > > > > > > > adding \_score as one of the sort value.
> > > > > > > > > 
> > > > > > > > > On Tuesday, February 26, 2013 5:30:46 PM UTC-5, echin1999 wrote:
> > > > > > > > > 
> > > > > > > > > > Hi,
> > > > > > > > > > 
> > > > > > > > > > I was wondering if someone could point me to some sample Java  
> > > > > > > > > > code that specifically shows how to sort on a custom script field.
> > > > > > > > > > 
> > > > > > > > > > I see that the SearchRequestBuilder has a method called: addScriptField(name,  
> > > > > > > > > > script, params);
> > > > > > > > > > 
> > > > > > > > > > but i'm not sure how to define the "script" argument. I assume  
> > > > > > > > > > the "name" is the reference to the script field that i would pass into the  
> > > > > > > > > > construction of a FieldSortBuilder()?
> > > > > > > > > > 
> > > > > > > > > > I want to be able to sort my results based on whether or not a  
> > > > > > > > > > property (or multiple properties) of type long contains a long value  
> > > > > > > > > > specified at search time (ie. documents where the criteria holds true will  
> > > > > > > > > > be returned ahead of those documents where the criteria is false).
> > > > > > > > > > 
> > > > > > > > > > I've attached my sample code which does basic sorting on an  
> > > > > > > > > > existing field - that seems to work fine. I now just want to enhance it  
> > > > > > > > > > to be able to sort on a custom script field coded as i describe above. Any  
> > > > > > > > > > pointers are greatly appreciated!
> > > > > > > > > > 
> > > > > > > > > > thanks
> > > > > > > > > > 
> > > > > > > > > > Ed

--  
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).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![M\_20](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/m_20/32/1371_2.png) [@M\_20](https://discuss.elastic.co/u/M_20)\
**Post date:** [July 23, 2014, 8:05pm UTC](https://discuss.elastic.co/t/sorting-on-a-custom-script-field-in-java/10912/13 "2014-07-23T20:05:11Z")

</div>

Hi Igor,

I wrote a code to sort on custom script( based on ES documentation

> **[Elasticsearch Platform — Find real-time answers at scale](https://www.elastic.co)**
>
> Power insights and outcomes with the Elasticsearch Platform and AI. See into your data and find answers that matter with enterprise solutions designed to help you build, observe, and protect. Try Elasticsearch free today.

), but it doesn't work.  
Here are the code and the error message. Any suggestion?

///// journalscorenormal --\> the field's name

String script = "doc['journalscorenormal'].  
value";  
ScriptSortBuilder sortSc = SortBuilders.scriptSort(script,"float");  
SearchResponse response = client  
.prepareSearch(index)  
.setTypes(MyVars.index.getType())  
.setSearchType(SearchType.DFS\_QUERY\_THEN\_FETCH)  
.setQuery(  
QueryBuilders.fuzzyLikeThisQuery(field)  
.likeText(searchTerm)).setFrom(0)  
.setSize(60)  
.addSort(sortSc)  
.setExplain(true).execute().actionGet();

Failed to parse source  
[{"from":0,"size":60,"query":{"flt":{"fields":["sentence"],"like\_text":"disease"}},"explain":true,"  
_sort":[{"\_script":{"script":"doc['journalscorenormal'].value","type":"float"}}]}]]_];  
nested: ScriptException[dynamic scripting disabled]; }  
at  
org.elasticsearch.action.search.type.TransportSearchTypeAction$BaseAsyncAction.onFirstPhaseResult(TransportSearchTypeAction.java:233)  
at  
org.elasticsearch.action.search.type.TransportSearchTypeAction$BaseAsyncAction$1.onFailure(TransportSearchTypeAction.java:179)  
at  
org.elasticsearch.search.action.SearchServiceTransportAction$23.run(SearchServiceTransportAction.java:523)  
at  
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)  
at  
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)  
at java.lang.Thread.run(Thread.java:744)

On Tuesday, February 26, 2013 4:30:46 PM UTC-6, echin1999 wrote:

> Hi,
> 
> I was wondering if someone could point me to some sample Java code that  
> specifically shows how to sort on a custom script field.
> 
> I see that the SearchRequestBuilder has a method called: addScriptField(name,  
> script, params);
> 
> but i'm not sure how to define the "script" argument. I assume the  
> "name" is the reference to the script field that i would pass into the  
> construction of a FieldSortBuilder()?
> 
> I want to be able to sort my results based on whether or not a property  
> (or multiple properties) of type long contains a long value specified at  
> search time (ie. documents where the criteria holds true will be returned  
> ahead of those documents where the criteria is false).
> 
> I've attached my sample code which does basic sorting on an existing field
> 
> - that seems to work fine. I now just want to enhance it to be able to  
> sort on a custom script field coded as i describe above. Any pointers are  
> greatly appreciated!
> 
> thanks
> 
> Ed

--  
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/89ace971-b938-4181-a27c-f7a842df4a82%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/89ace971-b938-4181-a27c-f7a842df4a82%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![M\_20](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/m_20/32/1371_2.png) [@M\_20](https://discuss.elastic.co/u/M_20)\
**Post date:** [July 23, 2014, 9:08pm UTC](https://discuss.elastic.co/t/sorting-on-a-custom-script-field-in-java/10912/14 "2014-07-23T21:08:24Z")

</div>

I found the error !!!!!!!!!!!!!!!!!!

enable dynamic scripting in config file

> **[Elasticsearch Platform — Find real-time answers at scale](https://www.elastic.co)**
>
> Power insights and outcomes with the Elasticsearch Platform and AI. See into your data and find answers that matter with enterprise solutions designed to help you build, observe, and protect. Try Elasticsearch free today.

On Wednesday, July 23, 2014 3:05:11 PM UTC-5, M\_20 wrote:

> Hi Igor,
> 
> I wrote a code to sort on custom script( based on ES documentation  
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-request-sort.html)  
> ), but it doesn't work.  
> Here are the code and the error message. Any suggestion?
> 
> ///// journalscorenormal --\> the field's name
> 
> String script = "doc['journalscorenormal'].  
> value";  
> ScriptSortBuilder sortSc = SortBuilders.scriptSort(script,"float");  
> SearchResponse response = client  
> .prepareSearch(index)  
> .setTypes(MyVars.index.getType())  
> .setSearchType(SearchType.DFS\_QUERY\_THEN\_FETCH)  
> .setQuery(  
> QueryBuilders.fuzzyLikeThisQuery(field)  
> .likeText(searchTerm)).setFrom(0)  
> .setSize(60)  
> .addSort(sortSc)  
> .setExplain(true).execute().actionGet();
> 
> Failed to parse source  
> [{"from":0,"size":60,"query":{"flt":{"fields":["sentence"],"like\_text":"disease"}},"explain":true,"  
> _sort":[{"\_script":{"script":"doc['journalscorenormal'].value","type":"float"}}]}]]_];  
> nested: ScriptException[dynamic scripting disabled]; }  
> at  
> org.elasticsearch.action.search.type.TransportSearchTypeAction$BaseAsyncAction.onFirstPhaseResult(TransportSearchTypeAction.java:233)  
> at  
> org.elasticsearch.action.search.type.TransportSearchTypeAction$BaseAsyncAction$1.onFailure(TransportSearchTypeAction.java:179)  
> at  
> org.elasticsearch.search.action.SearchServiceTransportAction$23.run(SearchServiceTransportAction.java:523)  
> at  
> java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)  
> at  
> java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)  
> at java.lang.Thread.run(Thread.java:744)
> 
> On Tuesday, February 26, 2013 4:30:46 PM UTC-6, echin1999 wrote:
> 
> > Hi,
> > 
> > I was wondering if someone could point me to some sample Java code that  
> > specifically shows how to sort on a custom script field.
> > 
> > I see that the SearchRequestBuilder has a method called: addScriptField(name,  
> > script, params);
> > 
> > but i'm not sure how to define the "script" argument. I assume the  
> > "name" is the reference to the script field that i would pass into the  
> > construction of a FieldSortBuilder()?
> > 
> > I want to be able to sort my results based on whether or not a property  
> > (or multiple properties) of type long contains a long value specified at  
> > search time (ie. documents where the criteria holds true will be returned  
> > ahead of those documents where the criteria is false).
> > 
> > I've attached my sample code which does basic sorting on an existing  
> > field - that seems to work fine. I now just want to enhance it to be  
> > able to sort on a custom script field coded as i describe above. Any  
> > pointers are greatly appreciated!
> > 
> > thanks
> > 
> > Ed

--  
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/43320492-9521-4ba5-9782-4afe782069b8%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/43320492-9521-4ba5-9782-4afe782069b8%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![M\_20](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/m_20/32/1371_2.png) [@M\_20](https://discuss.elastic.co/u/M_20)\
**Post date:** [July 24, 2014, 1:29pm UTC](https://discuss.elastic.co/t/sorting-on-a-custom-script-field-in-java/10912/15 "2014-07-24T13:29:42Z")

</div>

How can I use ES score in the script?  
The following script works and I am able to sort the results based on this  
script:

String script = "doc['studyscorenormal'].value";  
ScriptSortBuilder sortScript = SortBuilders.scriptSort(script,  
"number").order(SortOrder.DESC);

But when I am using \_score in the script I am getting an error:  
String script = "doc['studyscorenormal'].value \* \_score";

Here is the code:

```
            SearchResponse response = client
                    .prepareSearch(index)
                    .setTypes(type)
                    .setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
                    .setQuery(
                            QueryBuilders.fuzzyLikeThisQuery(field)
                                    .likeText(searchTerm)).setFrom(0)
                    .setSize(60)
                    .addSort(sortScript)
                    .setExplain(true).execute().actionGet();

```

Here is the error message:

sort[\<custom:"\_script":  
org.elasticsearch.index.fielddata.fieldcomparator.DoubleScriptDataComparator$InnerSource@65c4edc\>!,]:  
Query Failed [Failed to execute main query]]; nested:  
PropertyAccessException[[_Error: unresolvable property or identifier:  
\_score_]  
[Near : {... doc['studyscorenormal'].value + \_score ....}]  
^  
[Line: 1, Column: 33]]; }

On Wednesday, July 23, 2014 4:08:24 PM UTC-5, M\_20 wrote:

> I found the error !!!!!!!!!!!!!!!!!!
> 
> enable dynamic scripting in config file
> 
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/modules-scripting.html)
> 
> On Wednesday, July 23, 2014 3:05:11 PM UTC-5, M\_20 wrote:
> 
> > Hi Igor,
> > 
> > I wrote a code to sort on custom script( based on ES documentation  
> > [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-request-sort.html)  
> > ), but it doesn't work.  
> > Here are the code and the error message. Any suggestion?
> > 
> > ///// journalscorenormal --\> the field's name
> > 
> > String script = "doc['journalscorenormal'].  
> > value";  
> > ScriptSortBuilder sortSc = SortBuilders.scriptSort(script,"float");  
> > SearchResponse response = client  
> > .prepareSearch(index)  
> > .setTypes(MyVars.index.getType())  
> > .setSearchType(SearchType.DFS\_QUERY\_THEN\_FETCH)  
> > .setQuery(  
> > QueryBuilders.fuzzyLikeThisQuery(field)  
> > .likeText(searchTerm)).setFrom(0)  
> > .setSize(60)  
> > .addSort(sortSc)  
> > .setExplain(true).execute().actionGet();
> > 
> > Failed to parse source  
> > [{"from":0,"size":60,"query":{"flt":{"fields":["sentence"],"like\_text":"disease"}},"explain":true,"  
> > _sort":[{"\_script":{"script":"doc['journalscorenormal'].value","type":"float"}}]}]]_];  
> > nested: ScriptException[dynamic scripting disabled]; }  
> > at  
> > org.elasticsearch.action.search.type.TransportSearchTypeAction$BaseAsyncAction.onFirstPhaseResult(TransportSearchTypeAction.java:233)  
> > at  
> > org.elasticsearch.action.search.type.TransportSearchTypeAction$BaseAsyncAction$1.onFailure(TransportSearchTypeAction.java:179)  
> > at  
> > org.elasticsearch.search.action.SearchServiceTransportAction$23.run(SearchServiceTransportAction.java:523)  
> > at  
> > java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)  
> > at  
> > java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)  
> > at java.lang.Thread.run(Thread.java:744)
> > 
> > On Tuesday, February 26, 2013 4:30:46 PM UTC-6, echin1999 wrote:
> > 
> > > Hi,
> > > 
> > > I was wondering if someone could point me to some sample Java code that  
> > > specifically shows how to sort on a custom script field.
> > > 
> > > I see that the SearchRequestBuilder has a method called: addScriptField(name,  
> > > script, params);
> > > 
> > > but i'm not sure how to define the "script" argument. I assume the  
> > > "name" is the reference to the script field that i would pass into the  
> > > construction of a FieldSortBuilder()?
> > > 
> > > I want to be able to sort my results based on whether or not a property  
> > > (or multiple properties) of type long contains a long value specified at  
> > > search time (ie. documents where the criteria holds true will be returned  
> > > ahead of those documents where the criteria is false).
> > > 
> > > I've attached my sample code which does basic sorting on an existing  
> > > field - that seems to work fine. I now just want to enhance it to be  
> > > able to sort on a custom script field coded as i describe above. Any  
> > > pointers are greatly appreciated!
> > > 
> > > thanks
> > > 
> > > Ed

--  
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/e20fc8b7-f598-4b40-9ffe-09cf4288e73b%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/e20fc8b7-f598-4b40-9ffe-09cf4288e73b%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Maxime\_Nay](https://avatars.discourse-cdn.com/v4/letter/m/edb3f5/32.png) [@Maxime\_Nay](https://discuss.elastic.co/u/Maxime_Nay)\
**Post date:** [July 29, 2014, 2:56am UTC](https://discuss.elastic.co/t/sorting-on-a-custom-script-field-in-java/10912/16 "2014-07-29T02:56:02Z")

</div>

Same here, I am unable to access \_score from a sorting script.  
Any idea if it's possible?

On Thursday, July 24, 2014 6:29:42 AM UTC-7, M\_20 wrote:

> How can I use ES score in the script?  
> The following script works and I am able to sort the results based on this  
> script:
> 
> String script = "doc['studyscorenormal'].value";  
> ScriptSortBuilder sortScript = SortBuilders.scriptSort(script,  
> "number").order(SortOrder.DESC);
> 
> But when I am using \_score in the script I am getting an error:  
> String script = "doc['studyscorenormal'].value \* \_score";
> 
> Here is the code:
> 
> ```
> SearchResponse response = client
> .prepareSearch(index)
> .setTypes(type)
> .setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
> .setQuery(
> QueryBuilders.fuzzyLikeThisQuery(field)
> .likeText(searchTerm)).setFrom(0)
> .setSize(60)
> .addSort(sortScript)
> .setExplain(true).execute().actionGet();
> 
> ```
> 
> Here is the error message:
> 
> sort[\<custom:"\_script":  
> org.elasticsearch.index.fielddata.fieldcomparator.DoubleScriptDataComparator$InnerSource@65c4edc\>!,]:  
> Query Failed [Failed to execute main query]]; nested:  
> PropertyAccessException[[_Error: unresolvable property or identifier:  
> \_score_]  
> [Near : {... doc['studyscorenormal'].value + \_score ....}]  
> ^  
> [Line: 1, Column: 33]]; }
> 
> On Wednesday, July 23, 2014 4:08:24 PM UTC-5, M\_20 wrote:
> 
> > I found the error !!!!!!!!!!!!!!!!!!
> > 
> > enable dynamic scripting in config file
> > 
> > [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/modules-scripting.html)
> > 
> > On Wednesday, July 23, 2014 3:05:11 PM UTC-5, M\_20 wrote:
> > 
> > > Hi Igor,
> > > 
> > > I wrote a code to sort on custom script( based on ES documentation  
> > > [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-request-sort.html)  
> > > ), but it doesn't work.  
> > > Here are the code and the error message. Any suggestion?
> > > 
> > > ///// journalscorenormal --\> the field's name
> > > 
> > > String script = "doc['journalscorenormal'].  
> > > value";  
> > > ScriptSortBuilder sortSc = SortBuilders.scriptSort(script,"float");  
> > > SearchResponse response = client  
> > > .prepareSearch(index)  
> > > .setTypes(MyVars.index.getType())  
> > > .setSearchType(SearchType.DFS\_QUERY\_THEN\_FETCH)  
> > > .setQuery(  
> > > QueryBuilders.fuzzyLikeThisQuery(field)  
> > > .likeText(searchTerm)).setFrom(0)  
> > > .setSize(60)  
> > > .addSort(sortSc)  
> > > .setExplain(true).execute().actionGet();
> > > 
> > > Failed to parse source  
> > > [{"from":0,"size":60,"query":{"flt":{"fields":["sentence"],"like\_text":"disease"}},"explain":true,"  
> > > _sort":[{"\_script":{"script":"doc['journalscorenormal'].value","type":"float"}}]}]]_];  
> > > nested: ScriptException[dynamic scripting disabled]; }  
> > > at  
> > > org.elasticsearch.action.search.type.TransportSearchTypeAction$BaseAsyncAction.onFirstPhaseResult(TransportSearchTypeAction.java:233)  
> > > at  
> > > org.elasticsearch.action.search.type.TransportSearchTypeAction$BaseAsyncAction$1.onFailure(TransportSearchTypeAction.java:179)  
> > > at  
> > > org.elasticsearch.search.action.SearchServiceTransportAction$23.run(SearchServiceTransportAction.java:523)  
> > > at  
> > > java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)  
> > > at  
> > > java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)  
> > > at java.lang.Thread.run(Thread.java:744)
> > > 
> > > On Tuesday, February 26, 2013 4:30:46 PM UTC-6, echin1999 wrote:
> > > 
> > > > Hi,
> > > > 
> > > > I was wondering if someone could point me to some sample Java code that  
> > > > specifically shows how to sort on a custom script field.
> > > > 
> > > > I see that the SearchRequestBuilder has a method called: addScriptField(name,  
> > > > script, params);
> > > > 
> > > > but i'm not sure how to define the "script" argument. I assume the  
> > > > "name" is the reference to the script field that i would pass into the  
> > > > construction of a FieldSortBuilder()?
> > > > 
> > > > I want to be able to sort my results based on whether or not a property  
> > > > (or multiple properties) of type long contains a long value specified at  
> > > > search time (ie. documents where the criteria holds true will be returned  
> > > > ahead of those documents where the criteria is false).
> > > > 
> > > > I've attached my sample code which does basic sorting on an existing  
> > > > field - that seems to work fine. I now just want to enhance it to be  
> > > > able to sort on a custom script field coded as i describe above. Any  
> > > > pointers are greatly appreciated!
> > > > 
> > > > thanks
> > > > 
> > > > Ed

--  
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/e37eec3a-6a00-4b52-9374-a344f19d25ff%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/e37eec3a-6a00-4b52-9374-a344f19d25ff%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Maxime\_Nay](https://avatars.discourse-cdn.com/v4/letter/m/edb3f5/32.png) [@Maxime\_Nay](https://discuss.elastic.co/u/Maxime_Nay)\
**Post date:** [July 29, 2014, 3:02am UTC](https://discuss.elastic.co/t/sorting-on-a-custom-script-field-in-java/10912/17 "2014-07-29T03:02:06Z")

</div>

Hi,

I am facing the same issue here. Any idea how we could achieve this?

Thanks!

On Thursday, July 24, 2014 6:29:42 AM UTC-7, M\_20 wrote:

> How can I use ES score in the script?  
> The following script works and I am able to sort the results based on this  
> script:
> 
> String script = "doc['studyscorenormal'].value";  
> ScriptSortBuilder sortScript = SortBuilders.scriptSort(script,  
> "number").order(SortOrder.DESC);
> 
> But when I am using \_score in the script I am getting an error:  
> String script = "doc['studyscorenormal'].value \* \_score";
> 
> Here is the code:
> 
> ```
> SearchResponse response = client
> .prepareSearch(index)
> .setTypes(type)
> .setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
> .setQuery(
> QueryBuilders.fuzzyLikeThisQuery(field)
> .likeText(searchTerm)).setFrom(0)
> .setSize(60)
> .addSort(sortScript)
> .setExplain(true).execute().actionGet();
> 
> ```
> 
> Here is the error message:
> 
> sort[\<custom:"\_script":  
> org.elasticsearch.index.fielddata.fieldcomparator.DoubleScriptDataComparator$InnerSource@65c4edc\>!,]:  
> Query Failed [Failed to execute main query]]; nested:  
> PropertyAccessException[[_Error: unresolvable property or identifier:  
> \_score_]  
> [Near : {... doc['studyscorenormal'].value + \_score ....}]  
> ^  
> [Line: 1, Column: 33]]; }
> 
> On Wednesday, July 23, 2014 4:08:24 PM UTC-5, M\_20 wrote:
> 
> > I found the error !!!!!!!!!!!!!!!!!!
> > 
> > enable dynamic scripting in config file
> > 
> > [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/modules-scripting.html)
> > 
> > On Wednesday, July 23, 2014 3:05:11 PM UTC-5, M\_20 wrote:
> > 
> > > Hi Igor,
> > > 
> > > I wrote a code to sort on custom script( based on ES documentation  
> > > [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-request-sort.html)  
> > > ), but it doesn't work.  
> > > Here are the code and the error message. Any suggestion?
> > > 
> > > ///// journalscorenormal --\> the field's name
> > > 
> > > String script = "doc['journalscorenormal'].  
> > > value";  
> > > ScriptSortBuilder sortSc = SortBuilders.scriptSort(script,"float");  
> > > SearchResponse response = client  
> > > .prepareSearch(index)  
> > > .setTypes(MyVars.index.getType())  
> > > .setSearchType(SearchType.DFS\_QUERY\_THEN\_FETCH)  
> > > .setQuery(  
> > > QueryBuilders.fuzzyLikeThisQuery(field)  
> > > .likeText(searchTerm)).setFrom(0)  
> > > .setSize(60)  
> > > .addSort(sortSc)  
> > > .setExplain(true).execute().actionGet();
> > > 
> > > Failed to parse source  
> > > [{"from":0,"size":60,"query":{"flt":{"fields":["sentence"],"like\_text":"disease"}},"explain":true,"  
> > > _sort":[{"\_script":{"script":"doc['journalscorenormal'].value","type":"float"}}]}]]_];  
> > > nested: ScriptException[dynamic scripting disabled]; }  
> > > at  
> > > org.elasticsearch.action.search.type.TransportSearchTypeAction$BaseAsyncAction.onFirstPhaseResult(TransportSearchTypeAction.java:233)  
> > > at  
> > > org.elasticsearch.action.search.type.TransportSearchTypeAction$BaseAsyncAction$1.onFailure(TransportSearchTypeAction.java:179)  
> > > at  
> > > org.elasticsearch.search.action.SearchServiceTransportAction$23.run(SearchServiceTransportAction.java:523)  
> > > at  
> > > java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)  
> > > at  
> > > java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)  
> > > at java.lang.Thread.run(Thread.java:744)
> > > 
> > > On Tuesday, February 26, 2013 4:30:46 PM UTC-6, echin1999 wrote:
> > > 
> > > > Hi,
> > > > 
> > > > I was wondering if someone could point me to some sample Java code that  
> > > > specifically shows how to sort on a custom script field.
> > > > 
> > > > I see that the SearchRequestBuilder has a method called: addScriptField(name,  
> > > > script, params);
> > > > 
> > > > but i'm not sure how to define the "script" argument. I assume the  
> > > > "name" is the reference to the script field that i would pass into the  
> > > > construction of a FieldSortBuilder()?
> > > > 
> > > > I want to be able to sort my results based on whether or not a property  
> > > > (or multiple properties) of type long contains a long value specified at  
> > > > search time (ie. documents where the criteria holds true will be returned  
> > > > ahead of those documents where the criteria is false).
> > > > 
> > > > I've attached my sample code which does basic sorting on an existing  
> > > > field - that seems to work fine. I now just want to enhance it to be  
> > > > able to sort on a custom script field coded as i describe above. Any  
> > > > pointers are greatly appreciated!
> > > > 
> > > > thanks
> > > > 
> > > > Ed

--  
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/d58f0b36-b614-4932-9507-5900b1c68a27%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/d58f0b36-b614-4932-9507-5900b1c68a27%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:12am UTC](https://discuss.elastic.co/t/sorting-on-a-custom-script-field-in-java/10912/18 "2017-07-06T01:12:40Z")

</div>


