Search in sources :

Example 1 with BookIndex

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);
}
Also used : BookIndex(com.destiny.wolf.entity.BookIndex) Faker(com.github.javafaker.Faker) Book(com.github.javafaker.Book) Snowflake(cn.hutool.core.lang.Snowflake) ArrayList(java.util.ArrayList) Date(java.util.Date) Test(org.junit.Test)

Example 2 with BookIndex

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();
}
Also used : BookIndex(com.destiny.wolf.entity.BookIndex) SortBuilder(org.elasticsearch.search.sort.SortBuilder) PageRequest(org.springframework.data.domain.PageRequest) FetchSourceFilter(org.springframework.data.elasticsearch.core.query.FetchSourceFilter) RangeQueryBuilder(org.elasticsearch.index.query.RangeQueryBuilder) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) NativeSearchQueryBuilder(org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder) NativeSearchQueryBuilder(org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder) NativeSearchQuery(org.springframework.data.elasticsearch.core.query.NativeSearchQuery) Test(org.junit.Test)

Example 3 with BookIndex

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;
}
Also used : BookIndex(com.destiny.wolf.entity.BookIndex) SearchHit(org.elasticsearch.search.SearchHit) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) JSONObject(com.alibaba.fastjson.JSONObject) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Aggregations

BookIndex (com.destiny.wolf.entity.BookIndex)3 ArrayList (java.util.ArrayList)2 Test (org.junit.Test)2 Snowflake (cn.hutool.core.lang.Snowflake)1 JSONObject (com.alibaba.fastjson.JSONObject)1 Book (com.github.javafaker.Book)1 Faker (com.github.javafaker.Faker)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 SearchResponse (org.elasticsearch.action.search.SearchResponse)1 QueryBuilder (org.elasticsearch.index.query.QueryBuilder)1 RangeQueryBuilder (org.elasticsearch.index.query.RangeQueryBuilder)1 SearchHit (org.elasticsearch.search.SearchHit)1 SortBuilder (org.elasticsearch.search.sort.SortBuilder)1 PageRequest (org.springframework.data.domain.PageRequest)1 FetchSourceFilter (org.springframework.data.elasticsearch.core.query.FetchSourceFilter)1 NativeSearchQuery (org.springframework.data.elasticsearch.core.query.NativeSearchQuery)1 NativeSearchQueryBuilder (org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder)1