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. .
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!