前面两篇已经介绍了同步数据到Elasticsearch,接下来要进行查询数据了,查询数据毫秒级的体验,这才是我们要的结果,Elasticsearch的查询和Sql有些区别,有专门的逻辑,其实根据自己的实际情况就可以查出数据。
由于Elasticsearch已经支持RESTful的访问方式,如果使用JS访问会涉及到跨域的问题,在Domino平台,可以使用java代理访问Elasticsearch,获取数据后再返回前端。这就可以避免前端访问跨域问题。
查询时间说明 :took : Elasticsearch执行搜索的时间(以毫秒为单位)
以下查询的几张简要图说明:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Hashtable;
import java.util.StringTokenizer;
import lotus.domino.*;
public class JavaAgent extends AgentBase {
public static Hashtable parseQueryString(String queryString) {
StringTokenizer tokens = new StringTokenizer(queryString, "&");
Hashtable params = new Hashtable();
while (tokens.hasMoreTokens()) {
String token = tokens.nextToken(); int equalIdx = token.indexOf('=');
if (equalIdx != -1 && !token. equalsIgnoreCase("OpenAgent")) {
String name = token. substring(0, equalIdx);
String value = token. substring(equalIdx + 1);
params.put(name, value);
}
}
return params;
}
public void NotesMain() {
try {
Session session = getSession();
AgentContext agentContext = session.getAgentContext();
// Query_String_Decoded
Document doc = agentContext.getDocumentContext();
String url = doc.getItemValueString("Request_content");
Hashtable ht = parseQueryString(url);
String search = (String) ht.get("search");
search=java.net.URLDecoder.decode(java.net.URLDecoder.decode(search,"utf-8"),"utf-8");
System.out.println("search-->"+search);
java.io.PrintWriter pw = this.getAgentOutput();
pw.println("Content-type: application/json;charset=UTF-8");
String result=HttpSendSoapPost("POST","http://localhost:9200/xpages/_search/",search);
pw.println(result);
pw.close();
} catch(Exception e) {
e.printStackTrace();
}
}
public static String HttpSendSoapPost(String Method,String strurl,String xml){
HttpURLConnection connection = null;
InputStream is = null;
BufferedReader br = null;
String result = null;// 返回结果字符串
OutputStream out = null;
//Date d1 = new Date();
try {
// 创建远程url连接对象
URL url = new URL(strurl);
// 通过远程url连接对象打开一个连接,强转成httpURLConnection类
connection = (HttpURLConnection) url.openConnection();
// 设置连接方式:GET,POST
if(Method.equals("")){
connection.setRequestMethod("POST");
}else{
connection.setRequestMethod(Method);
}
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setRequestProperty("Content-Type", "application/json");
// 设置连接主机服务器的超时时间:15000毫秒
connection.setConnectTimeout(15000);
// 设置读取远程返回的数据时间:60000毫秒
connection.setReadTimeout(60000);
// 发送请求
connection.connect();
out = connection.getOutputStream(); // 获取输出流对象
connection.getOutputStream().write(xml.getBytes("UTF-8")); // 将要提交服务器的SOAP请求字符流写入输出流
out.flush();
out.close();
//System.out.println(connection.getResponseCode());
// 通过connection连接,获取输入流
if (connection.getResponseCode() == 200 || connection.getResponseCode() == 201) {
is = connection.getInputStream();
// 封装输入流is,并指定字符
br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
// 存放数据
StringBuffer sbf = new StringBuffer();
String temp = null;
while ((temp = br.readLine()) != null) {
sbf.append(temp);
sbf.append("\r\n");
}
result = sbf.toString();
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
// 关闭资源
if (null != br) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (null != is) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
connection.disconnect();// 关闭远程连接
}
//System.out.println();
return result;
}
}