Elasticsearch Java API Basic use of the increase, delete, change, check __elasticsearch

Source: Internet
Author: User
Tags create index createindex

Java official documentation for the main reference el: https://www.elastic.co/guide/en/elasticsearch/client/java-api/1.7/generate.html

A blog: http://www.cnblogs.com/huangfox/p/3543134.html

Elasticsearch Official Guide: http://es.xiaoleilu.com/010_Intro/30_Tutorial_Search.html

Key concepts of @xuguokun1986 code

Understand the following nouns, as mentioned in the previous article, indexed (indices)-> databases, types (types)-> tables, documents (documents)-> rows, fields (Fields)-> columns;-> The table that follows represents the basic relational database. The basic dependent jar package is contained in the corresponding Lib folder under the Elasticsearch folder, where the JSON file relies on the download address http://wiki.fasterxml.com/JacksonDownload of the jar package. problems encountered and solutions

Client = new Transportclient (). addtransportaddress (New inetsockettransportaddress ("192.168.203.148", 9200);

Error no node available

WORKAROUND: Here 9200 rewrite the 9300 code to create the index and insert the data code as shown below:

<pre name= "code" class= "HTML" ><pre name= "code" class= "HTML" >package com.asia.myTest;

Import java.io.IOException;

Import Net.sf.json.JSONObject;
Import org.elasticsearch.action.*;
Import Org.apache.lucene.search.TermQuery;
Import Org.codehaus.jackson.map.ObjectMapper;
Import Org.codehaus.jackson.map.util.JSONPObject;
Import org.elasticsearch.ElasticsearchException;
Import Org.elasticsearch.action.count.CountResponse;
Import Org.elasticsearch.action.delete.DeleteResponse;
Import Org.elasticsearch.action.get.GetResponse;
Import Org.elasticsearch.action.index.IndexRequestBuilder;
Import Org.elasticsearch.action.index.IndexResponse;
Import Org.elasticsearch.action.search.SearchResponse;
Import Org.elasticsearch.action.search.SearchType;
Import org.elasticsearch.client.Client;
Import org.elasticsearch.client.transport.TransportClient;
Import org.elasticsearch.common.transport.InetSocketTransportAddress;
Import Org.elasticsearch.common.xcontent.XContentFactory; Import Org.elastIcsearch.index.query.FilterBuilders;
Import Org.elasticsearch.index.query.QueryBuilder;
Import Org.elasticsearch.index.query.QueryBuilders;

Import org.elasticsearch.index.query.querybuilders.*;


Import static org.elasticsearch.index.query.filterbuilders.*;
	
	public class Esclient {private Client client;
				 public void init () {//on Start is equivalent to connecting the cluster client = new Transportclient ().
	Addtransportaddress (New Inetsockettransportaddress ("192.168.203.148", 9300));
	The public void Close () {//on shutdown disconnects the cluster client.close (); * * * CREATE INDEX, convert documents into JSON format store/public void CreateIndex () {for (int i=0; i<=200;i++) {Indexre
			Sponse indexresponse = null;
						                            try {indexresponse = Client.prepareindex ("Logs", "log2015", i+ ""). SetSource ( Xcontentfactory.jsonbuilder (). StartObject (). Field ("Sour CeIp "," 10.10.16. " +i). fieLD ("Sourceport", 389). Field ("Destip", "114.114.114.114") . EndObject ()). Execute (). Action
			Get ();
			catch (Elasticsearchexception e) {//TODO auto-generated catch block E.printstacktrace ();
			catch (IOException e) {//TODO auto-generated catch block E.printstacktrace ();
		} System.out.println ("Responseiscreated:" +indexresponse.iscreated ()); } System.out.println ("It is OK.")
	"); /* * Get index gets the document equivalent to reading a row of data from a database/public void get () {GetResponse GetResponse = Client.prepareget ("Logs", "Lo
		G2015 "," 1 "). Execute (). Actionget ();
	System.out.println (Getresponse.getsourceasstring ()); /* *delete index Deletes a document equivalent to deleting a row of data/public void Delete () {Deleteresponse deleteresponse = Client.preparedelete ( "Logs", "log2015", "he"). Execute (). Actionget ();
	System.out.println (Deleteresponse.getversion ()); }/* *search query is equivalent to a relational database query/public void search () {SearchResponse searchresponse = Client.preparesearch ("Logs" ). Settypes ("log2015"). Setsearchtype (Searchtype.dfs_query_then_fetch). Setquery (QUERY Builders.termquery ("Destip", "114.114.114.114")). Setpostfilter (Filterbuilders.rangefilter ("Sou
								  		Rceip "). From (" 10.10.16.57 "). to (" 10.10.16.68 "). Setfrom (0)
		
		. SetSize (3). Setexplain (True). Execute (). Actionget (); 
System.out.println (Searchresponse.tostring ());
Jsonobject jsonobject = Jsonobject.fromobject (searchresponse.tostring ());
Jsonobject hites = (jsonobject) jsonobject.get ("hits");
	System.out.println (Hites.get ("hits"). toString ());
				}/* *count API Statistic Analysis result */public void Count () {Countresponse countresponse = client.preparecount ("website") . Setquery (QuerybuilDers.termquery ("_type", "Asia")). Execute (). Actionget ();
	
	System.out.println (Countresponse.getcount ());
    	public static void Main (string[] args) {esclient client = new Esclient ();
    	Client.init ();
    	Client.createindex ();
    	Client.get ();
    	Client.delete ();
    	Client.search ();
    	Client.count ();
    	
    	
    Client.close ();
 }
}



Console Run Process: ... responseiscreated:true responseiscreated:true
Responseiscreated:true
Responseiscreated:true
Responseiscreated:true
Responseiscreated:true
Responseiscreated:true
It is OK.
To view basic results through the Curl command, insert data successfully:

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.