elasticsearch JAVA API

elasticsearch  版本 6.3.0

Java API [6.4] 

 

##### 获取全部索引

    import org.elasticsearch.client.Client;
    import org.elasticsearch.client.transport.TransportClient;
    import org.elasticsearch.common.settings.ImmutableSettings;
    import org.elasticsearch.common.settings.Settings;
    import org.elasticsearch.common.transport.InetSocketTransportAddress;
    public class SampleProgram{
     public static void main(String[] args) {
      String hostname = "localhost";
      String clusterName = "elasticsearch";
      Settings settings = ImmutableSettings.settingsBuilder().put("client.transport.sniff", true).put("cluster.name", clusterName).build();
      Client esclient = new TransportClient(settings).addTransportAddress(new InetSocketTransportAddress(hostname, 9300));
      String[] indexList = esclient.admin().cluster().prepareState().execute().actionGet().getState().getMetaData().concreteAllIndices();
          System.out.println("Index List:");
          for (String index : indexList) {
           System.out.println(index);
      }
     }
    }

#### 根据索引获取全部type

    private static List<String> getAllTypeByIndex(TransportClient esClient,String indexSTR) {
    List<String> typeList = new ArrayList<String>();
    try {
        GetMappingsResponse res = esClient.admin().indices().getMappings(new GetMappingsRequest().indices(indexSTR)).get();
        ImmutableOpenMap<String, MappingMetaData> mapping = res.mappings().get(indexSTR);
    for (ObjectObjectCursor<String, MappingMetaData> c : mapping) {
        typeList.add(c.key);
    }
    } catch (InterruptedException e) {
        e.printStackTrace();
    } catch (ExecutionException e) {
          e.printStackTrace();
    }
        return typeList;
    }


#### 根据索引和类型获取全部字段

     public ResponseData getIndexPropertiesByName(RequestData requestData) {
        ResponseData responseData = new ResponseData();
        List<Map<String,Object>> list = new ArrayList<Map<String, Object>>();

        String indexSTR = requestData.getParam().get("index").toString(); // 获取索引字段
        List<String> tempTypeList = getAllTypeByIndex(esClient,indexSTR);
        String tempType =  tempTypeList.get(0);

        List<String> fieldList = new ArrayList<String>();
        ClusterState cs = esClient.admin().cluster().prepareState().setIndices(indexSTR).execute().actionGet().getState();
        IndexMetaData imd = cs.getMetaData().index(indexSTR);
        MappingMetaData mdd = imd.mapping(tempType);
        Map<String, Object> map = null;
        map = mdd.getSourceAsMap();
        fieldList = getList("", map);
        for (String field : fieldList) {
            Map fieldMap=new HashMap();
            fieldMap.put("property",field);
            list.add(fieldMap);
        }
        responseData.setData(list);
        return responseData;
    }
    private static List<String> getList(String fieldName, Map<String, Object> mapProperties) {
            List<String> fieldList = new ArrayList<String>();
            Map<String, Object> map = (Map<String, Object>) mapProperties.get("properties");
            Set<String> keys = map.keySet();
            for (String key : keys) {
                if (((Map<String, Object>) map.get(key)).containsKey("type")) {
                    fieldList.add(fieldName + "" + key);
                } else {
                    List<String> tempList = getList(fieldName + "" + key + ".", (Map<String, Object>) map.get(key));
                    fieldList.addAll(tempList);
                }
            }
            return fieldList;
        }


#### 根据条件和过滤条件查询,并且高亮过滤条件字段

    // 设置高亮
    HighlightBuilder hiBuilder = new HighlightBuilder().field("*");  // "*" 代表全部过滤字段
    hiBuilder.preTags("<eam>");  // 这里可以改标签
    hiBuilder.postTags("</eam>");
    
    // bool 层级关系  看图1
    BoolQueryBuilder  boolQueryBuilder = QueryBuilders.boolQuery();
    boolQueryBuilder.must(QueryBuilders.queryStringQuery("张三")); // 查询条件
    List<QueryBuilder> filter = boolQueryBuilder.filter();
    
    
    filter.add( QueryBuilders.rangeQuery("age",gt(20)) )  // 年龄大于20
    filter.add( QueryBuilders.rangeQuery("monthlyPay",gt(20)) )// 月资大于 2000
    
    SearchResponse response = esClient.prepareSearch(index)  // 索引
                                      .setTypes(type)  // type
                                      .setQuery(boolQueryBuilder)  // 过滤条件
                                      .setFrom(0).setSize(20)  // 0-20 条数据
                                      .highlighter(hiBuilder)  // 高亮
                                      .execute().actionGet();
    
    SearchHits hits = response.getHits();
    for(int i = 0; i < hits.getHits().length; i++) {
      System.out.println(hits.getHits()[i].getSourceAsString());
    }


    
    // 意思是查询,全部张三有关的,并且年龄大于20和月薪大于2000的


#### 图1


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值