zoukankan      html  css  js  c++  java
  • elasticsearch 中文API 索引(三)

    索引API

    索引API允许开发者索引类型化的JSON文档到一个特定的索引,使其可以被搜索。

    生成JSON文档

    有几种不同的方式生成JSON文档

    • 利用byte[]或者作为一个String手动生成
    • 利用一个Map将其自动转换为相应的JSON
    • 利用第三方库如Jackson去序列化你的bean
    • 利用内置的帮助函数XContentFactory.jsonBuilder()

    手动生成

    需要注意的是,要通过Date Format编码日期。

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

    使用map

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

    序列化bean

    elasticsearch早就用到了Jackson,把它放在了org.elasticsearch.common.jackson下面。你可以在你的pom.xml文件里面添加你自己的Jackson版本。

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

    这样,你就可以序列化你的bean为JSON。

    import com.fasterxml.jackson.databind.*;
    
    // instance a json mapper
    ObjectMapper mapper = new ObjectMapper(); // create once, reuse
    
    // generate json
    String json = mapper.writeValueAsString(yourbeaninstance);
    

    利用elasticsearch帮助类

    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 json = builder.string();
    

    索引文档

    下面的例子将JSON文档索引为一个名字为“twitter”,类型为“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();
    

    你也可以不提供id:

    String json = "{" +
            ""user":"kimchy"," +
            ""postDate":"2013-01-30"," +
            ""message":"trying out Elasticsearch"" +
        "}";
    
    IndexResponse response = client.prepareIndex("twitter", "tweet")
            .setSource(json)
            .execute()
            .actionGet();
    

    IndexResponse将会提供给你索引信息

    // 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();
    

    如果你在索引时提供了过滤,那么IndexResponse将会提供一个过滤器(percolator )

    IndexResponse response = client.prepareIndex("twitter", "tweet", "1")
            .setSource(json)
            .execute()
            .actionGet();
    
    List<String> matches = response.matches();
  • 相关阅读:
    【笔记】Nios II PIO的说明与双向操作注意点
    【笔记】C++ 类与对象
    【连载】 FPGA Verilog HDL 系列实例矩阵键盘接口
    【笔记】C++ 指针和引用的使用
    【笔记】C++ 多态性
    VB 串口通信 MSComm控件的使用
    【笔记】C++ 继承
    【连载】 FPGA Verilog HDL 系列实例乐曲演奏
    AJAX Conrtol Toolkit——HoverMenuExtender(停靠菜单)
    AJAX Control Toolkit DropDown
  • 原文地址:https://www.cnblogs.com/bmaker/p/5472429.html
Copyright © 2011-2022 走看看