Created
July 24, 2012 13:35
-
-
Save karussell/3169940 to your computer and use it in GitHub Desktop.
Accessing ElasticSearch from Java without the ElasticSearch jar
This file contains hidden or 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
{ | |
"chat" : { | |
"dynamic_templates" : [ | |
{ | |
"template_date" : { | |
"match" : "*_dt", | |
"mapping" : { | |
"type" : "date", | |
"store" : "yes" | |
} | |
} | |
}, { | |
"template_text" : { | |
"match" : "*_t", | |
"match_mapping_type" : "string", | |
"mapping" : { | |
"type" : "string", | |
"index": "analyzed", | |
"store" : "yes" | |
} | |
} | |
}, { | |
"template_string" : { | |
"match" : "*", | |
"match_mapping_type" : "string", | |
"mapping" : { | |
"type" : "string", | |
"index" : "not_analyzed", | |
"store" : "yes" | |
} | |
} | |
} | |
], | |
"_all" : { | |
"enabled" : false | |
} | |
} | |
} |
This file contains hidden or 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
/** Just a quick snippet to show how to use the REST ElasticSearch in Java - grab the parts you like ;) | |
* Instead of using the official Java API, where adding the complete ~20MB ElasticSearch jar is necessary. | |
*/ | |
public class ElasticInterface { | |
public static void main(String[] args) throws Exception { | |
Module module = new DefaultModule(); | |
Injector injector = Guice.createInjector(module); | |
ElasticInterface tws = injector.getInstance(ElasticInterface.class); | |
try { | |
System.out.println("creating index ..."); | |
tws.createIndex("chatindex"); | |
} catch (Exception ex) { | |
System.out.println("error:" + ex.getMessage()); | |
} | |
System.out.println("posting settings ..."); | |
tws.createDefaultSettings(); | |
System.out.println("posting mapping ..."); | |
tws.createDefaultMapping(); | |
System.out.println("posting some objects ..."); | |
tws.putObject(toJson("{'x':'pest bla'}"), "1"); | |
tws.putObject(toJson("{'x_t':'pest bla'}"), "2"); | |
System.out.println("querying ..."); | |
System.out.println(tws.query(toJson("{'q':'x_t:pest'}"))); | |
System.out.println("finished"); | |
} | |
private Logger logger = LoggerFactory.getLogger(getClass()); | |
@Inject | |
private HttpClient client; | |
@Inject | |
private Configuration config; | |
private String index = "chatindex"; | |
private String type = "chat"; | |
// TODO enhance this! and use rolling index ala issue 1500 ! | |
private String feedIndex = index; | |
private String queryIndex = index; | |
public ElasticInterface setConfiguration(Configuration config) { | |
this.config = config; | |
return this; | |
} | |
public String getUrl() { | |
return config.getElasticUrl() + ":" + config.getElasticPort(); | |
} | |
public ElasticInterface setHttpClient(HttpClient client) { | |
this.client = client; | |
return this; | |
} | |
public String putObject(JSONObject json, String id) throws IOException { | |
return putJson(getUrl() + "/" + feedIndex + "/" + type + "/" + id, json.toString()); | |
} | |
/** this is a rather limited query method - just the query_string query taken from the parameter q */ | |
public String query(JSONObject params) throws IOException { | |
try { | |
JSONObject request = new JSONObject(); | |
JSONObject query = new JSONObject(); | |
request.put("query", query); | |
JSONObject query_string = new JSONObject(); | |
query.put("query_string", query_string); | |
query_string.put("query", params.get("q")); | |
return postJson(getUrl() + "/" + queryIndex + "/" + type + "/_search", request.toString()); | |
} catch (JSONException ex) { | |
throw new RuntimeException("problem while creating json", ex); | |
} | |
} | |
// http://www.elasticsearch.org/guide/reference/api/admin-indices-put-mapping.html | |
// you need to provide the chat.json in the classpath | |
// chat.json enables automagical chosen analyzers: | |
// fields ending on _dt will get dates, fields ending on _t will get text (suitable for searching), | |
// where as all others will get indexed as keywords suitable for facetting | |
public String createDefaultMapping() throws IOException { | |
String content = readString(getClass().getResourceAsStream("chat.json"), "UTF-8"); | |
return putJson(getUrl() + "/" + queryIndex + "/" + type + "/_mapping", content); | |
} | |
// http://www.elasticsearch.org/guide/reference/api/admin-indices-update-settings.html | |
// you need to provide a settings.json in the classpath | |
public String createDefaultSettings() throws IOException { | |
return putJson(getUrl() + "/_settings", | |
readString(getClass().getResourceAsStream("settings.json"), "UTF-8")); | |
} | |
public String createIndex(String index) throws IOException { | |
return putJson(getUrl() + "/" + index + "/", "index :\n" | |
+ " number_of_shards : 3\n" | |
+ " number_of_replicas : 1\n"); | |
} | |
public String putJson(String url, String content) { | |
return requestJson(new PutMethod(url), content); | |
} | |
public String postJson(String url, String content) { | |
return requestJson(new PostMethod(url), content); | |
} | |
public String requestJson(EntityEnclosingMethod method, String content) { | |
try { | |
StringRequestEntity se = new StringRequestEntity(content, "application/json", "UTF-8"); | |
method.setRequestEntity(se); | |
int ret = client.executeMethod(method); | |
if (ret / 200 == 1) | |
return method.getResponseBodyAsString(); | |
throw new RuntimeException("Problem " + ret + " while " + method.getName() | |
+ " " + method.getResponseBodyAsString()); | |
} catch (Exception ex) { | |
throw new RuntimeException("Problem while " + method.getName() + ", url:" + getUrl(), ex); | |
} finally { | |
method.releaseConnection(); | |
} | |
} | |
/** helper method to convert url parameters into json. E.g. taken from HttpServletRequest.getParameterMap */ | |
public static JSONObject createJsonFromParameterMap(Map<String, String[]> map) { | |
JSONObject json = new JSONObject(); | |
try { | |
for (Entry<String, String[]> e : map.entrySet()) { | |
if (e.getValue().length == 0 || "method".equals(e.getKey()) || "id".equals(e.getKey())) | |
continue; | |
String key = e.getKey(); | |
if (e.getValue().length == 1) | |
json.put(key, e.getValue()[0]); | |
else | |
json.put(key, Arrays.asList(e.getValue())); | |
} | |
return json; | |
} catch (Exception ex) { | |
throw new RuntimeException("Cannot create json from parameter map:" + map, ex); | |
} | |
} | |
public static BufferedInputStream toBufferedIStream(InputStream is) { | |
if (is instanceof BufferedInputStream) | |
return (BufferedInputStream) is; | |
else | |
return new BufferedInputStream(is); | |
} | |
public static String readString(InputStream inputStream, String encoding) throws IOException { | |
InputStream in = toBufferedIStream(inputStream); | |
try { | |
byte[] buffer = new byte[4096]; | |
ByteArrayOutputStream output = new ByteArrayOutputStream(); | |
int numRead; | |
while ((numRead = in.read(buffer)) != -1) { | |
output.write(buffer, 0, numRead); | |
} | |
return output.toString(encoding); | |
} finally { | |
in.close(); | |
} | |
} | |
/** catch those ugly exceptions ... */ | |
private static JSONObject toJson(String str) { | |
try { | |
return new JSONObject(str); | |
} catch (Exception ex) { | |
throw new RuntimeException("cannot create json from " + str, ex); | |
} | |
} | |
} |
This file contains hidden or 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
<!-- add this snippet to your pom.xml --> | |
<dependency> | |
<groupId>commons-httpclient</groupId> | |
<artifactId>commons-httpclient</artifactId> | |
<version>3.1</version> | |
</dependency> |
This file contains hidden or 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
{ | |
"settings": { | |
"analysis" :{ | |
"analyzer": { | |
"index_analyzer": { | |
"tokenizer" : "whitespace", | |
"filter":["lowercase", "snowball"] | |
}, | |
"search_analyzer" : { | |
"tokenizer" : "whitespace", | |
"filter":["lowercase", "snowball"] | |
} | |
}, | |
"filter" : { | |
"snowball": { | |
"type" : "snowball", | |
"language" : "English" | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment