IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 大数据 -> Elasticsearch RestHighLevelClient操作 -> 正文阅读

[大数据]Elasticsearch RestHighLevelClient操作

RestHighLevelClient操作

       <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>elasticsearch-rest-high-level-client</artifactId>
            <version>7.15.2</version>
        </dependency>
        <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>elasticsearch-rest-client</artifactId>
            <version>7.15.2</version>
        </dependency>

import org.apache.http.Header;
import org.apache.http.HttpHost;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.message.BasicHeader;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

@Configuration
public class RestClientConfig  {

    @Value("${es.iphosts:localhost:9200}")
    private String ipHosts;
    @Value("${es.password:null}")
    private String password;
    @Value("${es.username:null}")
    private String username;

    @Bean(destroyMethod = "close")
    public RestHighLevelClient restHighLevelClient() {
        List<HttpHost> hosts = new ArrayList<>();
        String[] iphostArray = ipHosts.split(";");
        for (int i = 0; i < iphostArray.length; i++) {
            if (StringUtils.isEmpty(iphostArray[i])) {
                continue;
            }
            hosts.add(new HttpHost(iphostArray[i].split(":")[0],
                    Integer.valueOf(iphostArray[i].split(":")[1]),"http"));
        }
        Header[] headers = new BasicHeader[2];
        headers[0] = new BasicHeader("username",username);
        headers[1] = new BasicHeader("password",password);

        RestClientBuilder builder = RestClient.builder(hosts.stream().toArray(HttpHost[]::new))
                .setDefaultHeaders(headers)
                .setRequestConfigCallback(new RestClientBuilder.RequestConfigCallback() {
                    @Override
                    public RequestConfig.Builder customizeRequestConfig(RequestConfig.Builder builder) {
                        return builder.setConnectTimeout(5000 * 1000)
                                /** 套接字超时(默认为30秒)//更改客户端的超时限制默认30秒现在改为100*1000分钟 */
                                .setSocketTimeout(6000 * 1000); //
                    }
                });
        RestHighLevelClient client =  new RestHighLevelClient(builder);
        return client;
    }
}


import com.alibaba.fastjson.JSONObject;
import com.example.demo.es.entity.Product;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.unit.Fuzziness;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.*;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.aggregations.AggregationBuilder;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.Aggregations;
import org.elasticsearch.search.aggregations.bucket.terms.ParsedDoubleTerms;
import org.elasticsearch.search.aggregations.bucket.terms.Terms;
import org.elasticsearch.search.aggregations.metrics.ParsedSum;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightField;
import org.elasticsearch.search.sort.SortOrder;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.IOException;
import java.util.List;
import java.util.Map;


@SpringBootTest
public class EsTest {
    @Autowired
    private  RestHighLevelClient client;

//    @Autowired
//    public EsTest(RestHighLevelClient restHighLevelClient){
//        this.client=restHighLevelClient;
//    }

    /**
     *  查看官网的api
     *  https://www.elastic.co/guide/en/elasticsearch/client/java-rest/7.17/java-rest-high-create-index.html
     * @throws IOException
     */

    /**
     * 创建索引,使用ik分词器
     * @throws IOException
     */
   @Test
    public void createIndex() throws IOException {
        CreateIndexRequest request = new CreateIndexRequest("products");
        request.settings(Settings.builder()
                .put("index.number_of_shards", 3)
                .put("index.number_of_replicas", 2)
        );
        request.mapping(
                "{\n" +
                        "        \"properties\": {\n" +
                        "            \"id\": {\n" +
                        "                \"type\": \"integer\"\n" +
                        "            },\n" +
                        "            \"title\": {\n" +
                        "                \"type\": \"keyword\"\n" +
                        "            },\n" +
                        "            \"price\": {\n" +
                        "                \"type\": \"double\"\n" +
                        "            },\n" +
                        "            \"created_at\": {\n" +
                        "                \"type\": \"date\"\n" +
                        "            },\n" +
                        "            \"description\": {\n" +
                        "                \"type\": \"text\",\n" +
                        "                \"analyzer\":\"ik_max_word\"\n" +
                        "            },\n" +
                        "             \"brand\": {\n" +
                        "                \"type\": \"keyword\"\n" +
                        "            }\n" +
                        "        }\n" +
                        "    }",
                XContentType.JSON);
        CreateIndexResponse createIndexResponse = client.indices().create(request, RequestOptions.DEFAULT);
        System.out.println(createIndexResponse.isAcknowledged());

    }

