码迷,mamicode.com
首页 > 编程语言 > 详细

elasticsearch java API 实现搜索样例

时间:2016-05-11 01:27:15      阅读:1182      评论:0      收藏:0      [点我收藏+]

标签:

查看cluster、version:curl ‘centos1:9200‘
插入:curl -XPUT ‘http://localhost:9200/dept/employee/1‘ -d ‘{ "empname": "emp1"}‘
查看index:curl ‘centos1:9200/_cat/indices?v‘
查看1条内容:curl ‘centos1:9200/dept/employee/1?pretty‘
查看所有内容:curl ‘centos1:9200/dept/employee/_search‘
简易搜索:curl ‘centos1:9200/dept/employee/_search?empname=emp1‘
复杂搜索:curl ‘centos1:9200/dept/employee/_search?pretty‘ -d ‘{"query" : {"match" : {"empname" : "emp2"}}}‘
删除:curl -XDELETE ‘http://172.16.1.16:9200/logstash-2016.02.*‘

3.1 ES原生API连接搜索(Java,非RestFul连接而是广播)
说明:Es的集群非常简单,只要在同一个局域网内,多台服务器能互相通讯,并且cluster.name是一样的,就能自动集在一起。

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Collection;

import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.index.query.FuzzyQueryBuilder;
import org.elasticsearch.index.query.MatchQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.QueryStringQueryBuilder;
import org.elasticsearch.index.query.RangeQueryBuilder;
import org.elasticsearch.index.query.TermQueryBuilder;
import org.elasticsearch.index.query.TermsQueryBuilder;
import org.elasticsearch.index.query.WildcardQueryBuilder;
import org.elasticsearch.node.Node;
import org.elasticsearch.node.NodeBuilder;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;

/* Implement ElasticSearch internal Java API (broadcast)
 * Refer
 * http://www.tuicool.com/articles/R7RVJb 
 * https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/java-search.html
 * http://stackoverflow.com/questions/23520684/elasticsearch-java-apinonodeavailableexception-no-node-available
 * Node Transport Difference - http://www.cnblogs.com/huangfox/p/3543134.html
 * */
public class EsQuery01 {
	
	public static void main(String[] args) throws Exception {
		
		// 1. Prepare Transport
		//Client client = buildClientByNode();
		Client client = buildClientByTransport();
		
		// 2. Prepare Query Param
		QueryBuilder queryBuilder = buildQuery();
		
		// 3. Get Response
		SearchResponse response = client.prepareSearch("dept")
		        .setTypes("employee")
		        .setQuery(queryBuilder)
		        .setFrom(0).setSize(2)
		        .execute()
		        .actionGet();
		//SearchResponse response = client.prepareSearch().execute().actionGet();
		
		if (response != null) {
			system.out.println(response);
			for (SearchHit hit : response.getHits().getHits()) {
				System.out.print(hit.getId() + "~");
				System.out.println(hit.getSourceAsString());
			}
		} else {
			System.out.println("response null~~~");
		}
		if (client != null) {
			client.close();
		}
	}
	
	/* slow */
	public static Client buildClientByTransport() throws UnknownHostException {
		final String CLUSTERNAME = "es-cluster";
		String str = "192.168.56.250";
        String[] ipStr = str.split("\\.");
        byte[] ipBuf = new byte[4];
        for(int i = 0; i < 4; i++){
            ipBuf[i] = (byte)(Integer.parseInt(ipStr[i])&0xff);
        }
		
		Settings settings = Settings.settingsBuilder()
		        .put("cluster.name", CLUSTERNAME).build();
		Client client = TransportClient.builder().settings(settings).build()
				.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("centos1"), 9300));
				//.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByAddress(ipBuf), 9300));
		return client;
	}
	
	/* very slow */
	public static Client buildClientByNode() throws UnknownHostException {
		/* Run Configurations > Arguments > VM arguments, add "-Des.path.home=/appl/elasticsearch-2.1.1" */
		final String CLUSTERNAME = "es-cluster";
		Node node = NodeBuilder.nodeBuilder().settings(Settings.settingsBuilder().put("http.enabled", false)).
				clusterName(CLUSTERNAME).client(true).node();
		Client client = node.client();
		return client;
	}
	
	public static QueryBuilder buildQuery() {
		//TermsQueryBuilder queryBuilder = queryByIds();
		//FuzzyQueryBuilder queryBuilder = QueryBuilders.fuzzyQuery("empname", "emp");
		MatchQueryBuilder queryBuilder = QueryBuilders.matchQuery("empname", "emp2");
		//WildcardQueryBuilder queryBuilder = QueryBuilders.wildcardQuery("empname", "*emp*");
		//QueryStringQueryBuilder queryBuilder = QueryBuilders.queryStringQuery("-26");			// full text
		
		SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
		String json = searchSourceBuilder.query(queryBuilder).toString();
		System.out.println(json);
		return queryBuilder;
	}
	
	/* Refer https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-id-field.html */
	public static TermsQueryBuilder queryByIds() {
		Collection coll = new ArrayList();
		coll.add(1);
		coll.add(2);
		TermsQueryBuilder queryBuilder = QueryBuilders.termsQuery("_id", coll);
		return queryBuilder;
	}

}


3.2 手工Restful连接搜索(Java)

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

import org.elasticsearch.index.query.MatchQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.builder.SearchSourceBuilder;

