elasticsearch使用_java API

7427 ワード


elasticsearch java API
 
公式サイト:
http://www.elasticsearch.org/guide/en/elasticsearch/client/java-api/current/
 
1.elasticsearchを起動する:
 
sh start-elasticsearch-jiqun.sh
 
 1 #!/bin/bash
  2 
  3 ##
  4 #  start elasticsearch jiqun
  5 ##
  6 
  7 cd /Users/hyy044101331/programfile/elasticsearch/elasticsearch-node1/bin
  8 ./elasticsearch -d -Xms512m -Xmx512m
  9 
 10 cd /Users/hyy044101331/programfile/elasticsearch/elasticsearch-node2/bin
 11 ./elasticsearch -d -Xms512m -Xmx512m
 
2.maven依存を追加する:
<!-- *** elasticsearch *** -->
		<dependency>
			<groupId>org.elasticsearch</groupId>
			<artifactId>elasticsearch</artifactId>
			<version>1.4.1</version>
		</dependency>
		<dependency>
			<groupId>org.apache.lucene</groupId>
			<artifactId>lucene-core</artifactId>
			<version>4.10.2</version>
		</dependency>
		<!-- *************** -->
 
3. ElasticSearch Cient
package com.mengka.elasticsearch;

public class Constant {

	/**
	 *  elacticsearch    
	 * 
	 */
	public static final String CLUSTER_NAME = "mengka-elasticsearch";

	/**
	 *  node    
	 *
	 */
	public static final String ELASTICSEARCH_NODE_1_NAME = "mengka-mac-node1";
	public static final String ELASTICSEARCH_NODE_2_NAME = "mengka-mac-node2";
	
	/**
	 *  elacticsearch  IP  
	 */
	public static final String ELASTICSEARCH_IP = "127.0.0.1";


	/**
	 *  node1 node2   :
	 *     [9300-9400] is for node-to-node communication
	 */
	public static final int ELASTICSEARCH_NODE_1_PORT = 9300;
	public static final int ELASTICSEARCH_NODE_2_PORT = 9301;

	/**
	 * node1 node2 http  :
	 *      [9200-9300] is for HTTP traffic
	 */
	public static final int ELASTICSEARCH_NODE_1_HTTP_PORT = 9200;
	public static final int ELASTICSEARCH_NODE_2_HTTP_PORT = 9201;
}
 
package com.mengka.elasticsearch;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;

/**
 * client  
 * <br/>
 * 》》node client:
 * <ul>
 * <li>         elasticsearch  ;</li>
 * <li>         ,       ;</li>
 * </ul>
 * <p/>
 * 》》TransportClient:
 * <ul>
 * <li>     ;</li>
 * <li>   socket   elasticsearch  ;</li>
 * <li>netty   ;</li>
 * </ul>
 * <p/>
 * Created by mengka
 */
public class ElasticSearchClient {

    private static final Log log = LogFactory.getLog(ElasticSearchClient.class);

    private static TransportClient client;

    private ElasticSearchClient() {
        try {
            Settings settings = ImmutableSettings.settingsBuilder()
                    .put("cluster.name", Constant.CLUSTER_NAME).build();

            client = new TransportClient(settings);
            client.addTransportAddress(new
                    InetSocketTransportAddress(Constant.ELASTICSEARCH_IP, Constant.ELASTICSEARCH_NODE_1_PORT));
//            client.addTransportAddress(new
//                    InetSocketTransportAddress(Constant.ELASTICSEARCH_IP, Constant.ELASTICSEARCH_NODE_2_PORT));

        } catch (Exception e) {
            log.error("ElasticSearchClient client error!", e);
        }
    }

    public void close() {
        client.close();
    }

    public Client client() {
        return client;
    }

    public static ElasticSearchClient getElasticSearchClient() {
        return ElasticSearchClientHolder.elasticSearchClient_holder;
    }

    private static class ElasticSearchClientHolder {
        private static final ElasticSearchClient elasticSearchClient_holder = new ElasticSearchClient();
    }
}
 
 
4.データの挿入
import java.util.List;
import com.mengka.elasticsearch.ElasticSearchClient;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.common.xcontent.XContentBuilder;
import static org.elasticsearch.common.xcontent.XContentFactory.*;


/**
 * elasticsearch  (java API):<br>
 * http://www.elasticsearch.org/guide/en/elasticsearch/client/java-api/current/
 * <hr>
 *
 * @author mengka.hyy
 */
public class put_01 {

    private static final Log log = LogFactory.getLog(put_01.class);

    private static Client client = ElasticSearchClient.getElasticSearchClient().client();

    public static void main(String[] args) throws Exception {
        try {
            invoke();
        } finally {
            client.close();
        }

    }


    public static void invoke() throws Exception {
        /**
         *   :
         *     elasticsearch      
         */
        XContentBuilder content = jsonBuilder().startObject()
                .field("name", "hyy044101331").field("age", "22")
                .field("address", "xiamen").endObject();

        IndexResponse response = client.prepareIndex("mengka", "age", "22")
                .setSource(content).execute().actionGet();

        String _index = response.getIndex();
        String _type = response.getType();
        String _id = response.getId();
        long _version = response.getVersion();
        log.info("------------, elasticsearch _index = " + _index
                + " , _type = " + _type + " , _id = " + _id + " , _version = "
                + _version);


    }

}
 
 
5.照会データ
curl -XGET 'http://localhost:9200/_search?pretty=true' -d '{"query":{"match":{"address":"xiamen"}}}'
  elasticsearch使用_ java API_第1张图片