1 创建 client 客户端
private final static String host = "127.0.0.1"; //创建客户端 public static Client getClient() { try { Client client = TransportClient.builder().build() .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(host), 9300)); return client; } catch (Exception ex) { System.out.println(ex); } return null; }
2.根据index,type查询数据 ,由于Elasticsearch是天生异步的,执行execute后会在等到查询结果之前就返回caller block,所以我们可以简单的使用actionGet()方法来阻塞程序直到查询结构返回。
public static void forGetResponse(String getIndex, String getType, String getId) {//GetResponse getResponse = getClient().prepareGet(getIndex, getType, getId).execute().actionGet();GetResponse getResponse = getClient().prepareGet(getIndex, getType, getId).get(); Map<String, Object> responMap = getResponse.getSourceAsMap();
//转换成json的格式 System.out.println("getResponse返回结果为:" + Json2Str(responMap)); }
3. 插入一条数据到ES,由于设置了主键,如果id存在就做更新的操作,不存在就插入类似mysql的upsert操作,另外在用spark对ES的写入的时候因为有native的接口 直接Data Frame进行写入,所以此upsert是可以设置的如:conf.set("es.write.operation","upsert")
Client cli = getClient(); IndexResponse response = cli.prepareIndex("account", "stu", String.valueOf(students.getSid()))//参数说明: 索引,类型 ,_id) .setSource(Json2Str(students))//setSource可以传以上map string byte[] 几种方式 .get(); boolean created = response.isCreated();
4.
这里自定义了某个Type的索引映射(Mapping),默认ES会自动处理数据类型的映射:针对整型映射为long,浮点数为double,字符串映射为string,时间为date,true或false为boolean。
注意:针对字符串,ES默认会做“analyzed”处理,即先做分词、去掉stop words等处理再index。如果你需要把一个字符串做为整体被索引到,需要把这个字段这样设置:field("index", "not_analyzed")。 例如:中华人民共和国
//搜索 select name from table where name=zhangsan and age >12 and age <18
SearchResponse searchResponse = cli.prepareSearch("account")//可以同时搜索多个索引prepareSearch("index","index2") .setTypes("stu", "info")//可以同时搜索多个类型 .setSearchType(SearchType.DFS_QUERY_THEN_FETCH) .setQuery(QueryBuilders.termQuery("sex", "male")) // Query .setPostFilter(QueryBuilders.rangeQuery("age").from(10).to(20)) // Filter .setFrom(0) .setSize(10) .setExplain(true) .execute() .actionGet();封装方法:查询response返回的结果集5.简单的聚合操作public static void forSearchResponse(SearchResponse response) { //SearchHit类代表了一个满足查询条件的document,获得SearchHit后可以通过便利来输出每一个hit的信息 for (SearchHit searchHit : response.getHits()) { //获取高亮显示的数据,含有富文本 /* Map<String, HighlightField> highlightResult = searchHit.getHighlightFields(); HighlightField highlightField = highlightResult.get("name"); Text[] titleText = highlightField.fragments(); String higtName = ""; for (Text text : titleText) { higtName += text; }*/ Map<String, Object> source = searchHit.getSource(); if (!source.isEmpty()) { Iterator<Map.Entry<String, Object>> it = source.entrySet().iterator(); while (it.hasNext()) { Map.Entry<String, Object> entry = it.next(); System.out.println("hists结果为:" + entry.getKey() + "----" + entry.getValue()); } } } }
public static void forAggreationSearch() { SearchRequestBuilder srb = getClient().prepareSearch("account"); srb.setTypes("stu"); TermsBuilder ageTermsBuilder = AggregationBuilders.terms("gradeAge").field("age"); TermsBuilder sexTermsBuilder = AggregationBuilders.terms("gradeSex").field("sex"); sexTermsBuilder.subAggregation(ageTermsBuilder); srb.addAggregation(sexTermsBuilder); SearchResponse response = srb.execute().actionGet(); Map<String, Aggregation> aggMap = response.getAggregations().asMap(); StringTerms sexTerms = (StringTerms) aggMap.get("gradeSex"); Iterator<Bucket> sexBuckets = sexTerms.getBuckets().iterator(); while (sexBuckets.hasNext()) { Bucket sexBucket = sexBuckets.next(); System.out.println("性别:" + sexBucket.getKey() + " 人数为:" + sexBucket.getDocCount()); StringTerms ageTerms = (StringTerms) sexBucket.getAggregations().asMap().get("gradeAge"); Iterator<Bucket> ageBuckets = ageTerms.getBuckets().iterator(); while (ageBuckets.hasNext()) { Bucket ageBucket = ageBuckets.next(); System.out.println("性别:" + sexBucket.getKey() + " " + ageBucket.getKey() + "岁" + ",人数为:" + ageBucket.getDocCount()); } } }