Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added the first version of the groovy client for elastic search
- Loading branch information
Showing
10 changed files
with
288 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# used to ignore certain files by git | ||
# mac osx files | ||
.DS_Store | ||
#gradle build files | ||
.gradle | ||
build | ||
# intellij files | ||
.idea/* | ||
*.ipr | ||
*.iws | ||
*.iml | ||
target/ | ||
out/* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
apply plugin: 'groovy' | ||
|
||
repositories { | ||
mavenCentral() | ||
} | ||
|
||
dependencies { | ||
groovy 'org.codehaus.groovy:groovy:2.0.6' | ||
groovy 'org.codehaus.groovy:groovy-all:2.0.6' | ||
groovy ('org.codehaus.groovy:groovy-xmlrpc:0.8') { | ||
exclude module: 'groovy-all' | ||
} | ||
groovy 'commons-cli:commons-cli:1.2' | ||
groovy 'log4j:log4j:1.2.16' | ||
|
||
|
||
groovy ('org.elasticsearch:elasticsearch-lang-groovy:1.2.0') { | ||
exclude module: 'groovy-all' | ||
} | ||
} | ||
|
||
// compile 'org.ccil.cowan.tagsoup:tagsoup:1.2.1' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import org.elasticsearch.groovy.node.GNode | ||
import org.elasticsearch.groovy.node.GNodeBuilder | ||
import org.elasticsearch.search.SearchHit | ||
|
||
import static org.elasticsearch.groovy.node.GNodeBuilder.nodeBuilder | ||
|
||
org.elasticsearch.groovy.common.xcontent.GXContentBuilder.rootResolveStrategy = Closure.DELEGATE_FIRST; | ||
|
||
// Start the connection | ||
|
||
GNodeBuilder nodeBuilder = nodeBuilder(); | ||
nodeBuilder.settings { | ||
node { | ||
client = true | ||
} | ||
|
||
cluster { | ||
name = "jc-elasticsearch" | ||
} | ||
} | ||
|
||
GNode node = nodeBuilder.node() | ||
|
||
// Do something with the connection | ||
def search = node.client.search { | ||
indices "ro" | ||
types "rijksoverheid" | ||
source { | ||
query { | ||
term(title_nl: "drinken") | ||
} | ||
} | ||
} | ||
|
||
search.response.hits.each {SearchHit hit -> | ||
println "Got hit $hit.id from $hit.index/$hit.type" | ||
println "Title: $hit.source.title_nl" | ||
} | ||
|
||
//System.in.withReader { | ||
// print 'input: ' | ||
// println it.readLine() | ||
//} | ||
|
||
// close the connection | ||
|
||
node.stop().close() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import nl.gridshore.elasticsearch.ElasticSearchGateway | ||
import nl.gridshore.wordpress.BlogItem | ||
import nl.gridshore.wordpress.WordpressReader | ||
|
||
/** | ||
* @author Jettro Coenradie | ||
*/ | ||
|
||
def rpcUrl = "http://www.gridshore.nl/xmlrpc.php" | ||
def username = "admin" | ||
def password = "Alwin245" | ||
|
||
def reader = new WordpressReader(rpcUrl,username,password) | ||
|
||
def posts = reader.obtainMostRecentPosts(100) | ||
|
||
ElasticSearchGateway gateway = new ElasticSearchGateway() | ||
|
||
posts.each {BlogItem item -> | ||
println item.title | ||
gateway.indexBlogItem(item) | ||
} | ||
|
||
System.in.withReader { | ||
print 'input: ' | ||
println it.readLine() | ||
} | ||
|
||
gateway.close() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import nl.gridshore.elasticsearch.ElasticSearchGateway | ||
|
||
/** | ||
* @author Jettro Coenradie | ||
*/ | ||
|
||
ElasticSearchGateway gateway = new ElasticSearchGateway() | ||
|
||
gateway.countAllDocuments() | ||
|
||
gateway.queryIndex("groovy") | ||
|
||
System.in.withReader { | ||
print 'input: ' | ||
println it.readLine() | ||
} | ||
|
||
gateway.close() |
2 changes: 2 additions & 0 deletions
2
groovy-es-client/src/main/groovy/data/elasticsearch/nodes/0/_state/global-12
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
:) | ||
��meta-data��version؈templates���� |
86 changes: 86 additions & 0 deletions
86
groovy-es-client/src/main/groovy/nl/gridshore/elasticsearch/ElasticSearchGateway.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package nl.gridshore.elasticsearch | ||
|
||
import nl.gridshore.wordpress.BlogItem | ||
import org.elasticsearch.action.index.IndexResponse | ||
import org.elasticsearch.groovy.common.xcontent.GXContentBuilder | ||
import org.elasticsearch.groovy.node.GNode | ||
import org.elasticsearch.groovy.node.GNodeBuilder | ||
import org.elasticsearch.search.SearchHit | ||
|
||
import static org.elasticsearch.groovy.node.GNodeBuilder.nodeBuilder | ||
|
||
/** | ||
* @author Jettro Coenradie | ||
*/ | ||
class ElasticSearchGateway { | ||
GNode node | ||
|
||
ElasticSearchGateway() { | ||
GXContentBuilder.rootResolveStrategy = Closure.DELEGATE_FIRST; // required to use a closure as settings | ||
|
||
GNodeBuilder nodeBuilder = nodeBuilder(); | ||
nodeBuilder.settings { | ||
node { | ||
client = true | ||
} | ||
|
||
cluster { | ||
name = "jc-elasticsearch" | ||
} | ||
} | ||
|
||
node = nodeBuilder.node() | ||
} | ||
|
||
public queryIndex(theTerm) { | ||
def search = node.client.search { | ||
indices : "gridshore" | ||
types : "blog" | ||
source { | ||
query { | ||
term(_all: theTerm) | ||
} | ||
} | ||
} | ||
|
||
search.response.hits.each {SearchHit hit -> | ||
println "Got hit $hit.id from $hit.index/$hit.type with title $hit.source.title" | ||
} | ||
} | ||
|
||
public countAllDocuments() { | ||
def count = node.client.count { | ||
indices : "gridshore" | ||
parameterTypes : "blog" | ||
} | ||
|
||
println "Number of found blog items : $count.response.count" | ||
} | ||
|
||
public indexBlogItem(BlogItem blogItem) { | ||
def future = node.client.index { | ||
index = "gridshore" | ||
type = "blog" | ||
source { | ||
blogId = blogItem.id | ||
link = blogItem.link | ||
status = blogItem.status | ||
keywords = blogItem.keywords | ||
title = blogItem.title | ||
createdOn_date = blogItem.createdOn | ||
content = blogItem.content | ||
categories = blogItem.categories | ||
author = blogItem.author | ||
slug = blogItem.slug | ||
} | ||
} | ||
// a listener can be added to the future | ||
future.success = {IndexResponse response -> | ||
println "Indexed $response.index/$response.type/$response.id" | ||
} | ||
} | ||
|
||
public close() { | ||
node.stop().close() | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
groovy-es-client/src/main/groovy/nl/gridshore/wordpress/BlogItem.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package nl.gridshore.wordpress | ||
|
||
/** | ||
* Value object for items read from the blog, complete content of the blog item is stored in this object. | ||
* | ||
* @author Jettro Coenradie | ||
*/ | ||
class BlogItem { | ||
def id | ||
def link | ||
def status | ||
def keywords | ||
def title | ||
def createdOn | ||
def content | ||
def categories | ||
def author | ||
def slug | ||
} |
47 changes: 47 additions & 0 deletions
47
groovy-es-client/src/main/groovy/nl/gridshore/wordpress/WordpressReader.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package nl.gridshore.wordpress | ||
|
||
import groovy.net.xmlrpc.XMLRPCServerProxy | ||
|
||
/** | ||
* This class exposes a few methods that help in reading items from a wordpress blog. The wordpress xmlrpc api is | ||
* used to get information from the blog. You initialize this object with the url to the xmlrpc api and the required | ||
* username and password. | ||
* | ||
* @author Jettro Coenradie | ||
*/ | ||
class WordpressReader { | ||
private String xmlrpcUrl | ||
private String username | ||
private String password | ||
|
||
private XMLRPCServerProxy serverProxy; | ||
|
||
def WordpressReader(xmlrpcUrl, username, password) { | ||
this.xmlrpcUrl = xmlrpcUrl | ||
this.username = username | ||
this.password = password | ||
|
||
serverProxy = new XMLRPCServerProxy(xmlrpcUrl) | ||
serverProxy.setBasicAuth(username, password) | ||
} | ||
|
||
def obtainMostRecentPosts(int number = 10) { | ||
def posts = [] | ||
def foundPosts = serverProxy.metaWeblog.getRecentPosts(1, username, password, number) | ||
foundPosts.each {post -> | ||
def blogItem = new BlogItem() | ||
blogItem.id = post['postid'] | ||
blogItem.link = post['permaLink'] | ||
blogItem.status = post['post_status'] | ||
blogItem.keywords = post['mt_keywords'] | ||
blogItem.title = post['title'] | ||
blogItem.createdOn = post['dateCreated'] | ||
blogItem.content = post['description'] | ||
blogItem.categories = post['categories'] | ||
blogItem.author = post['wp_author_display_name'] | ||
blogItem.slug = post['wp_slug'] | ||
posts.add(blogItem) | ||
} | ||
return posts | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
log4j.appender.Stdout=org.apache.log4j.ConsoleAppender | ||
log4j.appender.Stdout.layout=org.apache.log4j.PatternLayout | ||
log4j.appender.Stdout.layout.conversionPattern=%-5p - %-10.10t - %-26.26c{1} - %m\n | ||
|
||
log4j.rootLogger=INFO,Stdout |