Elastic Search Java Api 創建索引結構,添加索引

y37f 9年前發布 | 9K 次閱讀 Java

創建TCP客戶端

Client client = new TransportClient()
             .addTransportAddress(new InetSocketTransportAddress(
                     "localhost", 9300));

創建索引
    client.admin().indices().prepareCreate("pages").execute().actionGet();

創建索引結構
    XContentBuilder builder=XContentFactory
            .jsonBuilder()
            .startObject()
                .startObject("sina")
                .startObject("properties")
                    .startObject("article_title")
                        .field("type", "string")
                        .field("store", "yes")
                        .field("analyzer","ik")
                        .field("index","analyzed")
                    .endObject()
                    .startObject("article_content")
                        .field("type", "string")
                        .field("store", "no")
                        .field("analyzer","ik")
                        .field("index","analyzed")
                    .endObject()
                    .startObject("article_url")
                        .field("type", "string")
                        .field("store", "yes")
                        .field("index","not_analyzed")
                    .endObject()
                .endObject()
            .endObject()
        .endObject();
PutMappingRequest mapping = Requests.putMappingRequest("pages").type("sina").source(builder);
client.admin().indices().putMapping(mapping).actionGet();

添加索引數據
IndexResponse response = client.prepareIndex("pages", "sina", null) .setSource(jsonBuilder() .startObject() .field("article_title", Bytes.toString(r.getValue("article".getBytes(), "title".getBytes()))) .field("article_content", Bytes.toString(r.getValue("article".getBytes(), "content".getBytes()))) .field("article_url", Bytes.toString(r.getValue("article".getBytes(), "url".getBytes()))) .endObject() ) .execute() .actionGet();

client.close();</pre>



 本文由用戶 y37f 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!