弹性搜索自动完成/建议Java

时间:2017-06-14 13:18:29

标签: java elasticsearch

我想在使用Java API在Elastic Search中键入功能时实现搜索。我想要转换为Java的查询如下。 你有什么想法,我怎么能用Java执行这个查询? 这些查询非常相似,但我想解决至少一个。

这是我最初的做法:

 SearchResponse response = client.prepareSearch("kal")
            .setTypes("products")
            .setQuery(multiMatchQuery("description_en", "name", "description_en"))// Query
            .setFrom(0).setSize(60).setExplain(true)
            .get();
    SearchHit[] results = response.getHits().getHits();
    for (SearchHit hit : results) {
        String sourceAsString = hit.getSourceAsString();
        Map<String, SearchHitField> responseFields = hit.getFields();
        SearchHitField field = responseFields.get("product_id");
        Map map = hit.getSource();
        System.out.println(map.toString());
    }

查询:

POST /kal/products/_search?pretty
{
    "suggest": {
        "name-suggest" : {
            "prefix" : "wine",
            "completion" : {
                "field" : "suggest_name"
            }
        }
    }
}

GET /kal/products/_search
{ "query": {
    "prefix" : { 
      "name" : "wine",
      "description": "wine"
    }
  }
}

GET /kal/products/_search
{ 
  "query" : {
        "multi_match" : {
            "fields" : ["name", "description_en"],
            "query" : "description_",
            "type" : "phrase_prefix"
        }
    }
}

0 个答案:

没有答案