java 创建 ES 中 父子文档

java 创建 ES 中 父子文档,第1张

java 创建 ES 中 父子文档
//注入client
@Autowired RestHighLevelClient client = new RestHighLevelClient( RestClient.builder(new HttpHost("localhost",9200,"http")));

//创建索引
public void createFCIndex() throws IOException {
    Settings.Builder settings = Settings.builder();
    settings.put("number_of_shards",5);
    settings.put("number_of_replicas",1);
    XContentBuilder mapping = JsonXContent.contentBuilder()
            .startObject()
            .startObject("properties")
            .startObject("cancelSerialNo")
            .field("type", "keyword")
            .endObject()
            .startObject("settleBillNo")
            .field("type", "keyword")
            .endObject()
            .startObject("settleBillSubNo")
            .field("type", "keyword")
            .endObject()
            .startObject("stockNo")
            .field("type", "keyword")
            .endObject()
            .startObject("stockSubNo")
            .field("type", "keyword")
            .endObject()
            .startObject("orderNo")
            .field("type", "keyword")
            .endObject()
            .startObject("orderSubNo")
            .field("type", "keyword")
            .endObject()
            .startObject("invoiceNo")
            .field("type", "keyword")
            .endObject()
            .startObject("invoiceCode")
            .field("type", "keyword")
            .endObject()
            .startObject("amount")
            .field("type", "keyword")
            .endObject()
            .startObject("systemId")
            .field("type", "keyword")
            .endObject()
            .startObject("invoiceWt")
            .field("type", "keyword")
            .endObject()
            .startObject("unitPrice")
            .field("type", "keyword")
            .endObject()
            .startObject("parent_child_relation")
            .field("type", "join")
            .startObject("relations")
            .field("parent", new String[]{"child"})
            .endObject()
            .endObject()
            .endObject()
            .endObject();
    CreateIndexRequest request = new CreateIndexRequest("settle_wip_detail_index");
    request.mapping(mapping).settings(settings);
    CreateIndexResponse response = client.indices().create(request, RequestOptions.DEFAULT);
    System.out.println("创建父子文档索引成功");
}
//导入父文档
public void parentDate() throws IOException {

    JSonObject jsonObject = new JSonObject();
    jsonObject.put("cancelSerialNo","1");
    jsonObject.put("settleBillNo","2");
    jsonObject.put("settleBillSubNo","3");
    jsonObject.put("stockNo","4");
    jsonObject.put("stockSubNo","5");
    jsonObject.put("orderNo","6");
    jsonObject.put("orderSubNo","7");
    jsonObject.put("invoiceNo","8");
    jsonObject.put("invoiceCode","9");
    jsonObject.put("amount","9");
    jsonObject.put("systemId","1");
    JSonObject relationObject = new JSonObject();
    relationObject.put("name","parent");
    jsonObject.put("parent_child_relation",relationObject);
    IndexRequest request = new IndexRequest("settle_wip_detail_index").id("parent1")
            .source(jsonObject.toString(), XContentType.JSON);
    IndexResponse response = client.index(request, RequestOptions.DEFAULT);
    System.out.println("插入父文档成功");
}

//导入子文档
public void childDate() throws IOException {
    JSonObject jsonObject = new JSonObject();
    jsonObject.put("cancelSerialNo","11");
    jsonObject.put("settleBillNo","21");
    jsonObject.put("settleBillSubNo","31");
    jsonObject.put("stockNo","41");
    jsonObject.put("stockSubNo","51");
    jsonObject.put("orderNo","61");
    jsonObject.put("orderSubNo","71");
    jsonObject.put("invoiceNo","81");
    jsonObject.put("invoiceCode","91");
    jsonObject.put("amount","91");
    jsonObject.put("systemId","11");
    JSonObject relationObject = new JSonObject();
    relationObject.put("name","child");
    relationObject.put("parent","parent1");
    jsonObject.put("parent_child_relation",relationObject);
    IndexRequest request = new IndexRequest("settle_wip_detail_index").id("child1")
            .source(jsonObject.toString(), XContentType.JSON)
            .routing("parent1");
    IndexResponse response = client.index(request, RequestOptions.DEFAULT);
    System.out.println("插入子文档成功");
}

其中cancelSerialNo,settleBillNo等为字段名;parent和child为关系身份名;parent1与child1为_id

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5696262.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存