Domino毫秒级查询利器Elasticsearch(三)

本文介绍如何使用Java代理访问Elasticsearch,实现毫秒级数据查询,避免前端跨域问题,详细展示了HTTP请求与响应过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

       前面两篇已经介绍了同步数据到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;
	}
    
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

weijia3624

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值