elasticsearch sql example java

隐身守侯 提交于 2020-07-23 18:37:36
package org.vander.es;

import org.apache.http.HttpHost;
import org.apache.http.util.EntityUtils;
import org.elasticsearch.client.Request;
import org.elasticsearch.client.Response;
import org.elasticsearch.client.RestClient;

public class TestClient {

	public static void main(String[] args) throws Exception {
		testTranslate();
	}

	/**
	 *     代码中的sql检索
	 * @throws Exception
	 */
	public static void test() throws Exception {
		RestClient restClient = RestClient.builder(new HttpHost("localhost", 9200, "http")).build();
		Request request = new Request("POST", "/_sql");
		request.setJsonEntity("{\"query\":\"SELECT * FROM library WHERE release_date < '2000-01-01'\"}");
		Response response = restClient.performRequest(request);
		String responseBody = EntityUtils.toString(response.getEntity());
		System.out.println(responseBody);
		restClient.close();
	}

	public static void test2() throws Exception {
		RestClient restClient = RestClient.builder(new HttpHost("localhost", 9200, "http")).build();
		Request request = new Request("POST", "/_sql");
		request.setJsonEntity("{\"query\":\"SELECT * FROM library WHERE release_date < '2000-01-01'\"}");
		Response response = restClient.performRequest(request);
		String responseBody = EntityUtils.toString(response.getEntity());
		System.out.println(responseBody);
		restClient.close();
	}

	/**
	 * sql转换为dsl
	 * @throws Exception
	 */
	public static void testTranslate() throws Exception {
		RestClient restClient = RestClient.builder(new HttpHost("localhost", 9200, "http")).build();
		Request request = new Request("POST", "/_sql/translate");
		request.setJsonEntity("{\"query\":\"SELECT * FROM library WHERE release_date < '2000-01-01'\"}");
		Response response = restClient.performRequest(request);
		String responseBody = EntityUtils.toString(response.getEntity());
		System.out.println(responseBody);
		restClient.close();

	}
}

https://www.elastic.co/guide/en/elasticsearch/reference/current/sql-functions.html

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!