    /**
     * 插入文档
     * @throws IOException
     */
    @Test
    public void insertDoc() throws IOException {
        IndexRequest request=new IndexRequest("products");

        Product product=new Product();
        product.setId(1);
        product.setTitle("小米手机");
        product.setPrice(1999.9d);
        product.setDescription("小米手机正好");
        request.id(product.getId().toString());
        request.source(JSONObject.toJSONString(product), XContentType.JSON);
        IndexResponse indexResponse = client.index(request, RequestOptions.DEFAULT);
        System.out.println(indexResponse.getResult());

    }

    /**
     * 根据Id文档
     * @throws IOException
     */
    @Test
    public void updateDocById() throws IOException {

        UpdateRequest request=new UpdateRequest();
        request.index("products").id("1");
        Product product=new Product();
        product.setDescription("小米发烧为生");
        request.doc(JSONObject.toJSONString(product), XContentType.JSON);
        UpdateResponse  updateResponse = client.update(request, RequestOptions.DEFAULT);
        System.out.println(updateResponse.getResult());

    }

    /**
     * 根据Id删除文档
     * @throws IOException
     */
    @Test
    public void delDocById() throws IOException {
        //3.删除数据
        DeleteRequest deleteRequest=new DeleteRequest();
        deleteRequest.index("products").id("1");
        DeleteResponse deleteResponse = client.delete(deleteRequest, RequestOptions.DEFAULT);
        System.out.println(deleteResponse.getResult());
    }

    /**
     * 根据Id查询文档
     * @throws IOException
     */
    @Test
    public void getDocById() throws IOException {
        GetRequest getRequest=new GetRequest();
        getRequest.index("products").id("1");
        GetResponse getResponse = client.get(getRequest, RequestOptions.DEFAULT);
        System.out.println("查出的数据为:"+ getResponse.getSourceAsString());
    }



    /**
     * 查询所有文档
     * @throws IOException
     */
    @Test
    public void matchAllTest() throws IOException {
        QueryBuilder queryBuilder = QueryBuilders.matchAllQuery();
        query(queryBuilder);
    }

    public void query(QueryBuilder queryBuilder )throws IOException{
        SearchRequest searchRequest=new SearchRequest();
        searchRequest.indices("products");
        searchRequest.source(new SearchSourceBuilder().query(queryBuilder));
        System.out.println("查询DSL为:"+searchRequest.source().toString());
        SearchResponse searchRes = client.search(searchRequest, RequestOptions.DEFAULT);
        System.out.println("返回结果为:"+searchRes.toString());
        System.out.println("总条数为:"+searchRes.getHits().getTotalHits().value);
        System.out.println("最大分数为:"+searchRes.getHits().getMaxScore());
        SearchHit[] hits = searchRes.getHits().getHits();
        for (SearchHit hit: hits) {
            System.out.println("id:为"+hit.getId()+",数据为:"+hit.getSourceAsString());
        }
        client.close();
    }


    /**
     * 根据关键词term查询
     * @throws IOException
     */
    @Test
    public void termTest() throws IOException {
        QueryBuilder queryBuilder = QueryBuilders.termQuery("description","小米");
        query(queryBuilder);
    }

    /**
     * 范围range查询
     * @throws IOException
     */
    @Test
    public void rangeTest() throws IOException {
        RangeQueryBuilder queryBuilder=QueryBuilders.rangeQuery("price");
        queryBuilder.gte(100);
        queryBuilder.lt(300);
        query(queryBuilder);
    }

    /**
     * 前缀prefix查询
     * @throws IOException
     */
    @Test
    public void prefixTest() throws IOException {
        QueryBuilder queryBuilder = QueryBuilders.prefixQuery("description","小米");
        query(queryBuilder);
    }

    /**
     * 通配符wildcard查询,?匹配一个,*匹配多个
     * title 为keyword,不分词
     * @throws IOException
     */
    @Test
    public void wildcardTest() throws IOException {
        QueryBuilder queryBuilder = QueryBuilders.wildcardQuery("title","*红*");
        query(queryBuilder);
    }


