use of com.destiny.wolf.entity.BookIndex in project springboot-templet-start by thedestiny.
the class BookIndexServiceTest method test001.
@Test
public void test001() {
List<BookIndex> indexList = new ArrayList<>();
Snowflake flake = IdUtil.createSnowflake(1, 1);
List<String> tags = Lists.newArrayList("科幻", "悬疑", "探案", "小说", "散文", "科普", "报告", "绘画", "社科", "地理", "教育", "励志", "战争", "人文");
for (int i = 0; i < 30; i++) {
Faker instance = Faker.instance(Locale.CHINA);
Book bk = instance.book();
BookIndex index = new BookIndex();
index.setId(flake.nextIdStr());
index.setName(bk.title());
index.setAddress(instance.address().fullAddress());
index.setDescribe(instance.company().catchPhrase());
index.setPublishDate(instance.date().between(new Date(100, 1, 1), new Date(120, 1, 1)));
index.setPrice(RandomUtil.randomBigDecimal(BigDecimal.valueOf(1), BigDecimal.valueOf(2000)).setScale(2, BigDecimal.ROUND_HALF_UP));
index.setNumber(RandomUtil.randomInt(100, 30000));
index.setAuthor(bk.author());
List<String> list = Lists.newArrayList();
int num = RandomUtil.randomInt(1, 8);
for (int j = 0; j < num; j++) {
int cnt = RandomUtil.randomInt(0, 13);
list.add(tags.get(cnt));
}
list = list.stream().distinct().collect(Collectors.toList());
index.setTags(list);
indexList.add(index);
log.info(" book is {}", JSONObject.toJSONString(index));
}
// bookIndexService.insertBach(indexList);
}
use of com.destiny.wolf.entity.BookIndex in project springboot-templet-start by thedestiny.
the class BookIndexServiceTest method test003.
@Test
public void test003() {
/**
* 排序字段
* {
* "sort": [
* {
* "FIELD": {
* "order": "desc"
* }
* }
* ]
* }
*/
SortBuilder sortBuilder = // 排序字段
SortBuilders.fieldSort("_id").order(SortOrder.ASC);
// 显示和不显示的字段
FetchSourceFilter sourceFilter = new FetchSourceFilter(null, null);
/**
* "math":{
* "FIELD": "TEXT" //字段:值
* }
*/
NativeSearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchQuery("name", "sky")).withPageable(PageRequest.of(1, 1)).withHighlightFields(new HighlightBuilder.Field("sky")).withSourceFilter(sourceFilter).withSort(sortBuilder).build();
log.info("query is {}", searchQuery.toString());
QueryBuilder queryBuilder = QueryBuilders.matchAllQuery();
PageRequest page = PageRequest.of(2, 10);
Iterable<BookIndex> search = repository.search(queryBuilder, page);
SearchHits<BookIndex> search1 = template.search(searchQuery, BookIndex.class);
log.info("response {}", search);
log.info("response {}", search1);
// Bool查询现在包括四种子句:must,filter,should,must_not
// filter 比query 快 1 避免算分 2 会缓存结果
// withFilter withQuery
NativeSearchQuery searchQuery1 = new NativeSearchQueryBuilder().withFilter(boolQuery().should(matchQuery("price", 170))).build();
// 关键字查询,不分词
QueryBuilders.termQuery("name", "小明");
// .addAggregation(
AggregationBuilders.terms("all_tags").field("tags");
// 支持 collection
QueryBuilders.termsQuery("name", "小明", "小五");
// text 查询 分词
QueryBuilders.matchQuery("title", "平凡的世界");
// text 查询 分词
QueryBuilders.multiMatchQuery("title", "平凡的世界", "路遥").slop(0);
// 模糊查询 动态将关键词前后增加或者删除一个词进行匹配 模糊查询
QueryBuilders.fuzzyQuery("title", "开发开放").fuzziness(Fuzziness.ONE).prefixLength(// 前缀查询的长度
3).maxExpansions(// max expansion 选项,用来控制模糊查询
10);
// 前缀匹配
QueryBuilders.prefixQuery("title", "开发开放");
// * 是多个 ? 单个字符
QueryBuilders.wildcardQuery("title", "开*放");
QueryBuilders.wildcardQuery("title", "开?放");
// 短语匹配
QueryBuilders.matchPhraseQuery("title", "开放");
// fuzzyQuery、prefixQuery、wildcardQuery 不支持分词查询
// 闭区间查询
QueryBuilders.rangeQuery("fieldName").from("fieldValue1").to("fieldValue2");
// 开区间查询,默认是true,也就是包含
QueryBuilders.rangeQuery("fieldName").from("fieldValue1").to("fieldValue2").includeUpper(false).includeLower(false);
// 大于 gt lt gte lte
QueryBuilders.rangeQuery("fieldName").gt("fieldValue");
// 文档必须完全匹配条件,相当于and
QueryBuilders.boolQuery().must();
// 文档必须不匹配条件,相当于not
QueryBuilders.boolQuery().mustNot();
// 至少满足一个条件,这个文档就符合should,相当于or
QueryBuilders.boolQuery().should();
}
use of com.destiny.wolf.entity.BookIndex in project springboot-templet-start by thedestiny.
the class BookIndexServiceImpl method searchList.
@Override
public List<BookIndex> searchList() {
SearchResponse searchResponse = search(index);
SearchHit[] hits = searchResponse.getHits().getHits();
List<BookIndex> lolList = new ArrayList<>();
Arrays.stream(hits).forEach(hit -> {
Map<String, Object> sourceAsMap = hit.getSourceAsMap();
BookIndex lol = BeanUtil.mapToBean(sourceAsMap, BookIndex.class, true);
// 转换查询对象
BookIndex index = JSONObject.parseObject(hit.getSourceAsString(), BookIndex.class);
Map<String, String> map = new HashMap<>();
lolList.add(lol);
});
return lolList;
}
Aggregations