Monday, May 13, 2013

Run groovy script via Jenkins CLI

Jenkins supports ssh authentication in CLI.

Below is a command to verify that I am authenticated:

java -jar jenkins-cli.jar -s http://myserver/jenkins  who-am-i

    Authenticated as: myuser
    Authorities:
        authenticated

However you still would meet permission error when running groovy script in CLI.

java -jar jenkins-cli.jar -s http://myserver/jenkins groovysh 'jenkins.model.Jenkins.instance.pluginManager.plugins.each { println("${it.longName} - ${it.version}") };'

Exception in thread "main" java.lang.reflect.UndeclaredThrowableException
at $Proxy2.main(Unknown Source)
at hudson.cli.CLI.execute(CLI.java:271)
at hudson.cli.CLI._main(CLI.java:417)
at hudson.cli.CLI.main(CLI.java:322)

It's a bug of Jenkins. The workaround is create a groovy script, then run that script via Jenkins CLI.

java -jar jenkins-cli.jar -s http://myserver/jenkins/ groovy test_script.gsh

Saturday, May 11, 2013

Solr boost examples

The index has a field named 'create_time' that is the timestamp of document created time. The query string can boost the latest created document like below,

{!boost b=recip(ms(NOW,create_time),3.16e-11,0.08,0.05)}name:keyword

There is another field named 'important' that indicates whether the document is important or not. The query string can boost the document is important like below,

q={!boost b=$importfunc}name:keyword&importfunc=query({!v='important:true'})

Above query string uses a sub query in boost function.

Finally I want to boost both above two fields, and 'important' field has higher priority. The query string looks like below,

defType=edismax&q=name:keyword&bf=query({!v='import:true'})^20.0 recip(ms(NOW,create_time),3.16e-11,0.08,0.05)^10.0")