Sorting on a custom script field in Java


(echin1999-2) #1

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.
For more options, visit https://groups.google.com/groups/opt_out.


(Igor Motov) #2

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 Queryhttp://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.
For more options, visit https://groups.google.com/groups/opt_out.


(echin1999-2) #3

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 Queryhttp://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.
For more options, visit https://groups.google.com/groups/opt_out.


(Igor Motov) #4

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 Queryhttp://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.
For more options, visit https://groups.google.com/groups/opt_out.


(echin1999-2) #5

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 Queryhttp://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.
For more options, visit https://groups.google.com/groups/opt_out.


(Igor Motov) #6

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 Queryhttp://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.
For more options, visit https://groups.google.com/groups/opt_out.


(echin1999-2) #7

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 Queryhttp://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.
For more options, visit https://groups.google.com/groups/opt_out.


(echin1999-2) #8

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
elastic search 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 Queryhttp://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.
For more options, visit https://groups.google.com/groups/opt_out.


(Igor Motov) #9

You can include the the test that I posted class that I sent you into a
class inherited from AbstractNodesTestshttps://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
elastic search 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 Queryhttp://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.
For more options, visit https://groups.google.com/groups/opt_out.


(echin1999-2) #10

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 AbstractNodesTestshttps://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
elastic search 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 Queryhttp://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.
For more options, visit https://groups.google.com/groups/opt_out.


(Igor Motov) #11

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 AbstractNodesTestshttps://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
elastic search 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 Queryhttp://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.
For more options, visit https://groups.google.com/groups/opt_out.


(echin1999-2) #12

I prepared two log files: 1) log file when the NaN
occurs: 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

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 AbstractNodesTestshttps://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
elastic search 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 Queryhttp://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.
For more options, visit https://groups.google.com/groups/opt_out.


(M_20) #13

Hi Igor,

I wrote a code to sort on custom script( based on ES documentation
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.
To view this discussion on the web visit https://groups.google.com/d/msgid/elasticsearch/89ace971-b938-4181-a27c-f7a842df4a82%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


(M_20) #14

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

enable dynamic scripting in config file

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
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.
To view this discussion on the web visit https://groups.google.com/d/msgid/elasticsearch/43320492-9521-4ba5-9782-4afe782069b8%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


(M_20) #15

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

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
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.
To view this discussion on the web visit https://groups.google.com/d/msgid/elasticsearch/e20fc8b7-f598-4b40-9ffe-09cf4288e73b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


(Maxime Nay) #16

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

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
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.
To view this discussion on the web visit https://groups.google.com/d/msgid/elasticsearch/e37eec3a-6a00-4b52-9374-a344f19d25ff%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


(Maxime Nay) #17

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

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
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.
To view this discussion on the web visit https://groups.google.com/d/msgid/elasticsearch/d58f0b36-b614-4932-9507-5900b1c68a27%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


(system) #18