elasticsearch的javaAPI之index

Index API

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

index API同意你将JSON document转换为一个特定的index,使它便于搜索操作。

生成JSON文档:

有几种不同的方法生成一个JSON document:

  • 手动使用 byte[] String
  • 使用一个map来等效转换为JSON
  • 使用第三方库来将beans装换(如Jackson)
  • 使用内置的XContentFactory.jsonBuilder()

在内部,每个类型转换为 byte[] (所以String转换到byte[])。 因此,假设对象是已经在这个形式, 就直接使用它。 jsonBuilder 是高度优化的JSON生成机制,用来直接构造为byte[]

动手试一试:

这里没有特别困难的,但请注意,您将必须依据DateFormate来格式化日期。

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

使用map

map是一个关键:值对集合。 它较好的表示JSON 结构:

Map<String, Object> json = new HashMap<String, Object>();
json.put("user","kimchy");
json.put("postDate",new Date());
json.put("message","trying out Elasticsearch");

序列化beans

Elasticsearch已经使用jackson,在org.elasticsearch.common.jackson 包中。 所以,你能够加入自己Jackson版本号到 pom.xml 文件或在你的classpath中。

比如:

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

然后,您能够開始序列化JSON bean:

import com.fasterxml.jackson.databind.*;

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

// generate json
String json = mapper.writeValueAsString(yourbeaninstance);

使用Elasticsearch helpers

Elasticsearch提供了内置的帮手来生成JSON内容

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

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

注意,您还能够加入数组 startArray(String)endArray() 方法。 顺便说一下, field方法 接受非常多对象类型。

你能够直接使用数字、日期,甚至其它XContentBuilder对象。

假设你须要看生成的JSON内容,您能够使用string()方法。

String json = builder.string();

Indexdocument:

以下的演示样例索引将JSON document转换为一个索引 (twitter),type为tweet,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()
                  )
        .execute()
        .actionGet();

请注意,您也能够index document作为JSON Strings,你不用给一个ID:

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

IndexResponse response = client.prepareIndex("twitter", "tweet")
        .setSource(json)
        .execute()
        .actionGet();

IndexResponse 对象将给你一个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();

假设你使用percolation构造索引, IndexResponse对象会给对应的过滤器:

IndexResponse response = client.prepareIndex("twitter", "tweet", "1")
        .setSource(json)
        .execute()
        .actionGet();

List<String> matches = response.matches();

Operation threading:

Index API同意你设置线程来运行操作,实际运行API上运行的是同样的节点(API上运行一个分配在同一server的shard上)。

不同的线程上运行操作,或调用线程运行(注意,API仍然是异步)。

默认情况下, operationThreaded 被设置为true。这意味着操作是由不同的线程上运行。 


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

翻译欠佳,希望不会对大家造成误导

原文地址:https://www.cnblogs.com/wzzkaifa/p/7019890.html