zoukankan      html  css  js  c++  java
  • Elastic Search Java Api 创建索引结构,添加索引

    创建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();
  • 相关阅读:
    poj 2955 区间dp
    LightOJ
    hdu 2089 数位dp
    LightOJ
    等比数列
    江西财经大学第一届程序设计竞赛 G题 小Q的口袋校园
    江西财经大学第一届程序设计竞赛 F题 解方程
    江西财经大学第一届程序设计竞赛 H题 求大数的阶乘
    Google 的Web开发相关工具
    Android Vitals各性能指标介绍
  • 原文地址:https://www.cnblogs.com/yaomajor/p/7767058.html
Copyright © 2011-2022 走看看