Java-based ES development

Source: Internet
Author: User
Tags bulk insert dateformat string format log4j

3.1 Environment Configuration

JDK 1.8 and above

Elasticsearch.client 5.5.2 (consistent with server version)

LOG4J 2.7 and below

MAVEN project The necessary jar package dependencies

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi= "http://www.w3.org/2001/ Xmlschema-instance "

xsi:schemalocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>

<groupId>com.rodge</groupId>

<artifactId>elasticFirstDemo</artifactId>

<version>0.0.1-SNAPSHOT</version>

<properties>

<elasticSearch.version>5.5.2</elasticSearch.version>

</properties>

<dependencies>

<!--Https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-core--

<dependency>

<groupId>org.apache.logging.log4j</groupId>

<artifactId>log4j-core</artifactId>

<version>2.7</version>

</dependency>

<!--Https://mvnrepository.com/artifact/org.elasticsearch/elasticsearch--

<dependency>

<groupId>org.elasticsearch.client</groupId>

<artifactId>transport</artifactId>

<version>${elasticSearch.version}</version>

</dependency>

</dependencies>

</project>

Log file Log4j2.properties file contents

Appender.console.type = Console

Appender.console.name = Console

Appender.console.layout.type = Patternlayout

Appender.console.layout.pattern = [%t]%-5p%c-%m%n

Rootlogger.level = info

RootLogger.appenderRef.console.ref = Console

3.2 Simple Query

Test cases, querying documents with ID 1 in novel in the book Index

Package Com.rodge.elasticSearch.firstDemo;

Import java.net.InetAddress;

Import java.net.UnknownHostException;

Import Org.elasticsearch.action.get.GetResponse;

Import org.elasticsearch.client.transport.TransportClient;

Import org.elasticsearch.common.settings.Settings;

Import org.elasticsearch.common.transport.InetSocketTransportAddress;

Import org.elasticsearch.transport.client.PreBuiltTransportClient;

Import Org.junit.Test;

public class Esfirstdemo {

@SuppressWarnings ("Unchecked")

Public Transportclient Client () throws Unknownhostexception {

ES address, the address is 192.168.253.129 (here the address cannot be added "/http"), where the port is ES TCP port is 9300,

Instead of the previous http9200 port, if ES has more than one node, you can create multiple nodes and then add in the client

Note that the port number here is not 9200, but 9300. The 9200 port is used to make HTTP REST

API to access Elasticsearch, while Port 9300 is the default port on which the transport layer listens.

inetsockettransportaddress node = new Inetsockettransportaddress (

Inetaddress.getbyname ("192.168.253.129"), 9300);

The Configuration class for ES

Settings Settings = Settings.builder (). Put ("Cluster.name", "Wali"). Build ();

Transportclient is the core class of ES launcher, and the subsequent development of ES is centered around the transportclient.

Transportclient client = new prebuilttransportclient (settings);

Client.addtransportaddress (node);

return client;

}

@Test

public void Get () throws Unknownhostexception {

GetResponse result = Client (). Prepareget ("book", "Novel", "1"). get ();

System.out.println (Result.getsource (). toString ());

}

}

3.3 Insert 3.3.1 Specify ID Insert

Add document, specify ID to insert

@Test

Public void Addwithid () throws IOException {

String title = "One Yang finger";

String author = "Wang Chongyang";

int wordCount = 5000;

Date publishdate = new date ();

SimpleDateFormat DateFormat = new simpledateformat ("Yyyy-mm-dd HH:mm:ss");

String format = Dateformat.format (publishdate);

Constructing es documents

Constructed using the es -brought JSON tool

Xcontentbuilder Xcontentbuilder = xcontentfactory. Jsonbuilder (). StartObject ()

. Field ("title", title). Field ("Author", author). field ("Word_count", WordCount)

. Field ("Publish_date", format). EndObject ();

Inserts the data into index as book, type is novel, and the specified ID is 15

Indexresponse indexresponse = Client (). Prepareindex ("book", "novel"). SetId ("15")

. SetSource (Xcontentbuilder). get ();

System. out. println (Indexresponse.getid ());

}

3.3.2 ES randomly generated ID inserted

Add a document with ID es randomly generated

@Test