/* Implement RestFul manually (fast)
 * Refer
 * http://stackoverflow.com/questions/33889866/how-to-send-url-request-with-json-to-elasticsearch-using-java
 * http://blog.csdn.net/dm_vincent/article/details/41693125
 * */
public class EsQuery02 {

	public static void main(String[] args) throws Exception {
		HttpURLConnection conn = null;
		DataOutputStream wr = null;
		BufferedReader br = null;
		StringBuilder resultBuilder = new StringBuilder();
		String line = null;
		try {
			// 1. Prepare url
			String url01 = "http://centos1:9200/dept/employee/_search?pretty";
			
			// 2. Prepare query param
			//String queryParamJson = buildQueryParamByStr();
			String queryParamJson = buildQueryParamByAPI();
			
			// 3. Inject url
			URL url = new URL(url01);
			conn = (HttpURLConnection) url.openConnection();
			conn.setRequestMethod("POST");
			conn.setRequestProperty("Content-Type", "application/json");
			conn.setRequestProperty("Content-Length", 
					Integer.toString(queryParamJson.getBytes().length));
			conn.setRequestProperty("Content-Language", "en-US");
			conn.setUseCaches(false);
			conn.setDoOutput(true);
			
			// 4. Inject query param
			wr = new DataOutputStream (
					conn.getOutputStream());
			wr.writeBytes(queryParamJson);
			
			// Connection failure handling
			if (conn.getResponseCode() != 200) {
	            throw new RuntimeException("Failed : HTTP error code : "
	                    + conn.getResponseCode());
	        }
			
			// 5. Get Response
			br = new BufferedReader(new InputStreamReader(
	                (conn.getInputStream())));
			while ((line = br.readLine()) != null) {
				resultBuilder.append(line);
				resultBuilder.append(‘\r‘);
	        }
			System.out.println("result~~~" + resultBuilder.toString());
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if(wr != null) {
				wr.close();
			}
			if(br != null) {
				br.close();
			}
			if(conn != null) {
				conn.disconnect();
			}
		}
	}
	
	public static String buildQueryParamByAPI() {
		MatchQueryBuilder queryBuilder = QueryBuilders.matchQuery("empname", "emp2");
		SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
		String queryParamJson = searchSourceBuilder.query(queryBuilder).toString();
		System.out.println("json~~~" + queryParamJson);
		return queryParamJson;
	}
	public static String buildQueryParamByStr() {
		/*curl ‘centos1:9200/dept/employee/_search?pretty‘ -d 
		 * ‘{"query" : {"match" : {"empname" : "emp2"}}}‘*/
		String queryParamJson = "{\"query\" : {\"match\" : {\"empname\" : \"emp2\"}}}";
		return queryParamJson;
	}
}


3.3 Jest Restful连接搜索(Java)

import org.elasticsearch.index.query.MatchQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.builder.SearchSourceBuilder;

import io.searchbox.client.JestClientFactory;
import io.searchbox.client.config.HttpClientConfig;
import io.searchbox.client.http.JestHttpClient;

/* Implement RestFul by Jest
 * Refer:
 * http://www.searchly.com/documentation/developer-api-guide/java-jest/
 * http://www.cnblogs.com/huangfox/p/3542858.html
 * http://www.169it.com/article/13172689366007246587.html
 * https://github.com/searchbox-io/Jest/tree/master/jest
 * download: https://oss.sonatype.org/content/groups/public/io/searchbox/jest/2.0.2/
 * mvn: http://www.searchly.com/documentation/developer-api-guide/java-jest/
 * */
public class EsQuery03 {
	/*
	String connUrl = "http://centos1:9200";
	
	public static void main(String[] args) {
		// 1. connect
		JestClient client = getClient();
		// 2. prepare query param
		String queryParamJson = buildQueryParamByAPI();
		// 3. search
		SearchResult result = search(client, queryParamJson);
		// 4. get response
		getContent();
		
	}
	
	public static JestClient getClient() {
		JestClientFactory factory = new JestClientFactory();
		factory.setHttpClientConfig(new HttpClientConfig
		       .Builder(connUrl)
		       .multiThreaded(true)
		       .build());
		JestClient client = factory.getObject();
		return client;
	}
	
	public static String buildQueryParamByAPI(String queryParamJson) {
		MatchQueryBuilder queryBuilder = QueryBuilders.matchQuery("empname", "emp2");
		SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
		searchSourceBuilder.query(queryBuilder);
		return searchSourceBuilder.toString();
	}
	
	public static String buildQueryParamByStr() {
		String queryParamJson = "{\"query\" : {\"match\" : {\"empname\" : \"emp2\"}}}";
		return queryParamJson;
	}
	
	public static SearchResult search(JestClient client, String queryParamJson) {
		Search search = (Search) new Search.Builder(queryParamJson)
        // multiple index or types can be added.
        .addIndex("dept")
        .addType("employee")
        .build();

		//JestResult result = client.execute(search);
		SearchResult result = client.execute(search);
		return result;
	}
	
	public static void getContent() {
		//List<Article> articles = result.getSourceAsObjectList(Article.class);
		//List<SearchResult.Hit<Article, Void>> hits = searchResult.getHits(Article.class);
	}
	*/
}

elasticsearch java API 实现搜索样例

标签:

原文地址:http://www.cnblogs.com/bmaker/p/5479971.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!