ElasticSearch6.X Version Java Api Chinese Detailed Explanation (1) Index Api Analysis

The Inde API allows typed JSON documents to be indexed into a specific index and made searchable.

There are several different ways to generate JSON documents:

1. Manually (aka using it yourself) use native byte[] or as a string.

2. Use a map that will be automatically converted to its JSON equivalent.

3. Use a 3rd party library to serialize your beans like Jackson.

4. Use the built-in helper XContentFactory.jsonBuilder()

Internally, each type is converted to byte[] (so a string is converted to a byte[]). So if the object is already in this form then use it. jsonBuilder is a highly optimized JSON generator that directly constructs a byte[].

Nothing really difficult here, but note that you have to encode the date in a date format.

String json = "{" +
        "\"user\":\"kimchy\"," +
        "\"postDate\":\"2013-01-30\"," +
        "\"message\":\"trying out Elasticsearch\"" +
    "}";

Use Map

A Map is a collection of key:value pairs. It represents a 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");

serialize your beans

You can use Jackson to serialize your beans to JSON. Please add Jackson Databind to your project. Then you can use ObjectMapper to serialize your beans.

import com.fasterxml.jackson.databind.*;

// instance a json mapper
ObjectMapper mapper = new ObjectMapper(); // create once, reuse

// generate json
byte[] json = mapper.writeValueAsBytes(yourbeaninstance);

使用Elasticsearch helpers

Elasticsearch provides built-in help for generating JSON content.

import static org.elasticsearch.common.xcontent.XContentFactory.*;

XContentBuilder builder = jsonBuilder()
    .startObject ()
        .field("user", "kimchy")
        .field("postDate", new Date())
        .field("message", "trying out Elasticsearch")
    .endObject()

Note that you can also add arrays using the startArray(String) and endArray() methods. By the way, field methods accept many object types. You can pass numbers, dates or even other XContentBuilder objects directly.

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

String json = builder.string();
The following example indexes a JSON document into an index called twitter with type tweet and id 1:
import static org.elasticsearch.common.xcontent.XContentFactory.*;

IndexResponse response = client.prepareIndex("twitter", "tweet", "1")
        .setSource (jsonBuilder ()
                    .startObject ()
                        .field("user", "kimchy")
                        .field("postDate", new Date())
                        .field("message", "trying out Elasticsearch")
                    .endObject()
                  )
        .get();

Note that you can also index documents as JSON strings without having to give IDs:

String json = "{" +
        "\"user\":\"kimchy\"," +
        "\"postDate\":\"2013-01-30\"," +
        "\"message\":\"trying out Elasticsearch\"" +
    "}";

IndexResponse response = client.prepareIndex("twitter", "tweet")
        .setSource(json, XContentType.JSON)
        .get();
The IndexResponse object will give you a report:
// Index name
String _index = response.getIndex();
// Type name
String _type = response.getType();
// Document ID (generated or not)
String _id = response.getId();
// Version (if it's the first time you index this document, you will get: 1)
long _version = response.getVersion();
// status has stored current instance statement.
RestStatus status = response.status();

for reference only.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324669092&siteId=291194637