Home  >  Article  >  Java  >  How to create an index in java

How to create an index in java

little bottle
little bottleOriginal
2019-05-23 10:24:557008browse

How to create an index in java: first convert the object into a JSON string; then write the json document into the index; finally use Java code to create a new Java project and write the index creation code call in it. .

How to create an index in java

An index is an on-disk structure associated with a table or view that speeds up the retrieval of rows from the table or view. An index contains keys generated from one or more columns in a table or view. In fact, the key purpose of the index is to speed up retrieval. Therefore, how to use the index is a matter of the database system itself. As a database designer or user, you can design and create the index and then experience how the query becomes faster after adding the index. . So, how to create an index in Java?

1. Generate JSON

The first step in creating an index is to convert the object into a JSON string

2 , Create an index

Write the json document into the index

3. Java implements a new Java project

The specific code is as follows:

import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Date;
import java.util.List;

import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.common.xcontent.XContentBuilder;

import cn.com.bropen.entity.DataFactory;
import static org.elasticsearch.common.xcontent.XContentFactory.*;

public class ElasticSearchHandler {
    public static void main(String[] args) {
        try {
            /* 创建客户端 */
            // client startup
            Client client = TransportClient.builder().build()
                    .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9300));

            List<String> jsonData = DataFactory.getInitJsonData();

            for (int i = 0; i < jsonData.size(); i++) {
                IndexResponse response = client.prepareIndex("blog", "article").setSource(jsonData.get(i)).get();
                if (response.isCreated()) {
                   System.out.println("创建成功!");
                }
            }
            client.close();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}

The above is the detailed content of How to create an index in java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:what is java unit testingNext article:what is java unit testing