    /**
     * 模糊fuzzy查询
     *
     * fuzzy 模糊查询,最大模糊错误,必须现在0-2之间
     * 1.搜索关键词长度为2,不允许存在模糊
     * 2.搜索关键词长度为3-5,允许一次模糊
     * 3.搜索关键词长度大于5,允许最大2次模糊
     * @throws IOException
     */
    @Test
    public void fuzzyTest() throws IOException {
        FuzzyQueryBuilder queryBuilder = QueryBuilders.fuzzyQuery("title", "红米大机").fuzziness(Fuzziness.TWO);
        query(queryBuilder);
    }


    /**
     * ids查询
     * @throws IOException
     */
    @Test
    public void idsTest() throws IOException {
        QueryBuilder queryBuilder = QueryBuilders.idsQuery().addIds("1","3");
        query(queryBuilder);
    }

    /**
     * multiMatch多字段查询,基于分词器
     * @throws IOException
     */
    @Test
    public void multiMatchTest() throws IOException {
        QueryBuilder queryBuilder = QueryBuilders.multiMatchQuery("真棒").field("title").field("description");
        query(queryBuilder);
    }

    /**
     * 分页查询 form 起始位,size条数
     * 排序查询 sort
     * 包含和排查字段
     * @throws IOException
     */
    @Test
    public void pageTest() throws IOException {
        SearchRequest searchRequest=new SearchRequest();
        searchRequest.indices("products");
        SearchSourceBuilder query = new SearchSourceBuilder().query(QueryBuilders.matchAllQuery());
        //默认10条
        query.from(0).size(2);
        query.sort("price", SortOrder.DESC);
        //需要的字段名称
        String[] include={};
        //不需要的字段名称
        String[] exclude={"created_at"};
        query.fetchSource(include,exclude);
        searchRequest.source(query);
        SearchResponse searchRes = client.search(searchRequest, RequestOptions.DEFAULT);
        System.out.println("总条数为:"+searchRes.getHits().getTotalHits().value);
        System.out.println("最大分数为:"+searchRes.getHits().getMaxScore());
        SearchHit[] hits = searchRes.getHits().getHits();
        for (SearchHit hit: hits) {
            System.out.println("id:为"+hit.getId()+",数据为:"+hit.getSourceAsString());
        }
        client.close();
    }



    /**
     * 高亮查询,不能用matchAllQuery查询
     * @throws IOException
     */
    @Test
    public void  highlightTest() throws IOException {
        SearchRequest searchRequest=new SearchRequest();
        searchRequest.indices("products");
        SearchSourceBuilder query = new SearchSourceBuilder().query(QueryBuilders.termQuery("description","苹果"));
        HighlightBuilder highlightBuilder=new HighlightBuilder();
        highlightBuilder.preTags("<font color='red'>");
        highlightBuilder.postTags("</font>");
        /**
         * 需要高亮的字段
         */
        highlightBuilder.field("title").field("description");
        highlightBuilder.requireFieldMatch(false);
        query.highlighter(highlightBuilder);
        searchRequest.source(query);
        System.out.println("查询DSL为:"+searchRequest.source().toString());
        SearchResponse searchRes = client.search(searchRequest, RequestOptions.DEFAULT);
        System.out.println("返回结果为:"+searchRes.toString());
        System.out.println("总条数为:"+searchRes.getHits().getTotalHits().value);
        System.out.println("最大分数为:"+searchRes.getHits().getMaxScore());
        SearchHit[] hits = searchRes.getHits().getHits();
        for (SearchHit hit: hits) {
            System.out.println("id:为"+hit.getId()+",数据为:"+hit.getSourceAsString());
            Map<String, HighlightField> highlightFields = hit.getHighlightFields();
           if(highlightFields.containsKey("description")){
               System.out.println("description高亮的结果:"+highlightFields.get("description").fragments()[0]);
           }
            if(highlightFields.containsKey("title")){
                System.out.println("title高亮的结果:"+highlightFields.get("title").fragments()[0]);
            }
        }
        client.close();
    }



