Elasticsearch 2.0 script_score from Java API - java

I've just upgraded to ES 2.0.0-rc1.
I use a local node for JUnit testing.
Settings settings = Settings.builder()
.put("script.inline", "on")
.put("script.indexed", "on")
.put("path.home", "/").build();
return NodeBuilder.nodeBuilder()
.settings(settings)
.local(true)
.clusterName("c").node();
My problem is that the upgraded version doesn't see my native scripts.
The query seems like this:
Script script = new Script("myscript", ScriptType.INDEXED, "native", params);
ScoreFunctionBuilder scoreBuilder = ScoreFunctionBuilders.scriptFunction(script);
The output is the following:
...
"functions" : [ {
"script_score" : {
"script" : {
"id" : "myscript",
"lang" : "native",
"params" : {
"searchMode" : "A"
}
}
}
...
This script Plugin is in the Maven dependency list.
It worked well with the former version however with this new version I get the following exception:
Caused by: org.elasticsearch.index.query.QueryParsingException: script_score the script could not be loaded
Caused by: org.elasticsearch.index.IndexNotFoundException: no such index
So how could I install the plugin to a local node?
Edit 1:
https://www.elastic.co/guide/en/elasticsearch/plugins/2.0/plugin-authors.html / Loading plugins from the classpath
might be the solution. Nope.
Edit 2:
It seems that the ScoreFunctionBuilder has been changed.
1.7:
ScoreFunctionBuilder scoreBuilder = ScoreFunctionBuilders.scriptFunction("myscript", "native", params);
2.0:
Script script = new Script("myscript", ScriptType.INDEXED, "native", params);
ScoreFunctionBuilder scoreBuilder = ScoreFunctionBuilders.scriptFunction(script);
However this doesn't fits to native scripts.

I don't know why since it doesn't follow any logic but all you need to do is to use ScriptType.INLINE
Script script = new Script("myscript", ScriptType.INLINE, "native", params);
We can't use INDEXED because elasticsearch will look for an indexed script in its system and since our script isn't indexed per say... it wouldn't work.

Related

How to use different bootclasspaths in Bazel workspace?

I'm currently trying to build a Bazel 0.11.1 workspace with projects that use different Java language levels. The actual application uses Java 7, but for some code that won't ship, I want to settle with a more recent Java version to be able to use the new language features.
I can solve this to some extent by using --javacopt in .bazelrc, setting -source 1.7 -target 1.7 and override the defaults on a project level with the javacopts attribute, but this is not enough to ensure proper Java 7 compatibility when compiling with a more recent Java version. For this I really need to compile Java 7 projects against a Java 7 classpath as well.
The only way to use a custom bootclasspath seems to be via java_toolchain. It works. But I could not found a way to use different bootclasspaths for different projects, because the toolchain affects all projects and unlike with javacopts cannot be overridden at the project level.
Is this a usecase that is simply not (yet?) possible with Bazel? Or is there some trickery to make it work?
It turns out there is a way: write a custom rule that performs compilation.
The java_common module provides an interface to the compiler.
library.bzl
def _impl(ctx):
deps = []
for dep in ctx.attr.deps:
if java_common.provider in dep:
deps.append(dep[java_common.provider])
output_jar = ctx.new_file("lib{0}.jar".format(ctx.label.name))
runtime = java_common.JavaRuntimeInfo
compilation_provider = java_common.compile(
ctx,
source_jars = ctx.files.srcs_jars,
source_files = ctx.files.srcs,
output = output_jar,
javac_opts = ctx.attr.javac_opts,
deps = deps,
strict_deps = ctx.attr.strict_deps,
java_toolchain = ctx.attr.toolchain,
host_javabase = ctx.attr._host_javabase,
resources = ctx.files.resources,
neverlink = ctx.attr.neverlink,
)
return struct(
files = depset([output_jar]),
providers = [compilation_provider],
)
library = rule(
implementation = _impl,
attrs = {
"srcs_jars": attr.label_list(allow_files=True),
"srcs": attr.label_list(allow_files=True),
"javac_opts": attr.string_list(default=[]),
"deps": attr.label_list(),
"strict_deps": attr.string(default="ERROR"),
"toolchain": attr.label(default=Label("#bazel_tools//tools/jdk:toolchain")),
"sourcepath": attr.label_list(),
"resources": attr.label_list(),
"neverlink": attr.bool(default=False),
"_host_javabase": attr.label(default=Label("#local_jdk//:jdk")),
},
fragments = ["java"],
)
This rule I can now use to set a different toolchain for compilation.
BUILD
load('//build/jdk:library.bzl', 'library')
library(
name = "test",
srcs = glob(["src/main/java/**/*.java"]),
# data = glob(["src/main/resources/**"]),
toolchain = "//build/jdk:jdk8",
deps = ["..."],
)
Unfortunately I'm not 100% there yet. java_common.compile does not seem to have an equivalent for the data attribute of java_library, but for the most part the toolchain problem is solved.

htmlwebpackPlugin + Maven - how do I manually inject bundled chunkhash files?

I have an issue where Maven + frontend-maven-plugin and webpack doesn't go well together when I install an entire Maven module; Simply put Webpack the htmlwebpackPlugin will not inject the bundled js/css files the first time I install a Maven module, for some reason, even though a template is provided as such:
new HtmlWebpackPlugin({
template : '../resources/public/index.html',
filename : 'index.html',
inject : 'body',
})
However if I manually run the frontend-maven-plugin after installing the entire Maven module, it will actually inject the correct files, which is rather strange behavior.
To go around this, I wanted to know if there's a manual way to somehow inject these bundled files(I only have three; 1 css, 2 js files) with a chunkhash inside my own index.html template? That would make the build much more consistent.
A snip of my webpack.config.js - as you can see we add the chunkhash to the filenames if we are not in dev.
"use strict";
const ExtractTextPlugin = require("extract-text-webpack-plugin");
const HtmlWebpackPlugin = require('html-webpack-plugin');
let path = require('path');
let webpack = require("webpack");
const PATHS = {
build: path.join(__dirname, '../../../target', 'classes', 'public'),
};
const env = process.env.NODE_ENV;
let isDev = false;
if(env == "dev"){
isDev = true;
}
console.log(`Dev environment: ${isDev}`);
module.exports = {
entry: {
main: './js/index.jsx',
vendor: [
"react","react-dom","axios",
"react-table", "mobx", "mobx-react", "mobx-utils", "lodash"],
},
output: {
path: PATHS.build,
filename: `bundle.${isDev ? '' : '[chunkhash]'}.js`
},
plugins: [
new webpack.optimize.CommonsChunkPlugin({name: "vendor", filename: `/static/js/vendor.bundle.${isDev ? '' : '[chunkhash]'}.js`}),
new ExtractTextPlugin(`/static/css/[name].${isDev ? '' : '[chunkhash]'}.css`),
new HtmlWebpackPlugin({
template : '../resources/public/index.html',
filename : 'index.html',
inject : 'body',
})
],
module: {
loaders: [
// Bunch of loaders
]
},
};
I solved it - the issue was basically that Maven/Spring would take the Index.html(which I used as a template) in resources/public outside my target folder and overwrite it in the target folder - basically overwriting the output from webpackHTMLplugin, which makes logical sense in this context.
I solved it by not having any index.html file in resources/public, but just having a template.html in the src folder where webpack is. Thereby, it Maven/Spring doesn't overwrite the output with the empty template.

Authenticating users via LDAP with Shiro

Total newbie to java/groovy/grails/shiro/you-name-it, so bear with me. I have exhausted tutorials and all the "Shiro LDAP" searches available and still cannot get my project working.
I am running all of this on GGTS with jdk1.7.0_80, Grails 2.3.0, and Shiro 1.2.1.
I have a working project and have successfully ran quick-start-shiro,which built the domains ShiroRole and ShiroUser, the controller authController, the view login.gsp, and the relam ShiroDbRealm. I created a faux user in BootStrap with
def user = new ShiroUser(username: "user123", passwordHash: new Sha256Hash("password").toHex())
user.addToPermissions("*:*")
user.save()
and can successfully log into my homepage, and for all intents and purposes, that is as far as I have gotten. I cannot find a top-down tutorial of how to now log in with my username and password (authenticated through a LDAP server that I have available). From what I understand, I need to create a shiro.ini file and include something along the lines of
[main]
ldapRealm = org.apache.shiro.realm.activedirectory.ActiveDirectoryRealm
ldapRealm.url = ldap://MYURLHERE/
However I don't even know where to put this shiro.ini file. I've seen /src/main/resources, but there is no such directory. Do I manually create this or is it some script creation?
The next step seems to be creating the SecurityManager which reads the shiro.ini somehow with code along the lines of
Factory<org.apache.shiro.mgt.SecurityManager> factory = new IniSecurityManagerFactory("actived.ini");
// Setting up the SecurityManager...
org.apache.shiro.mgt.SecurityManager securityManager = factory.getInstance();
SecurityUtils.setSecurityManager(securityManager);
However this always appears in some Java file in tutorials, but my project is a Groovy project inside of GGTS. Do I need to create a Java file and put it in src/java or something like that?
I've recently found that I may need a ShiroLdapRealm file (similar to ShiroDbRealm) with information like
def appConfig = grailsApplication.config
def ldapUrls = appConfig.ldap.server.url ?: [ "ldap://MYURLHERE/" ]
def searchBase = appConfig.ldap.search.base ?: ""
def searchUser = appConfig.ldap.search.user ?: ""
def searchPass = appConfig.ldap.search.pass ?: ""
def usernameAttribute = appConfig.ldap.username.attribute ?: "uid"
def skipAuthc = appConfig.ldap.skip.authentication ?: false
def skipCredChk = appConfig.ldap.skip.credentialsCheck ?: false
def allowEmptyPass = appConfig.ldap.allowEmptyPasswords != [:] ? appConfig.ldap.allowEmptyPasswords : true
and the corresponding info in Config along the lines of
ldap.server.url = ["ldap://MYRULHERE/"]
ldap.search.base = 'dc=COMPANYNAME,dc=com'
ldap.search.user = '' // if empty or null --> anonymous user lookup
ldap.search.pass = 'password' // only used with non-anonymous lookup
ldap.username.attribute = 'AccountName'
ldap.referral = "follow"
ldap.skip.credentialsCheck = false
ldap.allowEmptyPasswords = false
ldap.skip.authentication = false
But putting all these pieces together hasn't gotten me anywhere! Am I at least on the right track? Any help would be greatly appreciated!
For /src/main/resources it will automatically created for you if you used maven for your project. Moreover, you can also create that directory manually.

Getting InvalidConfigurationException in JGit while pulling remote branch

I'm trying to pull the remote master branch in my currently checked out local branch. Here's the code for it
checkout.setName(branchName).call();
PullCommand pullCommand = git.pull();
System.out.println("Pulling master into " + branchName + "...");
StoredConfig config = git.getRepository().getConfig();
config.setString("branch", "master", "merge", "refs/heads/master");
pullCommand.setRemote("https://github.com/blackblood/TattooShop.git");
pullCommand.setRemoteBranchName("master");
pullResult = pullCommand.setCredentialsProvider(credentialsProvider).call();
When I run the code I get the following error on this line pullCommand.setRemote("https://github.com/blackblood/TattooShop.git");
Error :
org.eclipse.jgit.api.errors.InvalidConfigurationException:
No value for key remote.https://github.com/blackblood/TattooShop.git.url found in configurationCouldn't pull from remote. Terminating...
at org.eclipse.jgit.api.PullCommand.call(PullCommand.java:247)
at upload_gen.Launcher.updateFromRemote(Launcher.java:179)
at upload_gen.Launcher.main(Launcher.java:62)
Following are the contents of my .git/config file
[core]
repositoryformatversion = 0
filemode = false
bare = false
logallrefupdates = true
symlinks = false
ignorecase = true
hideDotFiles = dotGitOnly
[remote "origin"]
url = https://github.com/blackblood/TattooShop.git
fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
remote = origin
merge = refs/heads/master
[remote "heroku"]
url = git#heroku.com:tattooshop.git
fetch = +refs/heads/*:refs/remotes/heroku/*
This seems to be a bug in JGit. According to the JavaDoc of setRemote(), it sets the remote (uri or name) to be used for the pull operation but apparently only the remote name works.
Given your configuration you can work around the issue by using the remote name like this:
pullCommand.setRemote( "origin" );
I recommend to open a bug report in the JGit bugzilla so that this gets fixed in future versions of JGit.

Getting version of Mongo Instance with Java driver

As the title suggests, i want to get the version of the mongo instance the client is connecting to. Currently i am using mongo java driver 2.9.3 and mongo instance is 2.2.2.
I require this, in order to support both $pushAll and $push with $each functions, since former is deprecated from version 2.4 in favor of latter. In short I want to know Java driver equivalent of db.version()
Until future versions of driver presents a method, current solution is following, thanks to hint from here.
DB db = new Mongo("127.0.0.1").getDB("test");//Better use MongoClient since Mongo class is deprecated
System.out.println(db.getMongo().getVersion());//prints 2.9.3 driverversion
CommandResult commandResult = db.command("buildInfo");
System.out.println(commandResult.getString("version"));//prints 2.4.2 Note tried at home since my mongo version is 2.4.2
A little poking around revealed this:
> db.version()
2.4.6
> db.version
function (){
return this.serverBuildInfo().version;
}
> db.serverBuildInfo
function (){
return this._adminCommand( "buildinfo" );
}
> db.runCommand('buildinfo')
{
"version" : "2.4.6",
"gitVersion" : "b9925db5eac369d77a3a5f5d98a145eaaacd9673",
"sysInfo" : "Linux ip-10-2-29-40 2.6.21.7-2.ec2.v1.2.fc8xen #1 SMP Fri Nov 20 17:48:28 EST 2009 x86_64 BOOST_LIB_VERSION=1_49",
"loaderFlags" : "-fPIC -pthread -rdynamic",
"compilerFlags" : "-Wnon-virtual-dtor -Woverloaded-virtual -fPIC -fno-strict-aliasing -ggdb -pthread -Wall -Wsign-compare -Wno-unknown-pragmas -Winvalid-pch -Werror -pipe -fno-builtin-memcmp -O3",
"allocator" : "tcmalloc",
"versionArray" : [
2,
4,
6,
0
],
"javascriptEngine" : "V8",
"bits" : 64,
"debug" : false,
"maxBsonObjectSize" : 16777216,
"ok" : 1
}
So you just can use equivalent of runCommand in your java code (don't know java driver, I'm ruby guy).
This one works for me (Java client 3.5.0):
MongoClient client = //..
String version = client.getDatabase("dbname")
.runCommand(new BsonDocument("buildinfo", new BsonString("")))
.get("version")
.toString();
This is what I did:
MongoClient mongoClient = MongoClients.create("mongodb://localhost:27017");
MongoDatabase db = mongoClient.getDatabase("test");
Document document = db.runCommand(new Document("buildInfo",1));
System.out.println("MongoDB Version: "+document.getString("version"));
My Configs
MongoDB JVM Driver: 4.1
JDK 11

Categories

Resources