栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

ES JAVA代码操作

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

ES JAVA代码操作

//手动的创建映射  根据需求来确定

   
    @Test
    public void putMapping() throws  Exception{
        PutMappingRequest putMappingRequest = new PutMappingRequest("twitter");
        XContentBuilder xContentbuidler = XContentFactory.jsonBuilder()
                .startObject()//{
                    .startObject("properties")//"properties" : {
                        .startObject("id") // "id" : {
                            .field("type","long") //"type" : "long"
                            .field("store","true")
                        .endObject()//}
                        .startObject("title") // "title" : {
                            .field("type","text")
                            .field("store","true")
                            .field("index","true")
                            .field("analyzer","ik_smart")
                        .endObject()//}
                        .startObject("content") // "title" : {
                            .field("type","text")
                            .field("store","true")
                            .field("index","true")
                            .field("analyzer","ik_smart")
                        .endObject()//}
                    .endObject()// }
                .endObject();//}
        putMappingRequest.source(xContentbuidler);
        highLevelClient.indices().putMapping(putMappingRequest,RequestOptions.DEFAULT);
    }

//文档的操作

    @Autowired
    private ObjectMapper objectMapper;

    //添加文档 更新文档
    @Test
    public void add() throws Exception {

        //批量的导入100条  bulk
        long l = System.currentTimeMillis();
        BulkRequest request = new BulkRequest();//list

        request.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE);//刷新的策略 默认的情况下需要每隔1S刷新  采用立刻刷新的策略

        for (long i = 0; i < 100; i++) {
            IndexRequest indexrequest = new IndexRequest("twitter");
            Article article = new Article(i, "黑马666 elasticsearch"+i, "黑马写的文章非常6666"+i);
            String jsonstr = objectMapper.writeValueAsString(article);
            indexrequest.source(jsonstr, XContentType.JSON)
                    //设置文档的唯一标识
                    .id(""+i);
            //highLevelClient.index(indexrequest, RequestOptions.DEFAULT);//添加索引和类型和文档
            request.add(indexrequest);
        }
        //执行批量操作
        highLevelClient.bulk(request,RequestOptions.DEFAULT);
        long l1 = System.currentTimeMillis();
        System.out.println(l1-l);
    }

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/863212.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号