before you must install elastic search server, can learn at : http://na5cent.blogspot.com/2013/08/install-elastic-search.html
maven project
add dependencies (pom.xml)
note : dependencies version must same elastic search server version
<dependencies> <!-- elastic search @ http://www.elasticsearch.org/guide/reference/java-api/ --> <dependency> <groupId>org.elasticsearch</groupId> <artifactId>elasticsearch</artifactId> <version>0.90.3</version> </dependency> <!-- elastic search ******************************************************** --> ... ... ... </dependencies>ExlasticIndexing.java
package com.blogspot.na5cent.elasticsearch; import java.util.Date; import java.util.HashMap; import java.util.Map; import org.elasticsearch.action.index.IndexResponse; import org.elasticsearch.client.Client; import org.elasticsearch.client.transport.TransportClient; import org.elasticsearch.common.transport.InetSocketTransportAddress; import org.elasticsearch.common.transport.TransportAddress; /** * * @author redcorw */ public class ElasticIndexing { //point to elastic search server private static TransportAddress transportAddress = new InetSocketTransportAddress("192.168.1.34", 9300); public static void main(String[] args) { Client client = null; try { //connect to server client = new TransportClient().addTransportAddress(transportAddress); //make document data Map map = new HashMap(); map.put("user", "redcrow"); map.put("post_date", new Date()); map.put("message", "founder na5cent.blogspot.com"); /* put into index and get response - first parameter : index - second parameter : type - third parameter : id (this example not use) use for fixed id but this example generate by elastic search server */ IndexResponse response = client.prepareIndex("blogspot", "na5cent") .setSource(map) .execute() .actionGet(); //show response detail System.out.println("id : " + response.getId()); System.out.println("index : " + response.getIndex()); System.out.println("type : " + response.getType()); System.out.println("version : " + response.getVersion()); } finally { //close connection if (client != null) { client.close(); } } } }result from running ElasticIndexing.java
at the server (192.168.1.34)
check new index
... $ curl -XGET 'http://localhost:9200/blogspot/na5cent/_search?q=user:readcrow' ...
learning more : http://www.elasticsearch.org/guide/reference/java-api/index_/
have fun to learning ^____^
ไม่มีความคิดเห็น:
แสดงความคิดเห็น