当前位置: 首页 > 知识库问答 >
问题:

用Java REST客户端API在Elasticsearch 6.5.4中完成建议

农鸿德
2023-03-14
curl -H "Content-Type: application/json" -X POST localhost:9200/hotels/_search?pretty -d^
"{^
    \"suggest\": {^
        \"hotel_suggest\" : {^
            \"prefix\" : \"hot\", ^
            \"completion\" : { ^
                \"field\" : \"name_suggest\" ^
            }^
        }^
    }^
}"
    public String suggestSearch() throws IOException {
        SearchRequest searchRequest = new SearchRequest("hotels");
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
//        searchSourceBuilder.query(QueryBuilders.matchAllQuery());

        SuggestionBuilder termSuggestionBuilder =
                SuggestBuilders.termSuggestion("name").text("hot");
        SuggestBuilder suggestBuilder = new SuggestBuilder();
        suggestBuilder.addSuggestion("name_suggest", termSuggestionBuilder);
        searchSourceBuilder.suggest(suggestBuilder);

        searchRequest.source(searchSourceBuilder);
        SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);


        Suggest suggest = searchResponse.getSuggest();
        TermSuggestion termSuggestion = suggest.getSuggestion("name_suggest");

        for (TermSuggestion.Entry entry : termSuggestion.getEntries()) {
            for (TermSuggestion.Entry.Option option : entry) {
                String suggestText = option.getText().string();
                System.out.println(suggestText);
            }
        }
        return "SUCCESS";
    }

共有1个答案

岳和泽
2023-03-14
public static void suggestSearch() 
    {

        RestHighLevelClient client = new RestHighLevelClient(
                RestClient.builder(
                        new HttpHost( "your-ip-here", 9200, "http" ) ) );

        SearchRequest searchRequest = new SearchRequest( "music" );
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();

        SuggestionBuilder termSuggestionBuilder = SuggestBuilders.completionSuggestion( "suggest" ).prefix( "nev" );
        SuggestBuilder suggestBuilder = new SuggestBuilder();
        suggestBuilder.addSuggestion( "song-suggest", termSuggestionBuilder );
        searchSourceBuilder.suggest( suggestBuilder );

        searchRequest.source( searchSourceBuilder );
        SearchResponse searchResponse = null;

        try
        {
            searchResponse = client.search( searchRequest );
        }
        catch ( IOException e )
        {
            e.printStackTrace();
        }

        Suggest suggest = searchResponse.getSuggest();
        CompletionSuggestion entries = suggest.getSuggestion( "song-suggest" );

        for ( CompletionSuggestion.Entry entry : entries )
        {
            for ( CompletionSuggestion.Entry.Option option : entry.getOptions() )
            {
                String suggestText = option.getText().string();
                System.out.println( suggestText );
            }
        }
    }
public static void suggestSearch()
    {

        RestHighLevelClient client = new RestHighLevelClient(
                RestClient.builder(
                        new HttpHost( "192.168.1.245", 9200, "http" ) ) );

        SearchRequest searchRequest = new SearchRequest( "hotels" );
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
       searchSourceBuilder.query(QueryBuilders.matchAllQuery());

        CompletionSuggestionBuilder completionSuggestionBuilder = SuggestBuilders.completionSuggestion( "name_suggest" ).prefix( "M" );

        SuggestBuilder suggestBuilder = new SuggestBuilder();
        suggestBuilder.addSuggestion( "name_suggest", completionSuggestionBuilder );
        searchSourceBuilder.suggest( suggestBuilder );

        searchRequest.source( searchSourceBuilder );
        SearchResponse searchResponse = null;

        try
        {
            searchResponse = client.search( searchRequest );
        }
        catch ( IOException e )
        {
            e.printStackTrace();
        }

        Suggest suggest = searchResponse.getSuggest();
        CompletionSuggestion entries = suggest.getSuggestion( "name_suggest" );

        for ( CompletionSuggestion.Entry entry : entries )
        {
            for ( CompletionSuggestion.Entry.Option option : entry.getOptions() )
            {
                String suggestText = option.getText().string();
                System.out.println( suggestText );
            }
        }
    }
 类似资料:
  • 我目前正在尝试使用C#中的RestSharp(不想使用客户端库)为我的客户集成单个谷歌广告帐户。我发现了一个有用的链接,可以通过提供客户端ID、客户端机密和刷新令牌来生成访问令牌。 https://developers.google.com/google-ads/api/docs/rest/auth 我还尝试了上面链接中提到的CURL in postman,但得到了相同的错误。 仅供参考它是一个桌

  • 创建客户端有两种方式,一种是直接使用特化的构造器函数,另一种是使用工厂构造器函数。 第一种方式返回的是具体的客户端结构体指针对象,第二种方式返回的是客户端接口对象。 使用特化的构造器函数创建客户端 特化的构造器函数有下面几个: func NewHTTPClient(uri ...string) (client *HTTPClient) func NewTCPClient(uri ...string

  • ngrok客户端公开了一个REST API,它授予对以下各项的编程访问权限: 收集状态和指标信息 收集和重放捕获的请求 动态启动和停止隧道 基本URL和身份验证 Base URL http://127.0.0.1:4040/api Authentication None ngrok客户端API作为ngrok的本地Web检查接口的一部分公开。因为它在本地接口上提供,所以API没有身份验证。如果您覆盖

  • http://redis.cn/clients.html

  • Docusaurus 提供了一组客户端 API,对于构建网站很有帮助。 组件 <Head/> 这是一个可重用 React 组件,用于管理对 HTML 文档标头(即 <head> 中的标签)的修改。此组件接收纯 HTML 标签并输出纯 HTML 标签,对初学者很友好。此组件始对 React Helmet 的二次包装。 用法示例: import React from 'react'; import

  • 我涉足了Node JS。我需要Node JS(使用“请求”模块)通过HTTPS调用Java REST API。 正如在给定URL中解释的那样,我创建了一个CA文件(ca.key,ca.pem),服务器的证书(server1.key,server1.pem) 现在,我想从我的Node JS客户端访问在Apache Tomcat上运行的JAVA REST API。那么现在我如何导入我的节点JS服务器密