Skip to content

Instantly share code, notes, and snippets.

@letusfly85
Last active August 29, 2015 14:00
Show Gist options
  • Save letusfly85/11282198 to your computer and use it in GitHub Desktop.
Save letusfly85/11282198 to your computer and use it in GitHub Desktop.
package test2es
import org.elasticsearch.client.Client
import org.elasticsearch.node.NodeBuilder
import org.elasticsearch.node.Node
class Access2ES {
private final Client client
public Client getClient() {
return this.client
}
/**
* constructor
*
*
* @param builder
*/
public Access2ES(Builder builder) {
this.client = builder.client
}
/**
* Builder
*
*/
public static class Builder {
private Node node
private Client client
public Builder() {
this.node = NodeBuilder.nodeBuilder().clusterName("elasticsearch").client(true).node()
//セッティング情報をインスタンス生成時にお試しで出力
println(node.settings())
this.client = node.client()
}
public Access2ES build() {
return new Access2ES(this)
}
}
}
apply plugin: 'java'
apply plugin: 'groovy'
apply plugin: 'application'
version = '1.0'
repositories {
mavenCentral()
}
dependencies {
compile 'org.codehaus.groovy:groovy-all:2.1.5'
compile 'org.elasticsearch:elasticsearch:1.1.1'
testCompile group: 'junit', name: 'junit', version: '4.11'
}
mainClassName = 'test2es.Main'
package test2es
class Main {
public static void main(String[] args) {
Access2ES.Builder builder = new Access2ES.Builder()
Access2ES access2ES = builder.build()
println(access2ES.client.getClass())
}
}
package test2es
import org.elasticsearch.action.search.SearchResponse
import org.elasticsearch.action.search.SearchType
import org.elasticsearch.index.query.QueryBuilders
class Main {
public static void main(String[] args) {
Access2ES.Builder builder = new Access2ES.Builder()
Access2ES access2ES = builder.build()
SearchResponse response = access2ES.client.prepareSearch("index_name")
.setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
.setQuery(QueryBuilders.queryString("keyword"))
.setFrom(0).setSize(1).setExplain(false)
.execute()
.actionGet()
println(response.toString())
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment