Index of the Javaapi of Elasticsearch

Source: Internet
Author: User

Index API

Original: http://www.elasticsearch.org/guide/en/elasticsearch/client/java-api/current/index_.html

The index API allows you to convert a JSON document to a specific index, making it easy to search for operations.

To generate a JSON document:

There are several different ways to generate a JSON document:

    • Manual use byte[] orString
    • Use a map to convert the equivalent to JSON
    • use a third-party library to reload beans (such as Jackson)
    • Using the built-in Xcontentfactory.jsonbuilder ()

Internally, each type is converted to byte[] (so the string is converted to byte[] ). So if the object is already in this form, use it directly. A jsonBuilder 是 highly optimized JSON generation mechanism that is used to directly construct the byte[] .

Give it a try:

There is no particular difficulty here, but please note that you will have to format the date according to Dateformate.

String json = "{" + "\" User\ ": \" kimchy\ "," + "\" postdate\ ": \" 2013-01-30\ "," + "\" message\ ": \" Trying O UT elasticsearch\ "" + "}";
Using map

Map is a key: a set of value pairs. It better represents the JSON structure:

map<string, object> json = new hashmap<string, object> (), Json.put ("User", "Kimchy"), Json.put ("Postdate", New Date ()); Json.put ("message", "trying out Elasticsearch");
Serialization of Beans

Elasticsearch has used Jackson in the org.elasticsearch.common.jackson package. So you can add your own Jackson version to a pom.xml file or in your classpath.

For example:

<dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactid>jackson-databind </artifactId> <version>2.1.3</version></dependency>

You can then start serializing the JSON bean:

Import com.fasterxml.jackson.databind.*;//Instance a JSON mapperobjectmapper mapper = new Objectmapper (); Create once, reuse//generate jsonstring json = mapper.writevalueasstring (yourbeaninstance);
Using Elasticsearch Helpers

Elasticsearch provides built-in helper to generate JSON content

Import static org.elasticsearch.common.xcontent.xcontentfactory.*; Xcontentbuilder builder = Jsonbuilder (). StartObject (). Field ("User", "Kimchy"). Field ("Postdate", New Da Te ()). Field ("Message", "trying out Elasticsearch"). EndObject ()

Note that you can also add arrays startArray(String) and endArray() methods. By the way, the field method accepts many object types. You can use numbers, dates, and even other Xcontentbuilder objects directly.

If you need to look at the generated JSON content, you can use the string() method.

String json = builder.string ();
Indexdocument:

The following example index converts a JSON document to an index (Twitter) with a type of tweet,id of 1:

Import static Org.elasticsearch.common.xcontent.xcontentfactory.*;indexresponse response = Client.prepareindex (" Twitter "," tweet "," 1 "). SetSource (Jsonbuilder (). StartObject () field (" U  Ser "," Kimchy "). Field (" Postdate ", New Date ()). Field (" Message "," trying out Elasticsearch "). EndObject ()). Execute (). Actionget ();

Note that you can also index document as a JSON Strings, you do not have to give an ID:

String json = "{" + "\" User\ ": \" kimchy\ "," + "\" postdate\ ": \" 2013-01-30\ "," + "\" message\ ": \" Trying O        UT elasticsearch\ "" + "}"; Indexresponse response = Client.prepareindex ("Twitter", "tweet"). SetSource (JSON) . Execute (). Actionget ();

IndexResponse The object will give you a report:

Index namestring _index = Response.getindex ();//Type namestring _type = Response.gettype ();//Document ID (Generated O R not) String _id = Response.getid ();//Version (if it's the first time you index this document, you'll get:1) long _vers Ion = Response.getversion ();

If you use percolation to construct the index, IndexResponse the object will give the corresponding filter:

Indexresponse response = Client.prepareindex ("Twitter", "tweet", "1"). SetSource (JSON). Execute (). AC Tionget (); list<string> matches = Response.matches ();

Operation Threading:

The Index API allows you to set up threads to perform operations, and the actual execution API executes on the same node (the API performs an assignment on Shard on the same server).

perform operations on different threads, or invoke thread execution (note that the API is still asynchronous). By default, operationThreaded This means that the true, operation is performed on a different thread.


Original: http://www.elasticsearch.org/guide/en/elasticsearch/client/java-api/current/index_.html

I hope that the translation is not misleading.

Index of the Javaapi of Elasticsearch

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.