    /**
     * query :精确查询,查询计算文档得分,并根据得分进行返回
     * filter query:过滤查询,用在大数量中筛选出本地查询相关数据,不会计算文档得分,经常使用filter query 结果进行缓存
     * 注意:一旦使用query和filter query 。ES 优先执行filter,然后再执行query
     *  postFilter 中可以搭配term,terms,range,ids,exists
     * @throws IOException
     */
    @Test
    public void  filterTest() throws IOException {
        SearchRequest searchRequest=new SearchRequest();
        searchRequest.indices("products");
        SearchSourceBuilder builder = new SearchSourceBuilder().query(QueryBuilders.termQuery("description","手机"));

        //builder.postFilter(QueryBuilders.rangeQuery("price").gt(100).lt(200));
        //builder.postFilter(QueryBuilders.idsQuery().addIds("1","2"));
        //如果某个字段存在
        builder.postFilter(QueryBuilders.existsQuery("title"));
        searchRequest.source(builder);
        System.out.println("查询DSL为:"+searchRequest.source().toString());
        SearchResponse searchRes = client.search(searchRequest, RequestOptions.DEFAULT);
        System.out.println("返回结果为:"+searchRes.toString());
        System.out.println("总条数为:"+searchRes.getHits().getTotalHits().value);
        System.out.println("最大分数为:"+searchRes.getHits().getMaxScore());
        SearchHit[] hits = searchRes.getHits().getHits();
        for (SearchHit hit: hits) {
            System.out.println("id:为"+hit.getId()+",数据为:"+hit.getSourceAsString());
        }
        client.close();
    }


    /**
     * aggs :聚合查询(aggs) -sql中分组
     * @throws IOException
     */
    @Test
    public void  composeTest() throws IOException {
        SearchRequest searchRequest=new SearchRequest();
        searchRequest.indices("products");
        SearchSourceBuilder builder = new SearchSourceBuilder();
        BoolQueryBuilder boolQueryBuilder=QueryBuilders.boolQuery();
        boolQueryBuilder.must(QueryBuilders.termQuery("title","红米手机"));
        boolQueryBuilder.should(QueryBuilders.rangeQuery("price").gt(100).lt(200));
        builder.query(boolQueryBuilder);
        searchRequest.source(builder);
        System.out.println("查询DSL为:"+searchRequest.source().toString());
        SearchResponse searchRes = client.search(searchRequest, RequestOptions.DEFAULT);
        System.out.println("返回结果为:"+searchRes.toString());
        System.out.println("总条数为:"+searchRes.getHits().getTotalHits().value);
        System.out.println("最大分数为:"+searchRes.getHits().getMaxScore());
        SearchHit[] hits = searchRes.getHits().getHits();
        for (SearchHit hit: hits) {
            System.out.println("id:为"+hit.getId()+",数据为:"+hit.getSourceAsString());
        }
        client.close();

    }

    /**
     * aggs :聚合查询(aggs) -sql中分组
     * @throws IOException
     */
    @Test
    public void  aggsTest() throws IOException {
        SearchRequest searchRequest = new SearchRequest();
        searchRequest.indices("products");
        SearchSourceBuilder builder = new SearchSourceBuilder();
        AggregationBuilder aggregationBuilder = AggregationBuilders.terms("priceGroup").field("price");
        builder.aggregation(aggregationBuilder);

        /**
         * 如果不需要返回查询的结果,size=0
         *      builder.size(0);
         */

        searchRequest.source(builder);
        SearchResponse searchRes = client.search(searchRequest, RequestOptions.DEFAULT);
        SearchHits hits = searchRes.getHits();
        for (SearchHit hit : hits) {
            System.out.println("id:为"+hit.getId()+",数据为:"+hit.getSourceAsString());
        }
        Aggregations aggregations = searchRes.getAggregations();
        /**
           因为是根据Price来分组,price是double类型的,所以用ParsedDoubleTerms,适用于k->v这种的
         */
        ParsedDoubleTerms parsedDoubleTerms = aggregations.get("priceGroup");
        List<? extends Terms.Bucket> buckets = parsedDoubleTerms.getBuckets();
        for (Terms.Bucket bucket:buckets){
            System.out.println("key:为"+bucket.getKeyAsString()+",count数量为:"+bucket.getDocCount());
        }
        client.close();

    }