Public void Add () throws IOException {

String title = "Luoying Sword";

String author = "Yellow Pharmacist";

int wordCount = 3000;

Date publishdate = new date ();

SimpleDateFormat DateFormat = new simpledateformat ("Yyyy-mm-dd HH:mm:ss");

String format = Dateformat.format (publishdate);

Constructing es documents

Constructed using the es -brought JSON tool

Xcontentbuilder Xcontentbuilder = xcontentfactory. Jsonbuilder (). StartObject ()

. Field ("title", title). Field ("Author", author). field ("Word_count", WordCount)

. Field ("Publish_date", format). EndObject ();

Inserts the data into index as book, type is in novel, ID has es randomly generated, and gets return information

Indexresponse indexresponse = Client (). Prepareindex ("book", "novel")

. SetSource (Xcontentbuilder). get ();

System. out. println (Indexresponse.tostring ());

}

3.3.4 BULK Insert

@Test

Public void dbimportes () {

Long count = 10000l;//per acquisition bar

String index = "Rodge";// es index name

String type = "user";//type name in es

Bulkrequestbuilder preparebulk = transportclient. Preparebulk ();

long startTime = System. Currenttimemillis ();

System. out. println ("StartTime:" + startTime);

for (int i = 1; I <= count; i++) {

map<string, object> ret = new hashmap<string, object> ();

Ret.put ("Recordtime", System. Currenttimemillis());

Ret.put ("area", "Beijing" + i);

Ret.put ("usertype", + i);

Ret.put ("Count", I);

Preparebulk.add (transportclient. Prepareindex (index, type). SetSource (ret));

Every 10,000 articles submitted once

if (i% 1000 = = 0) {

Bulkresponse actionget = Preparebulk.execute (). Actionget ();

System. out. println (Actionget.tostring ());

long endTime = System. Currenttimemillis ();

System. out. println ("EndTime:" + endTime);

System. out. println ("Time Consumed:" + (Endtime-starttime)/1000);

}

}

}

3.4 Delete 3.4.1 Delete the document with the specified ID

Delete--delete the document with the specified ID

@Test

Public void Deletebyid () throws unknownhostexception {

Deleteresponse deleteresponse = Client ()

. Preparedelete ("book", "novel", "Av8abznyhgrpjnhorylk"). get ();

System. out. println (Deleteresponse.getversion ());

}

3.4.2 Deleting an index

Delete index is irreversible, use with caution!

Delete--delete index

@Test

Public void Deleteindex () throws unknownhostexception {

Deleteindexresponse deleteindexresponse = Client (). Admin (). Indices (). Preparedelete ("People")

. get ();

System. out. println (Deleteindexresponse.tostring ());

}

3.5 Modify 3.5.1 Update the specified document by ID

Update--update documents based on ID

@Test

Public void Updatebyid () throws IOException, Interruptedexception, executionexception {

Updaterequest updaterequest = new updaterequest ("book", "Novel", "1");

Xcontentbuilder Contentbuilder = xcontentfactory. Jsonbuilder (). StartObject ();

Contentbuilder.field ("Author", "Zhang San _update");

Contentbuilder.field ("title", "Moving Soul Dafa _update");

Contentbuilder.field ("Word_count", 200);

Contentbuilder.field ("Publish_date", "2017-10-16");

Contentbuilder.endobject ();

Updaterequest.doc (Contentbuilder);

Updateresponse updateresponse = Client (). Update (Updaterequest). get ();

System. out. println (Updateresponse.tostring ());

}

3.6 Complex Query Interface development

Compound query

@Test

Public void complexquery () throws unknownhostexception {

Match query

Boolquerybuilder Boolquerybuilder = querybuilders. Boolquery ();

Boolquerybuilder.must (querybuilders. Matchquery("Author", "Watt Force"));

Boolquerybuilder.must (querybuilders. Matchquery("title", "Elasticsearch Proficiency"));

Boolquerybuilder.must (Querybuilders.matchquery ("Publish_date", ""));

Scope Query

Rangequerybuilder Rangequerybuilder = querybuilders. Rangequery ("Word_count");

Rangequerybuilder.from (200);

Rangequerybuilder.to (3000);

Boolquerybuilder.filter (Rangequerybuilder);

Searchrequestbuilder requestbuilder = Client ().

Preparesearch ("book"). Settypes ("novel")

. Setsearchtype (SearchType. Dfs_query_then_fetch)

. Setquery (Boolquerybuilder)

. Setfrom (0)

. SetSize (10);

System. out. println ("Requestbuilder" + requestbuilder);

SearchResponse SearchResponse = Requestbuilder.get ();

list<map<string, object>> resultlist = new arraylist<map<string,object>> ();

for (Searchhit hit:searchResponse.getHits ()) {

Resultlist.add (Hit.getsource ());

}

System. out. println ("resultlist:" + resultlist);

}

Java-based ES development

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.