Skip to content

Commit

Permalink
Added the first version of the groovy client for elastic search
Browse files Browse the repository at this point in the history
  • Loading branch information
jettro committed Jan 27, 2013
1 parent 07b119e commit f15884a
Show file tree
Hide file tree
Showing 10 changed files with 288 additions and 0 deletions.
13 changes: 13 additions & 0 deletions groovy-es-client/.gitignore
@@ -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/*
22 changes: 22 additions & 0 deletions groovy-es-client/build.gradle
@@ -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'
47 changes: 47 additions & 0 deletions groovy-es-client/src/main/groovy/DoSomethingWithES.groovy
@@ -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()
29 changes: 29 additions & 0 deletions groovy-es-client/src/main/groovy/IndexGridshore.groovy
@@ -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()
18 changes: 18 additions & 0 deletions groovy-es-client/src/main/groovy/SearchIndex.groovy
@@ -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()
@@ -0,0 +1,2 @@
:)
��meta-data��version؈templates����
@@ -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()
}
}
@@ -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
}
@@ -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
}
}
5 changes: 5 additions & 0 deletions groovy-es-client/src/main/resources/log4j.properties
@@ -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

0 comments on commit f15884a

Please sign in to comment.