As we all know, elasticsearch is referred to as es, which is based on a search server based on Lucene. It provides a distributed multi-user capability full-text search engine based on the RESTful web interface. Elasticsearch is developed in Java and released as an open source under the terms of the Apache license. It is currently a popular enterprise-level search engine. Designed in cloud computing, it can achieve real-time search, stability, reliability, fast, and easy to install and use.
We build a website or application and add search functionality, but it is very difficult to create a search job. We want the search solution to run fast, we want to have a zero configuration and a completely free search mode, we want to be able to simply use JSON to index data over HTTP, we want our search server to always be available, we want to be able to start from one and scale to hundreds, we want to search in real time, we want to be simple multi-tenant, we want to build a cloud solution. So we use Elasticsearch to solve all these problems and more other problems that may arise.
When using es in Java, I just want to solve the problem that the query speed is not fast enough and the efficiency is not high enough. The single query of data from the database can no longer meet the current business needs. OK! So now let’s talk about how to use the magical search server of es in Java. First of all, you have to reference the es dependency package, and the dependencies are as follows:
<dependency><groupId>org.elasticsearch.client</groupId><artifactId>transport</artifactId><version>5.5.0</version> </dependency><groupId>org.elasticsearch.client</groupId><artifactId>transport</artifactId><version>5.5.0</version> </dependency> <dependency><groupId>org.elasticsearch</groupId><artifactId>elasticsearch</artifactId><version>5.5.0</version> </dependency>
OK, after adding the dependencies, we enter the es usage stage. By the way, there is a problem that needs to be explained. To use es, you need to install the jdk1.8 toolkit and tomcat should be version 7.05 or above, otherwise es will not be supported, and an error may be reported when integrating into the project! , there are also installation and download of es, here is the download address: https://www.elastic.co/downloads/elasticsearch
After everything is ready, we will enter the moment we really look forward to. What, yes, how to search and query the things in the es server in Java, let me reveal them one by one for you:
First, we suggest an es tool class
package com.osa.utils;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.io.PrintWriter;import java.net.URL;import java.net.URLConnection;import java.net.URLEncoder;import java.util.Iterator;import java.util.List;import java.util.Map;import java.util.Set;import org.apache.http.client.ClientProtocolException;import com.alibaba.fastjson.JSONArray;import com.alibaba.fastjson.JSONException;import com.alibaba.fastjson.JSONObject;public class HTTPSentUtils {/** * Send a request to the specified URL* * @param url * URL to send the request * @param param * Request parameters, the request parameters should be in the form of name1=value1&name2=value2. * @return URL Response result of the remote resource represented by the remote resource*/ public static String sendGet(String url, String param) { String result = ""; BufferedReader in = null; try { String urlNameString = url + "?" + param; URL realUrl = new URL(urlNameString); // Open the connection between the URL URLConnection connection = realUrl.openConnection(); // Set the general request attribute connection.setRequestProperty("accept", "*/*"); connection.setRequestProperty("connection", "Keep-Alive"); connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"); // Create an actual connection connection.connect(); // Get all response header fields Map<String, List<String>> map = connection.getHeaderFields(); // traverse all response header fields for (String key : map.keySet()) { System.out.println(key + "--->" + map.get(key)); } // Define the BufferedReader input stream to read the URL's response in = new BufferedReader(new InputStreamReader(connection.getInputStream())); String line; while ((line = in.readLine()) != null) { result += line; } } catch (Exception e) { System.out.println("Exception occurred when sending a GET request!" + e); e.printStackTrace(); } // Use finally block to close the input stream finally { try { if (in != null) { in.close(); } } catch (Exception e2) { e2.printStackTrace(); } } return result; } /** * Send a request to the specified URL * @param url * URL to send the request * @param param * Request parameter, the request parameter should be in the form of name1=value1&name2=value2. * @return Response result of the remote resource represented by */ public static String sendPost(String url, String param) { PrintWriter out = null; BufferedReader in = null; String result = ""; try { URL realUrl = new URL(url); // Open the connection between URLConnection conn = realUrl.openConnection(); // Set the general request attribute conn.setRequestProperty("accept", "*/*"); conn.setRequestProperty("connection", "Keep-Alive"); conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)"); // To send a POST request, you must set the following two lines to conn.setDoOutput(true); conn.setDoInput(true); // Get the output stream corresponding to the URLConnection object out = new PrintWriter(conn.getOutputStream()); // Send the request parameter out.print(param); // Buffer out.flush() of the flush output stream; // Define the BufferedReader input stream to read the response of the URL in = new BufferedReader( new InputStreamReader(conn.getInputStream())); String line; while ((line = in.readLine()) != null) { result += line; } } catch (Exception e) { System.out.println("Exception occurred when sending a POST request!" +e); e.printStackTrace(); } //Use finally blocks to close the output stream and the input stream finally{ try{ if(out!=null){ out.close(); } if(in!=null){ in.close(); } } catch(IOException ex){ ex.printStackTrace(); } } return result; } } After the tool class is available, you can see that there are two methods for sending requests, one is the sendGet and sendPost methods. The two methods can be selected based on their own choices. Of course, this is just a method for sending requests. How to call them? Here, since we are querying from es, there is no need to use mybatis or hibernate frameworks. You can define SQL by yourself in the data layer, and then use SQL to call the sendGet/sendPost method through the previous tool class, as follows:
String sql=URLEncoder.encode(" select * from table");encode method mainly removes some spaces in sql
String result=HTTPSentUtils.sendGet("http://221.124.71.8:9200/_sql", "sql="+sql); ip plus port can be configured when installing
OK! Generally, if the sending is successful at this time, the data in es can be retrieved. Because the returned json data in es, we format the json string.
net.sf.json.JSONObject jsonObject =net.sf.json.JSONObject.fromObject(result); //Take out the hits tag net.sf.json.JSONObject hitsjsonObject = jsonObject.getJSONObject("hits"); After the data is obtained, it is concluded that it is business operations. It's almost over here.
The above are some operations of query, so what should we do if we need to batch insert data into es? Give an example
package com.sojson.core.elasticsearch.manager;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import net.sf.json.JSONObject;import org.elasticsearch.action.bulk.BulkRequestBuilder;import org.elasticsearch.action.bulk.BulkResponse;import org.elasticsearch.action.index.IndexRequestBuilder;import com.sojson.common.utils.StringUtils;import com.sojson.core.elasticsearch.utils.ESTools;publicclassInsertManager{/*** Add data to Elasticsearch*@param index Index*@param type Type*@param idName Id field name*@param json The stored JSON can accept Map*@return*/publicstaticMapsave(String index,String type,String idName,JSONObject json){List list =newArrayList();list.add(json);return save(index, type, idName, list);} The parameters passed are processed and called the save method to perform the insertion operation
/*** Add data to Elasticsearch*@param index Index*@param type Type*@param idName Id field name*@param listData A collection of objects*@return*/@SuppressWarnings("unchecked")publicstaticMapsave(String index,String type,String idName,List listData){BulkRequestBuilder bulkRequest =ESTools.client.prepareBulk().setRefresh(true);Map resultMap =newHashMap();for(Object object: listData){JSONObject json =JSONObject.fromObject(object);//No idName is specified, then let Elasticsearch automatically generate. If(StringUtils.isBlank(idName)){IndexRequestBuilder lrb =ElasticsearchUtils.client .prepareIndex(index, type) .setSource(json);bulkRequest.add(lrb);//ElasticsearchUtils is a tool class, which is configured with some es configuration information}else{String idValue = json.optString(idName);IndexRequestBuilder lrb =ESTools.client.prepareIndex(index, type,idValue).setSource(json);bulkRequest.add(lrb);}}BulkResponse bulkResponse = bulkRequest.execute().actionGet();if(bulkResponse.hasFailures()){// process failures by iterating through each bulk response itemSystem.out.println(bulkResponse.getItems().toString());resultMap.put("500","Save ES failed!");return resultMap;}bulkRequest=ESTools.client.prepareBulk();resultMap.put("200","Save ES successful!");return resultMap;}} ElasticsearchUtils tool class
public class ElasticsearchUtils {private static final String CLUSTER_NAME = "cluster.name";private static final String ES_IP="es.ip";private static final String ES_PORT="es.port";private static Settings settings;private static TransportClient client;public static TransportClient getESClient() throws UnknownHostException{settings = Settings.builder().put(CLUSTER_NAME,ConfigUtils.getConfig(CLUSTER_NAME)).build();if(client != null){client = new PreBuiltTransportClient(settings).addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(ConfigUtils.getConfig(ES_IP)), Integer.parseInt(ConfigUtils.getConfig(ES_PORT))));} return client;} The above is the insertion es operation. Okay, that’s all for today’s summary. I hope it can bring you some small help, and I hope you can support Wulin.com more.