Using wES-client
wES-client use gradle build as a jar project, it's supported for Maven or Gradle project. Or download the jar file then add it into classpath.
Using wES-client for Maven
On ur pom.xml file, add wES-client dependency into dependencies part like this:
<dependency>
<groupId>io.github.datasays</groupId>
<artifactId>wES-client</artifactId>
<version>1.0</version>
<scope>compile</scope>
</dependency>
Using wES-client for Gradle
On ur build.gradle file, add wES-client dependency into dependencies part like this:
compile 'io.github.datasays:wES-client:1.0'
implement org.datasays.wes.core.IConvert
Because of mini class library dependencies, wES-client don't include any Json convert implments, u need implment it or use wES-toolkit org.datasays.wes.toolkit.WGsonConvert
. WGsonConvert
is a Gson implement.
create org.datasays.wes.client.EsHelper
instance and init it
EsHelper esHelper = new EsHelper("http://127.0.0.1:9200");
//set base auth info for all action
esHelper.baseAuth(user, password);
//set log config
esHelper.setLogFlag(LOG.isDebugEnabled(), LOG.isDebugEnabled(), LOG.isDebugEnabled());
//init
esHelper.init(new OkHttpClient.Builder().build(), new WGsonConvert());
Indexing a doc
Index action = esHelper.index(index, type, id);
action.setBody(body);
return esHelper.post(action, Object.class);
search a doc
Search action = esHelper.search(index, type, id);
action.setBody(queryDsl);
return esHelper.post(action, WSearchResult.class, TestDoc.class);
special usage
setup global HTTP cache(read only scene)
OkHttpClient.Builder cBuilder = new OkHttpClient.Builder();
cBuilder.cache(new Cache(new File("./tmp"), 10000));
esHelper.init(cBuilder.build(), new WGsonConvert());
HTTP authentication
authentication for all actions once
esHelper.baseAuth(user, password);
authentication for one action
action.baseAuth(user, password);
wES-client core class
org.datasays.wes.client.EsHelper
The okhttp3 like API to visit the ElasticSearch. It support all ElasticSearch API org.datasays.wes.actions
and Enum types org.datasays.wes.types
. You can use URL ways or send GET/POST/PUT/HEAD/DELETE request with a Json request body. The source code include all comments like this:
org.datasays.wes.core.IConvert
A Java interface for JSON serializer and parser.
org.datasays.wes.client.EsService
The retrofit2 like API to visit the ElasticSearch. It only support to convert Json response to Object and can't pass the params on URL. So it can't used on complex scene. we will fix it with a better implement ASAP.
CodeGen on wES-toolkit
project
org.datasays.wes.toolkit.codegen.EsRestSpecGen
It's the CodeGen java program for the okhttp3 like API. It will gen all codes on package 'org.datasays.wes.actions' and 'org.datasays.wes.types'. The code is will simple, you can modify it as your way.
You can run it as a normal Java program or run it by shell command on wES-toolkit path:
gradle EsRestSpecGen
org.datasays.wes.toolkit.codegen.EsRestSpecGen4Retrofit
It's like the EsRestSpecGen, but it's for The retrofit2 like API EsService.
gradle EsRestSpecGen4Retrofit
ElasticSearch Compatibility
Current version support the lastest ES version v5.x. You also can get another ES Rest API version from https://github.com/elastic/elasticsearch/tree/master/rest-api-spec by other Git branch versions, copy them to override all /wES-toolkit/api/ json files and re-Gen the codes.