1. Add Maven Dependency
XML code
- <dependency>
- <groupId>org.elasticsearch</groupId>
- <artifactid>elasticsearch</artifactid>
- <version>0.90.0</version>
- </Dependency>
It is recommended to use MAVEN to manage the project because Elasticsearch has a lot of dependencies and manual maintenance is cumbersome
2. Create a client that connects to the Elasticsearch service
Java code
- Settings Settings = Immutablesettings.settingsbuilder (). Put ("Client.transport.sniff", true). Put (" Cluster.name ", " Name of Node "). Build ();
- Client client = new Transportclient (Settings). addtransportaddress (new inetsockettransportaddress ("IP of Server ", 9300));
3. Create an index
Elasticsearch's Java client, which supports multiple ways of building index data, here are two ways to code examples: building data using Jsonbuilder
Java code
- Indexresponse response = Client.prepareindex ("Comment_index", "COMMENT_UGC", "comment_123674")
- . SetSource (Xcontentfactory.jsonbuilder ()
- . StartObject ()
- . Field ("Author", "569874")
- . Field ("Author_name", "riching")
- . Field ("Mark", 232)
- . Field ("Body", "Beijing is good, but too many people")
- . Field ("CreateDate", "20130801175520")
- . field ("valid", true)
- . EndObject ())
- . Setttl (8000)
- . Execute (). Actionget ();
- System.out.println (Response.getid ());
The other is to construct the data into a JSON string and pass it directly to the client.
Java code
- Student Student = new Student (103161066, " riching", "Beijing");
- String Jsonvalue = mapper.writevalueasstring (student);
- Response = Client.prepareindex ("Student_index", "Student_info", "stu_103161066"). SetSource (Jsonvalue). Execute (). Actionget ();
- System.out.println (Response.getid ());
The actual application should be the following one more convenient, you can put the objects need to be indexed directly past
4. Get data based on ID
Java code
- GetResponse responseget = Client.prepareget ("Comment_index", "COMMENT_UGC", "comment_123674"). Execute ( ). Actionget ();
- System.out.println (Responseget.getsourceasstring ());
5. Query Index
Java code
- Searchrequestbuilder builder = client.preparesearch ("Comment_index"). Settypes ("COMMENT_UGC"). Setsearchtype (Searchtype.default). Setfrom (0). SetSize (100);
- Boolquerybuilder QB = Querybuilders.boolquery (). Must (new Querystringquerybuilder ("Beijing"). Field ("Body")) /c2>
- . should (new Querystringquerybuilder ("Too many"). Field ("body"));
- Builder.setquery (QB);
- SearchResponse response = Builder.execute (). Actionget ();
- System.out.println ("" + response);
- System.out.println (Response.gethits (). Gettotalhits ());
Execution results
Java code
- {
- "took": 8,
- "Timed_out": false,
- "_shards": {
- "Total": 5,
- "Successful": 5,
- "failed": 0
- },
- "hits": {
- "Total": 1,
- "Max_score": 0.19178301,
- "hits": [{
- " _index": "Comment_index",
- " _type": "COMMENT_UGC",
- " _id": "comment_123674",
- "_score": 0.19178301, "_source": {"author":"569874", "author_name":"riching","mark ":232," body ":" Beijing is good, but too many people "," CreateDate ":" 20130801175520 "," valid ":true}
- } ]
- }
- }
- 1
6, delete the index, you can delete the index based on the index ID, you can also construct a query to delete, which is similar to Lucene API, but the API is not the same
Java code
- Deleteresponse response = Client.preparedelete ("Comment_index", "COMMENT_UGC", "comment_123674"). Setoperationthreaded (false). Execute (). Actionget ();
- System.out.println (Response.getid ());
This deletion has a small problem, if you delete immediately after the query or can be found
Elasticsearch Java Index Operations