<!--ELK--><dependency> <groupId>org.elasticsearch.client</groupId> <artifactId> Transport</artifactid> <version>5.1.1</version> <exclusions> <!--<exclusion> <artifactId>transport-netty4-client</artifactId> <groupid>org.elasticsearch.plugin</grou pid> </exclusion>--> </exclusions></dependency><dependency> <groupid>org.apac He.logging.log4j</groupid> <artifactId>log4j-core</artifactId> <version>2.6.2</ version></dependency><!--springmvc JSON--><dependency> <groupId> Com.fasterxml.jackson.core</groupid> <artifactId>jackson-databind</artifactId> <version> 2.8.8</version></dependency><dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-mapper-asl</artifactId> <version>1.9.13</version></dependency><dependency> <groupId>com.fasterxml.jackson.core</groupId> < Artifactid>jackson-core</artifactid> <version>2.8.8</version></dependency>< Dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactid>jackson-annotations </artifactId> <version>2.8.5</version></dependency>
Package com.sxis.util;
Import Java.io.ioexception;import java.net.inetaddress;import java.net.unknownhostexception;import java.util.Date; Import Java.util.hashmap;import Java.util.map;import Java.util.concurrent.executionexception;import Org.elasticsearch.elasticsearchexception;import Org.elasticsearch.action.admin.cluster.state.clusterstateresponse;import Org.elasticsearch.action.admin.indices.delete.deleteindexresponse;import Org.elasticsearch.action.delete.deleteresponse;import Org.elasticsearch.action.get.getresponse;import Org.elasticsearch.action.index.indexresponse;import Org.elasticsearch.action.search.searchresponse;import Org.elasticsearch.action.search.searchtype;import Org.elasticsearch.client.transport.transportclient;import Org.elasticsearch.common.settings.settings;import org.elasticsearch.common.transport.InetSocketTransportAddress ; Import Org.elasticsearch.common.xcontent.xcontentbuilder;import Org.elasticsearch.common.xcontent.xcontentfactory;import Org.elasticsearch.index.query.QueryBuilder;Import Org.elasticsearch.index.query.querybuilders;import Org.elasticsearch.search.searchhit;import Org.elasticsearch.search.searchhits;import Org.elasticsearch.search.sort.sortorder;import Org.elasticsearch.transport.client.PreBuiltTransportClient;/*** "description": ELK Java API interface, including query, delete, insert wait* "Steps": * @param* @return* @throws* @authorAllen * @date2017/7/4 9:47 */public class Elasticsearchutil {private static transportclientClient; private static StringElasticip= "192.168.211.50"; private static intElasticport= 9300;/*** Description: Initialize the Elasticsearch object* "Steps": * @param* @return* @throws* @authorAllen * @date2017/7/4 15:19 */public static void Init () throws Unknownhostexception, Interruptedexception, executionexception {//sets the name of the ES instance. Put ("cl Ient.transport.sniff ", true)//automatically sniff the status of the entire cluster and add the IP of the other ES nodes in the cluster to the local client list Settings essettings = Settings.builder (). put (" C Luster.name "," Elasticsearch "). Build ();Client= new Prebuilttransportclient (essettings);//initialization of the client older version has changed, this method has several overloaded methods, initialization plug-ins and so on. This step adds IP, at least one, in fact one is enough because the automatic sniffing configuration is addedClient. addtransportaddress (New Inetsockettransportaddress (Inetaddress.getbyname (Elasticip),Elasticport)); System. out. println ("Connection established successfully"); }/*** "description": Create index to convert the document into JSON format store* "Steps": * @param <Node>: node IP<port>: Node port number, default 9200<Index>: Index name<Type>: Index Type<ID>: The ID number of the Action object * @return* @throws* @authorAllen * @date2017/7/5 9:42 */public static void CreateIndex () throws elasticsearchexception,ioexception {for (int i=300; i<=50000000;i++) { User user = new user (); User.setid (1); User.setname ("Huang Fox" + i); User.setage (i% 100); Indexresponse indexresponse = null; Indexresponse =Client. Prepareindex ("Users", "User", i+ ""). SetSource (Generatejson (user)). Execute (). Actionget (); System. out. println ("responseiscreated:" +indexresponse); } System. out. println ("It is OK! "); } public static void query () throws Exception {//term query//querybuilder QueryBuilder = querybuilders.termquery ("Age", 50); Age is equal to//range query QueryBuilder Rangequerybuilder = Querybuilders.rangequery ("ages"). GT (50); Older than searchresponse SearchResponse =Client. Preparesearch ("Users"). Settypes ("User"). Setquery (Rangequerybuilder)//query. Setpo Stfilter (Querybuilders.rangequery ("age"). from (+) to (+))//Filter. AddSort ("Age", SortOrder.DESC). SetSize (120)//If not set, the default is to fetch 10 data. Execute (). Actionget (); Searchhits hits = Searchresponse.gethits (); System. out. println ("Number of records found:" +hits.gettotalhits ()); searchhit[] searchhists = Hits.gethits (); if (searchhists.length>0) {for (Searchhit hit:searchhists) {String name = (string) hit.getsource (). GE T ("name"); Integer age = Integer.parseint (Hit.getsource (). Get ("Age"). ToString ()); System. out. Format ("name:%s, age:%d \ n", name, age); } } }/*** Convert to JSON object * * @paramUser* @return*/private static string Generatejson (user user) {string json = ""; try {xcontentbuilder contentbuilder = Xcontentfactory.jsonbuilder (). StartObject (); Contentbuilder.field ("id", User.getid ()); Contentbuilder.field ("Name", User.getname ()); Contentbuilder.field ("Age", User.getage ()); JSON = Contentbuilder.endobject (). String (); } catch (IOException e) {e.printstacktrace (); } return JSON; }/* Get index Gets the document equivalent to reading a row of data from the database */public static void GetIndex () {GetResponse GetResponse =Client. Prepareget ("Users", "User", "402"). Execute (). Actionget (); System. out. println (Getresponse.getsourceasstring ()); }/* *delete index is equivalent to deleting a row of data */public static void Delete () {Deleteresponse deleteresponse =Client. Preparedelete ("Users", "User", "1"). Execute (). Actionget (); System. out. println (Deleteresponse.getversion ()); }/* *delete index deletes indexes and records governed by this index */public static void Deleteindex () {//delete all records Deleteindexresponse de Leteindexresponse =Client. admin (). Indices (). Preparedelete ("Logs"). Execute (). Actionget (); System. out. println (Deleteindexresponse.isacknowledged ()); }/*** "description": Get to all indexes* "Steps": * @param* @return* @throws* @authorAllen * @date2017/7/4 16:27 */public static void Getallindex () {Clusterstateresponse response =Client. admin (). cluster (). Preparestate (). Execute (). Actionget (); Get all Indexes string[] indexs=response.getstate (). GetMetaData (). Getconcreteallindices (); for (String index:indexs) {System. out. println (index + "delete"),//}} public void Close () {//on shutdown disconnects the clusterClient. Close (); } public static void Main (string[] args) {try {init (); CreateIndex (); GetIndex (); Delete (); Deleteindex (); Getallindex (); Query (); } catch (Exception e) {e.printstacktrace (); }}}class user{private static final longSerialversionuid= 5290251719938640641L; Private Integer ID; private String name; private int age; Public Integer GetId () {return id; } public void SetId (Integer id) {this.id = ID; } public String GetName () {return name; } public void SetName (String name) {this.name = name; } public int Getage () {return age; public void Setage (int.) {this.age = age; }}
Java elasticsearch Additions and deletions to change the search