构建数据:


   @Test
   public void createIndex(){
       /**
        * 创建索引
        * */
       client.admin().indices().prepareCreate("player").get();
  }



   /**
    * 创建映射
    */
   @Test
   public void testCreateIndexMapping_boost() throws Exception{
       /**
        * 格式:
        "mappings": {
           "player": {
               "properties": {
                    "name": {"index": "not_analyzed","type": "string"},
                   "age": {"type": "integer"},
                   "salary": {"type": "integer"},
                   "team": {"index": "not_analyzed","type": "string"},
                   "position": {"index": "not_analyzed","type": "string"}
               }
           }
        }

        */
       //构建json的数据格式,创建映射
       XContentBuilder mappingBuilder = XContentFactory.jsonBuilder()
              .startObject()
              .startObject("player")
              .startObject("properties")
              .startObject("name").field("type","string").field("index", "not_analyzed").endObject()
              .startObject("age").field("type","integer").endObject()
              .startObject("salary").field("type","integer").endObject()
              .startObject("team").field("type","string").field("index", "not_analyzed").endObject()
              .startObject("position").field("type","string").field("index", "not_analyzed").endObject()
              .endObject()
              .endObject()
              .endObject();
       PutMappingRequest request = Requests.putMappingRequest("player")
              .type("player")
              .source(mappingBuilder);
       client.admin().indices().putMapping(request).get();
  }

   @Test
   public void BulkInsertDocument() throws IOException {
       BulkRequestBuilder bulkRequest = client.prepareBulk();

// either use client#prepare, or use Requests# to directly build index/delete requests
       bulkRequest.add(client.prepareIndex("player", "player", "1")
              .setSource(jsonBuilder()
                      .startObject()
                      .field("name", "郭德纲")
                      .field("age", 33)
                      .field("salary",3000)
                      .field("team" , "cav")
                      .field("position" , "sf")
                      .endObject()
              )
      );
       bulkRequest.add(client.prepareIndex("player", "player", "2")
              .setSource(jsonBuilder()
                      .startObject()
                      .field("name", "于谦")
                      .field("age", 25)
                      .field("salary",2000)
                      .field("team" , "cav")

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-11-22
  • 2021-08-03
  • 2021-11-10
  • 2022-01-12
  • 2021-06-09
猜你喜欢
  • 2021-10-20
  • 2021-10-20
  • 2022-12-23
  • 2022-12-23
  • 2021-10-20
  • 2021-08-10
  • 2021-07-02
相关资源
相似解决方案