您的位置:首页 > 财经 > 金融 > 地推平台招代理_手表网站背景素材_百度网页推广费用_什么样的人适合做策划

地推平台招代理_手表网站背景素材_百度网页推广费用_什么样的人适合做策划

2024/12/23 10:18:04 来源:https://blog.csdn.net/m0_46695127/article/details/143031182  浏览:    关键词:地推平台招代理_手表网站背景素材_百度网页推广费用_什么样的人适合做策划
地推平台招代理_手表网站背景素材_百度网页推广费用_什么样的人适合做策划

文章目录

    • 查询
    • 聚合查询
      • RestHighLevelClient
      • ElasticsearchRestTemplat

SpringData对ES客户端的封装:ElasticsearchRestTemplate
SpringData对CRUD的封装:ElasticsearchRepository
原生ES客户端:RestHighLevelClient

查询

package com.xd.cubemall.sdes;import com.xd.cubemall.search.CubemallSearchApplication;
import com.xd.cubemall.search.model.Blog;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.SearchHit;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.test.context.junit4.SpringRunner;import java.util.List;
import java.util.Map;@RunWith(SpringRunner.class)
@SpringBootTest(classes = CubemallSearchApplication.class)
public class QueryTest {@Autowiredprivate ElasticsearchRestTemplate template;@Testpublic void testQuery1() {NativeSearchQuery query = new NativeSearchQueryBuilder().withQuery(QueryBuilders.matchAllQuery()).build();SearchHits<Blog> searchHits = template.search(query, Blog.class);//取总记录数long totalHits = searchHits.getTotalHits();System.out.println("总记录数:" + totalHits);//取文档列表List<SearchHit<Blog>> hits = searchHits.getSearchHits();hits.stream().forEach(h-> System.out.println(h));}@Testpublic void testQuery2() throws Exception {NativeSearchQuery query = new NativeSearchQueryBuilder()//查询设置.withQuery(QueryBuilders.multiMatchQuery("看电影","title","content"))//过滤条件,可以设置多个,支持查询条件.withFilter(QueryBuilders.boolQuery().should(QueryBuilders.termQuery("title", "电影")).should(QueryBuilders.termQuery("content","祝福")))
//                .withFilter(QueryBuilders.termQuery("mobile", "13000000000"))//分页设置.withPageable(PageRequest.of(0, 10))//高亮设置.withHighlightBuilder(new HighlightBuilder()//高亮显示的字段.field("title").field("content")//高亮显示的前缀.preTags("<em>")//高亮显示的后缀.postTags("</em>")).build();//使用template对象执行查询SearchHits<Blog> searchHits = template.search(query, Blog.class);//取返回结果//总记录数long totalHits = searchHits.getTotalHits();System.out.println("总记录数:" + totalHits);//取文档列表List<SearchHit<Blog>> hits = searchHits.getSearchHits();hits.stream().forEach(hit->{//取文档对象Blog blog = hit.getContent();//System.out.println(blog);//取高亮结果Map<String,List<String>> highlightFields = hit.getHighlightFields();//System.out.println(highlightFields);String title = highlightFields.get("title").get(0);String content = highlightFields.get("content").get(0);blog.setTitle(title);blog.setContent(content);System.out.println(blog);});}
}

聚合查询

api使用方法
搜索时,请求体中包含聚合条件
在aggs属性中设置聚合条件:
value_count
max
min
avg

GET blog_1/_search
{"query": {"match_all": {}},"aggs": {"mobile_count": {"value_count": {"field": "mobile"}},"mobile_max":{"max": {"field": "mobile"}}}
}

分桶(分组)聚合

GET blog_1/_search
{"query": {"match_all": {}},"aggs": {"mobile_count": {"value_count": {"field": "mobile"}},"mobile_group":{"terms": {"field": "mobile","size": 10}}}
}

RestHighLevelClient

@Autowiredprivate RestHighLevelClient restHighLevelClient;@Testpublic void testAggs() throws IOException {SearchRequest request = new SearchRequest().indices("blog_1").source(new SearchSourceBuilder().query(QueryBuilders.matchAllQuery()).aggregation(new ValueCountAggregationBuilder("doc_count").field("mobile")).aggregation(new TermsAggregationBuilder("group_count").field("mobile").size(10)));SearchResponse response = restHighLevelClient.search(request, RequestOptions.DEFAULT);System.out.println(response);}

ElasticsearchRestTemplat

    @Testpublic void testQuery2() throws Exception {NativeSearchQuery query = new NativeSearchQueryBuilder()//查询设置.withQuery(QueryBuilders.multiMatchQuery("看电影","title","content"))//过滤条件,可以设置多个,支持查询条件.withFilter(QueryBuilders.boolQuery().should(QueryBuilders.termQuery("title", "电影")).should(QueryBuilders.termQuery("content","祝福")))
//                .withFilter(QueryBuilders.termQuery("mobile", "13000000000"))//分页设置.withPageable(PageRequest.of(0, 10))//高亮设置.withHighlightBuilder(new HighlightBuilder()//高亮显示的字段.field("title").field("content")//高亮显示的前缀.preTags("<em>")//高亮显示的后缀.postTags("</em>")).addAggregation(new TermsAggregationBuilder("mobile_group").field("mobile")).build();//使用template对象执行查询SearchHits<Blog> searchHits = template.search(query, Blog.class);//取返回结果//总记录数long totalHits = searchHits.getTotalHits();System.out.println("总记录数:" + totalHits);//取文档列表List<SearchHit<Blog>> hits = searchHits.getSearchHits();hits.stream().forEach(hit->{//取文档对象Blog blog = hit.getContent();//System.out.println(blog);//取高亮结果Map<String,List<String>> highlightFields = hit.getHighlightFields();//System.out.println(highlightFields);String title = highlightFields.get("title").get(0);String content = highlightFields.get("content").get(0);blog.setTitle(title);blog.setContent(content);System.out.println(blog);});Aggregations aggregations = searchHits.getAggregations();ParsedStringTerms aggregation = aggregations.get("mobile_group");List<? extends Terms.Bucket> buckets = aggregation.getBuckets();buckets.forEach(e-> {System.out.println(e.getKeyAsString());System.out.println(e.getDocCount());});}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com