    /**
     * aggs :聚合查询(aggs) -聚合函数max,min,avg,sum
     * @throws IOException
     */
    @Test
    public void  aggsOperateTest() throws IOException {
        SearchRequest searchRequest = new SearchRequest();
        searchRequest.indices("products");
        SearchSourceBuilder builder = new SearchSourceBuilder();
        AggregationBuilder aggregationBuilder = AggregationBuilders.sum("priceSum").field("price");
        builder.aggregation(aggregationBuilder);

        /**
         * 如果不需要返回查询的结果,size=0
         *      builder.size(0);
         */

        searchRequest.source(builder);
        SearchResponse searchRes = client.search(searchRequest, RequestOptions.DEFAULT);
        SearchHits hits = searchRes.getHits();
        for (SearchHit hit : hits) {
            System.out.println("id:为"+hit.getId()+",数据为:"+hit.getSourceAsString());
        }
        Aggregations aggregations = searchRes.getAggregations();

        /**
         max 使用ParsedMax处理
         min 使用ParsedMin处理
         avg 使用 ParsedAvg处理
         sum 使用ParsedSum处理
         */
        ParsedSum parsedSum = aggregations.get("priceSum");
        double sumValue = parsedSum.getValue();
        System.out.println("求和值为:"+sumValue);
        client.close();
    }

 /**
     * collapse :折叠,比如根据菜系折叠,从每个菜系中选前2名好吃的菜
     * <p>
     * 下面是根据价格折叠,然后再每个子域中按照title折叠
     *
     * @throws IOException
     */
    @Test
    public void collapseTest() throws IOException {
        SearchRequest searchRequest = new SearchRequest();
        searchRequest.indices("products");
        SearchSourceBuilder builder = new SearchSourceBuilder();

        QueryBuilder queryBuilder = QueryBuilders.matchQuery("brand", "vivo");
        builder.query(queryBuilder);
        CollapseBuilder collapseBuilder = new CollapseBuilder("price");
        List<InnerHitBuilder> innerHits = new ArrayList<>();
        InnerHitBuilder innerHitBuilder = new InnerHitBuilder();
        innerHitBuilder.setName("r_date");
        innerHitBuilder.addSort(SortBuilders.fieldSort("created_at").order(SortOrder.DESC));
        innerHitBuilder.setSize(10);
        innerHitBuilder.setInnerCollapse(new CollapseBuilder("title"));
        innerHits.add(innerHitBuilder);
        collapseBuilder.setInnerHits(innerHits);
        builder.collapse(collapseBuilder);
        searchRequest.source(builder);
        System.out.println("查询DSL为:" + searchRequest.source().toString());
        SearchResponse searchRes = client.search(searchRequest, RequestOptions.DEFAULT);
        SearchHits hits = searchRes.getHits();
        for (SearchHit hit : hits) {
            System.out.println("id:为" + hit.getId() + ",数据为:" + hit.getSourceAsString());
            Map<String, SearchHits> innerHitsRes = hit.getInnerHits();
            for (Map.Entry<String, SearchHits> map : innerHitsRes.entrySet()) {
                String fieldKey = map.getKey();
                SearchHits innerSearchHits = map.getValue();
                SearchHit[] innerHitRes = innerSearchHits.getHits();
                for (SearchHit h : innerHitRes) {
                    System.out.println("collapse field: " + fieldKey + ",id:为" + h.getId() + ",数据为:" + h.getSourceAsString());
                }
            }

        }
        client.close();
    }


    /**
     * aggs :聚合查询(aggs) -top_hits 取分组中的前几位
     *
     * @throws IOException
     */
    @Test
    public void aggsTopHitsTest() throws IOException {
        SearchRequest searchRequest = new SearchRequest();
        searchRequest.indices("products");
        SearchSourceBuilder builder = new SearchSourceBuilder();
        AggregationBuilder aggregationBuilder = AggregationBuilders.topHits("price_top").sort(SortBuilders.fieldSort("price").order(SortOrder.DESC)).size(2);
        builder.aggregation(aggregationBuilder);

        /**
         * 如果不需要返回查询的结果,size=0
         *      builder.size(0);
         */
        builder.size(0);
        searchRequest.source(builder);
        SearchResponse searchRes = client.search(searchRequest, RequestOptions.DEFAULT);
        SearchHits hits = searchRes.getHits();
        for (SearchHit hit : hits) {
            System.out.println("id:为" + hit.getId() + ",数据为:" + hit.getSourceAsString());
        }
        Aggregations aggregations = searchRes.getAggregations();
        TopHits topHits = aggregations.get("price_top");
        SearchHits hitsRes = topHits.getHits();

        for (SearchHit hit : hitsRes.getHits()) {
            System.out.println("id:为" + hit.getId() + ",数据为:" + hit.getSourceAsString());
        }
        client.close();

    }



