Score boosting (via Java API)

Hi,

Is there a way to use a boost expression in Elasticsearch, via the
Java interface? If so, what's the procedure?

For example, when using Solr, I sometimes used boost expressions such
as this:

{!boost b=sum(product(rord(docType),
10),product(recip(ms(NOW,createdOn),3.16e-11,1,1),sum(product(clicks,
0.1),likes)))}

(where docType, createdOn, clicks and likes are all the names of
fields in my documents).

Can I add an expression of this type to my SearchRequestBuilder?

Thanks.

Can I do something like add a script field and then specify the script
field as the sort order?

On Jan 24, 3:04 pm, Frank LaRosa fr...@studyblue.com wrote:

Hi,

Is there a way to use a boost expression in Elasticsearch, via the
Java interface? If so, what's the procedure?

For example, when using Solr, I sometimes used boost expressions such
as this:

{!boost b=sum(product(rord(docType),
10),product(recip(ms(NOW,createdOn),3.16e-11,1,1),sum(product(clicks,
0.1),likes)))}

(where docType, createdOn, clicks and likes are all the names of
fields in my documents).

Can I add an expression of this type to my SearchRequestBuilder?

Thanks.

You can use a boost script with parameters and implement
NativeScriptFactory.

Usage would be e.g.

QueryBuilders.customScoreQuery(QueryBuilders.matchAllQuery()).
script("mybooster").lang("native").param("friends", friends)

Then you need to specify that Java class in the elasticsearch config
file (or via API)

"index", {
      ...
},
"script" : {
    "native" : {
        "mybooster" : {
            "type": "de.jetwick.es.BoostSearchScript"
        }
    }
}

The class itself looks like

