1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
| @SpringBootTest class ElasticsearchDemoApplicationTests {
@Autowired private ElasticsearchRestTemplate elasticsearchTemplate;
@Autowired private GoodsMapper goodsMapper;
@Test void contextLoads() { elasticsearchTemplate.createIndex(Goods.class); }
@Test void contextLoads2() { Goods goods = Goods.builder().id(1001L).name("macBook pro").desc("苹果电脑").prise(100.2).count(998).version(1L).build(); Goods save = goodsMapper.save(goods); System.out.println(save); }
@Test void tset3() { goodsMapper.deleteById("1002"); }
@Test void test4() { PageRequest pageRequest = PageRequest.of(0, 20, Sort.Direction.ASC, "id"); Page<Goods> goodsPage = goodsMapper.findAll(pageRequest); Iterator<Goods> iterator = goodsPage.iterator(); while (iterator.hasNext()) { System.out.println(iterator.next()); } }
@Test void test5() { goodsMapper.existsById("1001"); }
@Test void test6() { TermsQueryBuilder termsQueryBuilder = new TermsQueryBuilder("count", "998"); Iterable<Goods> goodsPage = goodsMapper.search(termsQueryBuilder); Iterator<Goods> iterator = goodsPage.iterator(); while (iterator.hasNext()) { System.out.println(iterator.next()); } }
@Test void testPage() { SearchQuery searchQuery = new NativeSearchQueryBuilder() .withQuery(matchAllQuery()) .withIndices("es_rest_goods_test") .withPageable(PageRequest.of(0, 10)) .build();
ScrolledPage<Goods> scroll = elasticsearchTemplate.startScroll(1000, searchQuery, Goods.class); String scrollId = scroll.getScrollId(); List<Goods> content = scroll.getContent(); content.forEach(item -> { System.out.println(item); }); System.out.println(scrollId); ScrolledPage<Goods> scroll1 = elasticsearchTemplate.continueScroll(scrollId, 1000, Goods.class); String scrollId1 = scroll1.getScrollId(); List<Goods> content1 = scroll1.getContent(); content1.forEach(item -> { System.out.println(item); }); System.out.println(scrollId1); elasticsearchTemplate.clearScroll(scrollId);
}
@Test void test7() { AvgAggregationBuilder builder = AggregationBuilders.avg("avg_name").field("prise"); NativeSearchQuery query = new NativeSearchQueryBuilder().addAggregation(builder).withIndices("es_rest_goods_test").build(); Aggregations aggregations = elasticsearchTemplate.query(query, response -> response.getAggregations()); aggregations.asMap().forEach((key, value) -> { ParsedAvg avg = (ParsedAvg) value; System.out.println(key + " " + avg.getName() + " " + avg.getValue() + " " + avg.value()); });
}
@Test void test8() { CardinalityAggregationBuilder builder = AggregationBuilders.cardinality("prise_cardinality").field("prise"); NativeSearchQuery query = new NativeSearchQueryBuilder().addAggregation(builder).withIndices("es_rest_goods_test").build(); Aggregations aggregations = elasticsearchTemplate.query(query, response -> response.getAggregations()); aggregations.asMap().forEach((key, value) -> { System.out.println(key); ParsedCardinality parsedCardinality = (ParsedCardinality)value; System.out.println(parsedCardinality.getValue());
}); } @Test void test9() { RangeAggregationBuilder rangeAggregationBuilder = AggregationBuilders.range("price_range").field("count") .addRange(920,930).addRange(930,940).addRange(940,950); NativeSearchQuery query = new NativeSearchQueryBuilder().addAggregation(rangeAggregationBuilder).withIndices("es_rest_goods_test").build(); Aggregations aggregations = elasticsearchTemplate.query(query, response -> response.getAggregations()); aggregations.asMap().forEach((key, value) -> { System.out.println(key); ParsedRange range = (ParsedRange) value;
range.getBuckets().forEach((bucket)->{ System.out.println(bucket.getKeyAsString()+" "+bucket.getDocCount()); }); }); }
@Test void test10() { TermsAggregationBuilder termsAggregationBuilder = AggregationBuilders.terms("price_name").field("count"); TopHitsAggregationBuilder topHitsAggregationBuilder = AggregationBuilders.topHits("top").size(3); termsAggregationBuilder.subAggregation(topHitsAggregationBuilder); NativeSearchQuery query = new NativeSearchQueryBuilder().addAggregation(termsAggregationBuilder).withIndices("es_rest_goods_test").build(); Aggregations aggregations = elasticsearchTemplate.query(query, response -> response.getAggregations()); aggregations.asMap().forEach((key, value) -> { System.out.println(key); ParsedLongTerms parsedLongTerms = (ParsedLongTerms)value; parsedLongTerms.getBuckets().forEach((bucket)->{ Aggregation aggregation = bucket.getAggregations().getAsMap().get("top"); TopHits topHits= (TopHits) aggregation; Iterator<SearchHit> iterator = topHits.getHits().iterator(); while (iterator.hasNext()){ SearchHit next = iterator.next(); Object object= JSONObject.parse(next.getSourceAsString()); System.out.println(object); } }); }); }
}
|