Elasticsearch Java Index Operations

Source: Internet
Author: User

1. Add Maven Dependency

XML code
  1. <dependency>
  2. <groupId>org.elasticsearch</groupId>
  3. <artifactid>elasticsearch</artifactid>
  4. <version>0.90.0</version>
  5. </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
    1. Settings Settings = Immutablesettings.settingsbuilder (). Put ("Client.transport.sniff", true). Put ("  Cluster.name ", " Name of Node "). Build ();
    2. 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
  1. Indexresponse response = Client.prepareindex ("Comment_index", "COMMENT_UGC", "comment_123674")
  2. . SetSource (Xcontentfactory.jsonbuilder ()
  3. . StartObject ()
  4. . Field ("Author", "569874")
  5. . Field ("Author_name", "riching")
  6. . Field ("Mark", 232)
  7. . Field ("Body", "Beijing is good, but too many people")
  8. . Field ("CreateDate", "20130801175520")
  9. . field ("valid", true)
  10. . EndObject ())
  11. . Setttl (8000)
  12. . Execute (). Actionget ();
  13. 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
    1. Student Student = new Student (103161066, " riching", "Beijing");
    2. String Jsonvalue = mapper.writevalueasstring (student);
    3. Response = Client.prepareindex ("Student_index", "Student_info", "stu_103161066"). SetSource (Jsonvalue).  Execute (). Actionget ();
    4. 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
    1. GetResponse responseget = Client.prepareget ("Comment_index", "COMMENT_UGC", "comment_123674"). Execute (  ). Actionget ();
    2. System.out.println (Responseget.getsourceasstring ());



5. Query Index

Java code
  1. Searchrequestbuilder builder = client.preparesearch ("Comment_index"). Settypes ("COMMENT_UGC").  Setsearchtype (Searchtype.default). Setfrom (0). SetSize (100);
  2. Boolquerybuilder QB = Querybuilders.boolquery (). Must (new Querystringquerybuilder ("Beijing"). Field ("Body")) /c2>
  3. . should (new Querystringquerybuilder ("Too many"). Field ("body"));
  4. Builder.setquery (QB);
  5. SearchResponse response = Builder.execute (). Actionget ();
  6. System.out.println ("" + response);
  7. System.out.println (Response.gethits (). Gettotalhits ());


Execution results

Java code
  1. {
  2. "took": 8,
  3. "Timed_out": false,
  4. "_shards": {
  5. "Total": 5,
  6. "Successful": 5,
  7. "failed": 0
  8. },
  9. "hits": {
  10. "Total": 1,
  11. "Max_score": 0.19178301,
  12. "hits": [{
  13. " _index": "Comment_index",
  14. " _type": "COMMENT_UGC",
  15. " _id": "comment_123674",
  16. "_score": 0.19178301, "_source": {"author":"569874", "author_name":"riching","mark ":232," body ":" Beijing is good, but too many people "," CreateDate ":" 20130801175520 "," valid ":true}
  17. } ]
  18. }
  19. }
  20. 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
    1. Deleteresponse response = Client.preparedelete ("Comment_index", "COMMENT_UGC", "comment_123674").  Setoperationthreaded (false). Execute (). Actionget ();
    2. 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

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.