class BoostSearchScript implements NativeScriptFactory {

@Override public ExecutableScript newScript(final Map<String, Object>
params) {
// friends is of type ArrayList as it was serialized via json
even it was Set before
final Collection friends = (Collection)
params.get("friends");

return new AbstractFloatSearchScript() {

  @Override public float runAsFloat() {
    float ret =

doc().numeric("myPreCalculatedScore").getFloatValue();
// use params here somehow
return ret * friends.size();
}};
...

Peter.

On 24 Jan., 22:33, Frank LaRosa fr...@studyblue.com wrote:

Can I do something like add a script field and then specify the script
field as the sort order?

On Jan 24, 3:04 pm, Frank LaRosa fr...@studyblue.com wrote:

Hi,

Is there a way to use a boost expression in Elasticsearch, via the
Java interface? If so, what's the procedure?

For example, when using Solr, I sometimes used boost expressions such
as this:

{!boost b=sum(product(rord(docType),
10),product(recip(ms(NOW,createdOn),3.16e-11,1,1),sum(product(clicks,
0.1),likes)))}

(where docType, createdOn, clicks and likes are all the names of
fields in my documents).

Can I add an expression of this type to my SearchRequestBuilder?

Thanks.

You don't have to implement native (Java) script, you can simply use script sorting (SearchRequestBuilder#addSort, with SortBuilders as factory), or custom score script.

On Wednesday, January 25, 2012 at 10:23 AM, Karussell wrote:

You can use a boost script with parameters and implement
NativeScriptFactory.

Usage would be e.g.

QueryBuilders.customScoreQuery(QueryBuilders.matchAllQuery()).
script("mybooster").lang("native").param("friends", friends)

Then you need to specify that Java class in the elasticsearch config
file (or via API)

"index", {
...
},
"script" : {
"native" : {
"mybooster" : {
"type": "de.jetwick.es.BoostSearchScript"
}
}
}

The class itself looks like

class BoostSearchScript implements NativeScriptFactory {

@Override public ExecutableScript newScript(final Map<String, Object>
params) {
// friends is of type ArrayList as it was serialized via json
even it was Set before
final Collection friends = (Collection)
params.get("friends");

return new AbstractFloatSearchScript() {

@Override public float runAsFloat() {
float ret =
doc().numeric("myPreCalculatedScore").getFloatValue();
// use params here somehow
return ret * friends.size();
}};
...

Peter.

On 24 Jan., 22:33, Frank LaRosa <fr...@studyblue.com (http://studyblue.com)> wrote:

Can I do something like add a script field and then specify the script
field as the sort order?

On Jan 24, 3:04 pm, Frank LaRosa <fr...@studyblue.com (http://studyblue.com)> wrote:

Hi,

Is there a way to use a boost expression in Elasticsearch, via the
Java interface? If so, what's the procedure?

For example, when using Solr, I sometimes used boost expressions such
as this:

{!boost b=sum(product(rord(docType),
10),product(recip(ms(NOW,createdOn),3.16e-11,1,1),sum(product(clicks,
0.1),likes)))}

(where docType, createdOn, clicks and likes are all the names of
fields in my documents).

Can I add an expression of this type to my SearchRequestBuilder?

Thanks.

Thanks for the reply.

Question: my app is connected to a remote Elasitcsearch server (via
TransportClient). Where does this Java class need to exist -- only on
my client, or on the server as well?

On Jan 25, 2:23 am, Karussell tableyourt...@googlemail.com wrote:

You can use a boost script with parameters and implement
NativeScriptFactory.

Usage would be e.g.

QueryBuilders.customScoreQuery(QueryBuilders.matchAllQuery()).
script("mybooster").lang("native").param("friends", friends)

Then you need to specify that Java class in the elasticsearch config
file (or via API)

"index", {
      ...
},
"script" : {
    "native" : {
        "mybooster" : {
            "type": "de.jetwick.es.BoostSearchScript"
        }
    }
}

The class itself looks like

class BoostSearchScript implements NativeScriptFactory {

@Override public ExecutableScript newScript(final Map<String, Object>
params) {
// friends is of type ArrayList as it was serialized via json
even it was Set before
final Collection friends = (Collection)
params.get("friends");

return new AbstractFloatSearchScript() {

  @Override public float runAsFloat() {
    float ret =

doc().numeric("myPreCalculatedScore").getFloatValue();
// use params here somehow
return ret * friends.size();
}};
...

Peter.

On 24 Jan., 22:33, Frank LaRosa fr...@studyblue.com wrote:

Can I do something like add a script field and then specify the script
field as the sort order?

On Jan 24, 3:04 pm, Frank LaRosa fr...@studyblue.com wrote:

Hi,

Is there a way to use a boost expression in Elasticsearch, via the
Java interface? If so, what's the procedure?

For example, when using Solr, I sometimes used boost expressions such
as this:

{!boost b=sum(product(rord(docType),
10),product(recip(ms(NOW,createdOn),3.16e-11,1,1),sum(product(clicks,
0.1),likes)))}

(where docType, createdOn, clicks and likes are all the names of
fields in my documents).

Can I add an expression of this type to my SearchRequestBuilder?

Thanks.

Which classes? All the search related classes are part of the elasticsearch jar.

On Wednesday, January 25, 2012 at 7:07 PM, Frank LaRosa wrote:

Thanks for the reply.

Question: my app is connected to a remote Elasitcsearch server (via
TransportClient). Where does this Java class need to exist -- only on
my client, or on the server as well?

On Jan 25, 2:23 am, Karussell <tableyourt...@googlemail.com (http://googlemail.com)> wrote:

You can use a boost script with parameters and implement
NativeScriptFactory.

Usage would be e.g.

QueryBuilders.customScoreQuery(QueryBuilders.matchAllQuery()).
script("mybooster").lang("native").param("friends", friends)

Then you need to specify that Java class in the elasticsearch config
file (or via API)

"index", {
      ...
},
"script" : {
    "native" : {
        "mybooster" : {
            "type": "de.jetwick.es.BoostSearchScript"
        }
    }
}

The class itself looks like

class BoostSearchScript implements NativeScriptFactory {

@Override public ExecutableScript newScript(final Map<String, Object>
params) {
// friends is of type ArrayList as it was serialized via json
even it was Set before
final Collection friends = (Collection)
params.get("friends");

return new AbstractFloatSearchScript() {

  @Override public float runAsFloat() {
    float ret =

doc().numeric("myPreCalculatedScore").getFloatValue();
// use params here somehow
return ret * friends.size();
}};
...

Peter.

On 24 Jan., 22:33, Frank LaRosa <fr...@studyblue.com (http://studyblue.com)> wrote:

Can I do something like add a script field and then specify the script
field as the sort order?

On Jan 24, 3:04 pm, Frank LaRosa <fr...@studyblue.com (http://studyblue.com)> wrote:

Hi,

Is there a way to use a boost expression in Elasticsearch, via the
Java interface? If so, what's the procedure?

For example, when using Solr, I sometimes used boost expressions such
as this:

{!boost b=sum(product(rord(docType),
10),product(recip(ms(NOW,createdOn),3.16e-11,1,1),sum(product(clicks,
0.1),likes)))}

(where docType, createdOn, clicks and likes are all the names of
fields in my documents).

Can I add an expression of this type to my SearchRequestBuilder?

Thanks.

The example provided by Peter shows a custom class (class
BoostSearchScript implements NativeScriptFactory). It's unclear to me
whether this class must be installed at the server or if it just needs
to be available on the client.

He also mentions changing a configuration file -- again, is this a
server-side configuration change or client-side?

I'd rather not have to make server-side changes, since it greatly adds
to the complexity of maintaining my environment.

On Jan 25, 11:33 am, Shay Banon kim...@gmail.com wrote:

Which classes? All the search related classes are part of the elasticsearch jar.

On Wednesday, January 25, 2012 at 7:07 PM, Frank LaRosa wrote:

Thanks for the reply.

Question: my app is connected to a remote Elasitcsearch server (via
TransportClient). Where does this Java class need to exist -- only on
my client, or on the server as well?

On Jan 25, 2:23 am, Karussell <tableyourt...@googlemail.com (http://googlemail.com)> wrote:

You can use a boost script with parameters and implement
NativeScriptFactory.

Usage would be e.g.

QueryBuilders.customScoreQuery(QueryBuilders.matchAllQuery()).
script("mybooster").lang("native").param("friends", friends)

Then you need to specify that Java class in the elasticsearch config
file (or via API)

"index", {
      ...
},
"script" : {
    "native" : {
        "mybooster" : {
            "type": "de.jetwick.es.BoostSearchScript"
        }
    }
}

The class itself looks like

class BoostSearchScript implements NativeScriptFactory {

@Override public ExecutableScript newScript(final Map<String, Object>
params) {
// friends is of type ArrayList as it was serialized via json
even it was Set before
final Collection friends = (Collection)
params.get("friends");

return new AbstractFloatSearchScript() {
  @Override public float runAsFloat() {
    float ret =

doc().numeric("myPreCalculatedScore").getFloatValue();
// use params here somehow
return ret * friends.size();
}};
...

Peter.

On 24 Jan., 22:33, Frank LaRosa <fr...@studyblue.com (http://studyblue.com)> wrote:

Can I do something like add a script field and then specify the script
field as the sort order?

On Jan 24, 3:04 pm, Frank LaRosa <fr...@studyblue.com (http://studyblue.com)> wrote:

Hi,

Is there a way to use a boost expression in Elasticsearch, via the
Java interface? If so, what's the procedure?

For example, when using Solr, I sometimes used boost expressions such
as this:

{!boost b=sum(product(rord(docType),
10),product(recip(ms(NOW,createdOn),3.16e-11,1,1),sum(product(clicks,
0.1),likes)))}

(where docType, createdOn, clicks and likes are all the names of
fields in my documents).

Can I add an expression of this type to my SearchRequestBuilder?

Thanks.

When you say "script sorting", what kind of a script are you talking
about?

I'm a Java programmer -- I don't know any scripting languages. I'd
prefer to just write in Java unless it's impossible.

On Jan 25, 10:36 am, Shay Banon kim...@gmail.com wrote:

You don't have to implement native (Java) script, you can simply use script sorting (SearchRequestBuilder#addSort, with SortBuilders as factory), or custom score script.

On Wednesday, January 25, 2012 at 10:23 AM, Karussell wrote:

You can use a boost script with parameters and implement
NativeScriptFactory.

Usage would be e.g.

QueryBuilders.customScoreQuery(QueryBuilders.matchAllQuery()).
script("mybooster").lang("native").param("friends", friends)

Then you need to specify that Java class in the elasticsearch config
file (or via API)

"index", {
...
},
"script" : {
"native" : {
"mybooster" : {
"type": "de.jetwick.es.BoostSearchScript"
}
}
}

The class itself looks like

class BoostSearchScript implements NativeScriptFactory {

@Override public ExecutableScript newScript(final Map<String, Object>
params) {
// friends is of type ArrayList as it was serialized via json
even it was Set before
final Collection friends = (Collection)
params.get("friends");

return new AbstractFloatSearchScript() {

@Override public float runAsFloat() {
float ret =
doc().numeric("myPreCalculatedScore").getFloatValue();
// use params here somehow
return ret * friends.size();
}};
...

Peter.

On 24 Jan., 22:33, Frank LaRosa <fr...@studyblue.com (http://studyblue.com)> wrote:

Can I do something like add a script field and then specify the script
field as the sort order?

On Jan 24, 3:04 pm, Frank LaRosa <fr...@studyblue.com (http://studyblue.com)> wrote:

Hi,

Is there a way to use a boost expression in Elasticsearch, via the
Java interface? If so, what's the procedure?

For example, when using Solr, I sometimes used boost expressions such
as this:

{!boost b=sum(product(rord(docType),
10),product(recip(ms(NOW,createdOn),3.16e-11,1,1),sum(product(clicks,
0.1),likes)))}

(where docType, createdOn, clicks and likes are all the names of
fields in my documents).

Can I add an expression of this type to my SearchRequestBuilder?

Thanks.

Then write it Java, all is explained here: Elasticsearch Platform — Find real-time answers at scale | Elastic.

On Wednesday, January 25, 2012 at 8:21 PM, Frank LaRosa wrote:

When you say "script sorting", what kind of a script are you talking
about?

I'm a Java programmer -- I don't know any scripting languages. I'd
prefer to just write in Java unless it's impossible.

On Jan 25, 10:36 am, Shay Banon <kim...@gmail.com (http://gmail.com)> wrote:

You don't have to implement native (Java) script, you can simply use script sorting (SearchRequestBuilder#addSort, with SortBuilders as factory), or custom score script.

On Wednesday, January 25, 2012 at 10:23 AM, Karussell wrote:

You can use a boost script with parameters and implement
NativeScriptFactory.

Usage would be e.g.

QueryBuilders.customScoreQuery(QueryBuilders.matchAllQuery()).
script("mybooster").lang("native").param("friends", friends)

Then you need to specify that Java class in the elasticsearch config
file (or via API)

"index", {
...
},
"script" : {
"native" : {
"mybooster" : {
"type": "de.jetwick.es.BoostSearchScript"
}
}
}

The class itself looks like

class BoostSearchScript implements NativeScriptFactory {

@Override public ExecutableScript newScript(final Map<String, Object>
params) {
// friends is of type ArrayList as it was serialized via json
even it was Set before
final Collection friends = (Collection)
params.get("friends");

return new AbstractFloatSearchScript() {

@Override public float runAsFloat() {
float ret =
doc().numeric("myPreCalculatedScore").getFloatValue();
// use params here somehow
return ret * friends.size();
}};
...

Peter.

On 24 Jan., 22:33, Frank LaRosa <fr...@studyblue.com (http://studyblue.com)> wrote:

Can I do something like add a script field and then specify the script
field as the sort order?

On Jan 24, 3:04 pm, Frank LaRosa <fr...@studyblue.com (http://studyblue.com)> wrote:

Hi,

Is there a way to use a boost expression in Elasticsearch, via the
Java interface? If so, what's the procedure?

For example, when using Solr, I sometimes used boost expressions such
as this:

{!boost b=sum(product(rord(docType),
10),product(recip(ms(NOW,createdOn),3.16e-11,1,1),sum(product(clicks,
0.1),likes)))}

(where docType, createdOn, clicks and likes are all the names of
fields in my documents).

Can I add an expression of this type to my SearchRequestBuilder?

Thanks.