欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

elasticsearch 5.x java doc

程序员文章站 2022-07-05 09:04:17
...
    //查询
    String index = "客户表_"+公司ID;//es  索引
    String type = "good"; //es  type

    //查询组装
    BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
    //简单查询
    // 等于
    boolQueryBuilder.must(QueryBuilders.termQuery(fieldName, value));
    // 多个值
    boolQueryBuilder.must(QueryBuilders.termsQuery(fieldName, value));
    // 不等于
    boolQueryBuilder.mustNot(QueryBuilders.termQuery(fieldName, value));
    // 包含
    boolQueryBuilder.must(QueryBuilders.wildcardQuery(fieldName, "*" + value + "*"));
    // 不包含
    boolQueryBuilder.mustNot(QueryBuilders.wildcardQuery(fieldName, "*" + value + "*"));
    // 有值
    boolQueryBuilder.must(QueryBuilders.existsQuery(fieldName));
    // 没有值
    boolQueryBuilder.mustNot(QueryBuilders.existsQuery(fieldName));
    // 数字范围
    boolQueryBuilder.must(QueryBuilders.rangeQuery(fieldName).gt(dateStart).lt(dateEnd));
    // 日期
    // 绝对时间
    boolQueryBuilder.must(QueryBuilders.rangeQuery(fieldName).format("yyyy-MM-dd").timeZone("+08:00").gte(dateStart).lte(dateEnd));
    //查询复杂条件
    boolQueryBuilder.must(QueryBuilders.nestedQuery("even", QueryBuilders.termQuery("even.111", 222), ScoreMode.None));
   //has_child:基于子文档的内容,查找父文档
    boolQueryBuilder.must(JoinQueryBuilders.hasChildQuery("type",QueryBuilders.termQuery("openid","111"), ScoreMode.None));
    boolQueryBuilder.must(QueryBuilders.termQuery("mobile","186666666"));

    System.out.println("has_child:基于子文档的内容,查找父文档"+boolQueryBuilder);

    //has_child:基于子文档的内容,查找父文档
    boolQueryBuilder.must(JoinQueryBuilders.hasParentQuery(index,QueryBuilders.termsQuery("mobile","186666666"),true));
    boolQueryBuilder.must(QueryBuilders.termQuery("openid","111"));

    SearchResponse searchResponse = client.prepareSearch(index)
        .setTypes(type)
        .setQuery(boolQueryBuilder)
        .addSort(SortBuilders.fieldSort("_doc"))
        .setSize(10000)
        // 这个游标维持多长时间
        .setScroll(TimeValue.timeValueMinutes(15)).execute().actionGet();
    // 发送量
   Long count = searchResponse.getHits().getTotalHits();

    for (SearchHit hit : searchResponse.getHits()) {
        System.out.println(hit.getId());    //_id
        System.out.println(hit.getSource());//数据
    }


    Map<String,Object> map = new HashMap();
    map.put("name","张三");
    //单条新增
    IndexRequestBuilder indexRequestBuilder = client.prepareIndex(index,"event_two");
    indexRequestBuilder.setSource(map);
    indexRequestBuilder.setId("222222222");
    indexRequestBuilder.execute();


    //单条修改
    UpdateRequestBuilder requestBuilder =   client.prepareUpdate(index,type,"AWReEq5GvJs95AmUKpif");
    requestBuilder.setDoc(map);
    requestBuilder.execute();


   //父子级别新增
   IndexRequestBuilder indexRequestBuilder = client.prepareIndex(index,type);
   indexRequestBuilder.setSource(map);
   indexRequestBuilder.setParent("222222222");
   indexRequestBuilder.setId("222222222");
   indexRequestBuilder.execute();


   //父子集修改
   UpdateRequestBuilder requestBuilder =   client.prepareUpdate(index,type,"AWReEq5GvJs95AmUKpif");
   requestBuilder.setDoc(map);
   requestBuilder.setParent("222222222");
   requestBuilder.execute();


  批量增加
   String index = "客户表_"+公司ID;//es  索引
   String type = "good"; //es  type
  //获取默认组
  BulkRequestBuilder EsBulkRequest = client.prepareBulk();

    //同步es
  IndexRequestBuilder indexRequestBuilder = client.prepareIndex(index,type);
  indexRequestBuilder.setSource(map).setId(uuid.toString());
  EsBulkRequest.add(indexRequestBuilder);

  UpdateRequestBuilder updateBuilder = client.prepareUpdate(index,type,"_id");
                  updateBuilder.setDoc(map);
                  EsBulkRequest.add(updateBuilder);

  //保存
    if (EsBulkRequest.numberOfActions()!= 0){
           bulkWriteOperation.execute();
           EsBulkRequest.execute();
    }