elasticsearch 提供的API比较多,看名字差不多可以明白什么意思
源码地址:源码下载点击
public void search01() { QueryBuilder query = QueryBuilders.queryString("9"); // 设置搜索的内容和现实的大小 SearchResponse res = client.prepareSearch("test").setQuery(query) .setFrom(0).setSize(60).execute().actionGet(); SearchHits shs = res.getHits(); System.out.println("总共有数据:" + shs.getHits().length); for (SearchHit it : shs) { System.out.println(it.getSource()); } } // 分组fact类似于sql的group public void search02() { // 定义一个分组并为其命名 TermsFacetBuilder facetBuilder = FacetBuilders.termsFacet("groupBysex"); // 按照哪个字段进行分组 facetBuilder.field("sex").size(1); // 过滤器匹配所有文件 facetBuilder.facetFilter(FilterBuilders.matchAllFilter()); SearchResponse res = client.prepareSearch("test2").setTypes("test02") .addFacet(facetBuilder).execute().actionGet(); // 获取搜索结果中的分组 Facets facet = res.getFacets(); // 多个分组是以map的形式存储 TermsFacet fac = (TermsFacet) facet.getFacets().get("groupBysex"); for (TermsFacet.Entry tf : fac.entries()) { System.out.println(tf.getCount() + "======" + tf.getTerm()); } } // QUERY public void search03() { // 按照字段进行索引,只要内容含有即可不用全部符合 QueryBuilder queryBuilder = QueryBuilders.fieldQuery("name", "李四1后面有内容也可以"); //整个 数据内容中只要 有 这个字即可 QueryBuilder qb =QueryBuilders.queryString("四"); //组合查询 name为 张三,sex为boy的记录 must表示必须有,mustNot表示不包含,should表示可以存在 QueryBuilder queryBuilder2 = QueryBuilders.fieldQuery("name", "张三"); QueryBuilder queryBuilder3 = QueryBuilders.fieldQuery("sex", "boy"); QueryBuilder qbmust = QueryBuilders.boolQuery().must(queryBuilder2).should(queryBuilder3); //过滤查询 QueryBuilder qb3 = QueryBuilders.filteredQuery( queryBuilder2, FilterBuilders.prefixFilter("sex", "bo") ); SearchResponse res = client.prepareSearch("test").setTypes("test01") .setSearchType(SearchType.DEFAULT) .setQuery(qb3).execute().actionGet(); SearchHits shs = res.getHits(); System.out.println("共查到数据:" + shs.getHits().length); for (SearchHit it : shs) { System.out.println(it.getSource()); } } // 高亮显示 public void search04() { //前缀过滤器 汉字不好用 FilterBuilder fb = FilterBuilders.prefixFilter("sex", "bo"); //范围过滤 FilterBuilder fb2 = FilterBuilders.rangeFilter("id").from(1).to(10); //组合过滤 FilterBuilder fb3 = FilterBuilders.andFilter( FilterBuilders.rangeFilter("id").from(1).to(10), FilterBuilders.prefixFilter("sex", "bo") ).cache(true);//默认false SearchResponse res = client.prepareSearch("test").setTypes("test01") .setSearchType(SearchType.DFS_QUERY_THEN_FETCH) .setFilter(fb3) .execute().actionGet(); SearchHits shs = res.hits(); System.out.println("总共有数据:" + shs.getHits().length); for (SearchHit it : shs) { System.out.println(it.getSource()); } } // 高亮显示 public void search05() { QueryBuilder qb1 = QueryBuilders.termQuery("name", "张三_4"); // FilterBuilder filter =FilterBuilders.prefixFilter("name", "张三"); SearchResponse res = client.prepareSearch("test4").setTypes("test04") .setSearchType(SearchType.DFS_QUERY_THEN_FETCH) .setQuery(QueryBuilders.fieldQuery("id", "1")) .addHighlightedField("id").setHighlighterPreTags("<spand>") .setHighlighterPostTags("</bod>").execute().actionGet(); SearchHits shs = res.hits(); System.out.println("总共有数据:" + shs.getHits().length); for (SearchHit it : shs) { System.out.println(it.getSource()); // 获取对应的高亮域 Map<String, HighlightField> result = it.highlightFields(); // 从设定的高亮域中取得指定域 HighlightField titleField = result.get("id"); // 取得定义的高亮标签 String[] titleTexts = titleField.fragments(); // 为title串值增加自定义的高亮标签 String title = ""; for (String text : titleTexts) { title += text; } // 将追加了高亮标签的串值重新填充到对应的对象 // product.setTitle(title); // 打印高亮标签追加完成后的实体对象 System.out.println(title); } }
时间: 2024-10-23 09:45:35