0

我正在尝试从 elasticsearch 5.1 检索建议结果,但目前我得到一个空结果。我目前正在使用开玩笑的客户端。我的代码如下。

 JestClientFactory factory = new JestClientFactory();
            factory.setHttpClientConfig(new HttpClientConfig
                    .Builder("localhost:9200")
                    .multiThreaded(true)
                    .build());
            JestClient client = factory.getObject();
            String query="{\n" +
                    "  \"suggest\": {\n" +
                    "    \"text\": \"porm\",\n" +
                    "    \"simple_phrase\": {\n" +
                    "      \"phrase\": {\n" +
                    "        \"field\": \"content\",\n" +
                    "        \"size\": 1,\n" +
                    "        \"gram_size\": 3,\n" +
                    "        \"direct_generator\": [ {\n" +
                    "          \"field\": \"content\",\n" +
                    "          \"suggest_mode\": \"always\"\n" +
                    "        } ],\n" +
                    "        \"highlight\": {\n" +
                    "          \"pre_tag\": \"<em>\",\n" +
                    "          \"post_tag\": \"</em>\"\n" +
                    "        }\n" +
                    "      }\n" +
                    "    }\n" +
                    "  }\n" +
                    "}";
            Suggest suggest1 = new Suggest.Builder(query).addIndex("index").build();
            SuggestResult result1 = client.execute(suggest1);


            List<SuggestResult.Suggestion> suggestions = result1.getSuggestions("simple_phrase");
            ArrayList<String> suggest2=new ArrayList<String>();
            for (SuggestResult.Suggestion sugg:
                 suggestions) {
               suggest2.add(sugg.text);
            }
           return ok(Json.toJson(suggest2));
    }
4

1 回答 1

0
I have fixed this issue.Actually there was a problem with the json query.
I have modified the query to:
String query="{\n" +
                    "    \"simple_phrase\": {\n" +
                    "    \"text\": \"porm\",\n" +
                    "      \"phrase\": {\n" +
                    "        \"field\": \"content\",\n" +
                    "        \"size\": 1,\n" +
                    "        \"gram_size\": 3,\n" +
                    "        \"direct_generator\": [ {\n" +
                    "          \"field\": \"content\",\n" +
                    "          \"suggest_mode\": \"always\"\n" +
                    "        } ],\n" +
                    "        \"highlight\": {\n" +
                    "          \"pre_tag\": \"<em>\",\n" +
                    "          \"post_tag\": \"</em>\"\n" +
                    "        }\n" +
                    "      }\n" +
                    "    }\n" +
                    "}"; 
于 2017-02-16T16:36:16.240 回答