The
Elasticsearch (name is too long, hereafter referred to as ES) as a search engine, is now at its zenith, almost and Solr Qi Drive. There is no longer a description of what he can do with the cloud. However, the official ES of the documentation, especially on Java client documentation, is really little pathetic, even a complete deletion of the example of the change is not. Here, I will shortcoming.
Before beginning to explain, or to do a foreshadowing, in order to have a model can be indexed, we have customized a model, a temporary name called Logmodel Bar, the model has a variety of data types, int,long,string,list, But do not think that this is a log-related model. As one of the simplest models of an index. The code is as follows: Java code import java.util.arraylist; import java.util.list; A model of import java.util.random; import java.util.uuid; /** * fabricated, It has nothing to do with the log * @author donlian */ Public class logmodel { //main id private long id; //Times id private int subId; /** * system name */ private String systemName; private String host; //Log Description private String desc; private List<Integer> catIds; public logmodel () { Random random = New random (); this.id = math.abs ( Random.nextlong ()); int subId = Math.Abs (Random.nextint ()); this.subId = subid; List<Integer> list = New arraylist<integer> (5); for (int i=0;i<5;i++) { list.add ( Math.Abs (random.neXtint ()); } this.catIds = list; this.systemname = subid%1 == 0? " OA ":" CMS "; this.host = subid%1 == 0? " 10.0.0.1 ":" 10.2.0.1 "; this.desc = " Chinese " + uuid.randomuuid (). toString (); } public logmodel (Long id,int subid,string sysname,string host,string desc, List<integer> catids) { this.id = id; this.subId = subId; this.systemname = sysname; this.host = host ; this.desc = desc; this.catIds = catIds; } /Save Get,set Method }
Also, because ES are generally used in JSON format when indexing, using Jackson to define a tool class that converts objects to JSON is also very simple, code: Java code public class ESUtils { private static ObjectMapper Objectmapper = new objectmapper (); public static String tojson (Object o) { try { return Objectmapper.writevalueasstring (o); } catch (jsonprocessingexception e) { e.printstacktrace (); } return ""; } }
Before we start to operate ES servers, we have to get an ES API, a brief introduction to the two ways of the ES operation server, one that uses node mode, where the machine also starts an ES and communicates with the server's ES, which can even store (strangely, Do you usually need this kind of way? And the other, which I've described below, interacts with the server through an object using the HTTP protocol.
The code for obtaining an ES client API is as follows: Java code settings settings = immutablesettings.settingsbuilder () // Specify Cluster name .put ("Cluster.name", "Elasticsearch") Machine status in //detection cluster .put ("Client.transport.sniff", true). Build (); /* * Create client, all operations are started by the client, this is like JDBC Connection object * remember to close */ &nBsp; client client = new transportclient (Settings)