    /**
     * aggs :聚合查询(aggs) -percentile_ranks 百分比排名
     *
     * 统计手机在不同的价格区间的占比
     * @throws IOException
     */
    @Test
    public void aggsPercentileRanksTest() throws IOException {
        SearchRequest searchRequest = new SearchRequest();
        searchRequest.indices("products");
        SearchSourceBuilder builder = new SearchSourceBuilder();
        double[] rankVal={200d,300d,500d};
        PercentileRanksAggregationBuilder aggregationBuilder = AggregationBuilders.percentileRanks("price_ranks",rankVal);
        aggregationBuilder.field("price");
        builder.aggregation(aggregationBuilder);

        /**
         * 如果不需要返回查询的结果,size=0
         *      builder.size(0);
         */
        builder.size(0);
        searchRequest.source(builder);
        SearchResponse searchRes = client.search(searchRequest, RequestOptions.DEFAULT);
        SearchHits hits = searchRes.getHits();
        for (SearchHit hit : hits) {
            System.out.println("id:为" + hit.getId() + ",数据为:" + hit.getSourceAsString());
        }
        Aggregations aggregations = searchRes.getAggregations();
        PercentileRanks rercentileRanks  = aggregations.get("price_ranks");
        for (Percentile entry : rercentileRanks) {
            double percent = entry.getPercent();    // Percent
            double value = entry.getValue();        // Value
            System.out.println("value:"+value+"percent:"+percent);
        }
        client.close();

    }

 /**
     * 自动补全功能,使用拼音分词器搜索
     *
     * @throws IOException
     */
    @Test
    public void autoCompleteTest() throws IOException {
        SearchRequest searchRequest = new SearchRequest();
        searchRequest.indices("stars");
        SearchSourceBuilder builder = new SearchSourceBuilder();

        CompletionSuggestionBuilder completionSuggestionBuilder=new CompletionSuggestionBuilder("name").skipDuplicates(true).size(10);
        completionSuggestionBuilder.prefix("liu");
        SuggestBuilder suggestBuilder=new SuggestBuilder();
        suggestBuilder.addSuggestion("star_name_suggest",completionSuggestionBuilder);
        builder.suggest(suggestBuilder);
        /**
         * 如果不需要返回查询的结果,size=0
         *      builder.size(0);
         */
        builder.size(0);
        /**
         * 不需要Source可以去掉
         */
        builder.fetchSource(false);
        searchRequest.source(builder);
        System.out.println("查询DSL为:" + searchRequest.source().toString());
        SearchResponse searchRes = client.search(searchRequest, RequestOptions.DEFAULT);

        CompletionSuggestion
               suggest = searchRes.getSuggest().getSuggestion("star_name_suggest");
        ArrayList<String> list=new ArrayList<>();
        suggest.forEach(c->c.getOptions().forEach(f->
                list.add(   f.getText().toString())
        ));
        list.forEach(System.out::println);
        client.close();

    }

}


有一些写法可以参考
https://www.kgraph.cn/218.html

  大数据 最新文章
实现Kafka至少消费一次
亚马逊云科技:还在苦于ETL?Zero ETL的时代
初探MapReduce
【SpringBoot框架篇】32.基于注解+redis实现
Elasticsearch:如何减少 Elasticsearch 集
Go redis操作
Redis面试题
专题五 Redis高并发场景
基于GBase8s和Calcite的多数据源查询
Redis——底层数据结构原理
上一篇文章      下一篇文章      查看所有文章
加:2022-03-15 22:37:17  更:2022-03-15 22:40:53 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/24 7:58:15-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码