【问题标题】:Unable to create an index with nested json using Jest elastic client无法使用 Jest 弹性客户端创建具有嵌套 json 的索引
【发布时间】:2015-12-17 06:12:43
【问题描述】:

我使用 java 和 Jest (https://github.com/searchbox-io/Jest) 作为弹性搜索集群的客户端。 尝试在http://localhost:9200/myindex/mytype中使用以下结构创建和索引

{
  "doc": {
    "_content": "mycontent",
    "_name": "mypdf.pdf",
    "_content_type": "application/pdf"
  }
}
下面是我的java代码,
 XContentBuilder docObject = jsonBuilder().startObject().field("_content", doc).field("_name", name).field("_content_type", contentType)
                .field("title", title).field("author", author).endObject();
        index1 = new Index.Builder(docObject).build();
        source = jsonBuilder()
                .startObject()
                .field("doc", docObject.string())
                .endObject().string();
 Index index = new Index.Builder(source).index(baseIndexName).type(ElasticConstants.BASE_TYPE).build();

但是当它被执行时,源不会作为 Json 传递,键“doc”的值作为字符串文本传递,因为没有创建索引。如何使用 Jest 将嵌套的 json 对象传递给 Index.Builder?

【问题讨论】:

    标签: elasticsearch elasticsearch-jest


    【解决方案1】:
        //Instantiate JEST Client
        JestClientFactory factory = new JestClientFactory();
        HttpClientConfig config = new HttpClientConfig.
                Builder("http://localhost:9201")
                .multiThreaded(true).build();
        factory.setHttpClientConfig(config);
        JestClient jestClient = factory.getObject();
    
        //create index
        jestClient.execute(new CreateIndex.Builder("so4index").build());
    
    
    
    
        //create index mapping
        RootObjectMapper.Builder rootObjMapBuilder1 = new RootObjectMapper.Builder("so1type");
        RootObjectMapper.Builder rootObjMapBuilder = new RootObjectMapper.Builder("doc");   
        rootObjMapBuilder.add(new StringFieldMapper.Builder("_content").store(true));
        rootObjMapBuilder.add(new StringFieldMapper.Builder("_name").store(true));
        rootObjMapBuilder.add(new StringFieldMapper.Builder("_content_type").store(true));
        rootObjMapBuilder1.add(rootObjMapBuilder);
    
        DocumentMapper docMapper = new DocumentMapper.Builder("so4index", null, rootObjMapBuilder1).build(null);
        String source = docMapper.mappingSource().toString();
    
    
        PutMapping putMapping = new PutMapping.Builder("so4index", "so1type", source).build();
    
        JestResult mappingResult = jestClient.execute(putMapping);
    
        //Insert Document Content
        String doc="mycontent";
        String name="mypdf.pdf";
        String contentType = "application/pdf";
        XContentBuilder docObject = jsonBuilder()
                .startObject()
                .startObject("doc").field("_content", doc)
                .field("_name", name)
                .field("_content_type", contentType)
                .endObject()
                .endObject();
    
       Index index = new Index.Builder(docObject.string()).index("so4index").type("so1type").build();
       JestResult jestResult= jestClient.execute(index);
    
    
       jestClient.shutdownClient();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-10
      • 1970-01-01
      • 1970-01-01
      • 2020-09-06
      • 2015-05-06
      相关资源
      最近更新 更多