标签:
ES(elasticsearch)的suggester共有四类(term suggester, phrase suggester, completion suggester, context suggester), 其中completion suggester作为搜索框中的自动补齐功能,尤为常用。
本文将用java语言实现一个简单例子来叙述如何使用completion suggester。
例子的主要功能是为股票的名称和编号建立自动补齐功能。
实现一个完整的completion suggester功能,需要三个步骤:建立schema,索引数据,搜索数据。下面分别进行介绍。
1.建立schema
schema对于ES来说,就如同一个database的表格定义,它用于预先定义各个字段的名称以及类型等。
需要被自动补齐的数据,得用一个类型为completion的字段来存储待补齐数据。具体如下:
{ "stock_suggest" : { "mappings" : { "stock" : { "_id" : { "path" : "id" }, "properties" : { "id" : { "type" : "string", "analyzer" : "keyword" }, "name" : { "type" : "string", "index" : "not_analyzed" }, "nameSuggest" : { "type" : "completion", "analyzer" : "standard", "payloads" : true, "preserve_separators" : false, "preserve_position_increments" : false, "max_input_length" : 50 } } } } } }
需要说明的是payloads属性被设置为true是为了激活自动补齐时,返回一个payload字段用于承载预定义好(在索引数据时定义)的数据信息。
2. 索引数据
下面将待补齐数据注入到ES 中。代码如下:
1 String esHosts = ""; 2 String clusterName = ""; 3 GsonBuilder gsonBuilder = new GsonBuilder().setDateFormat("yyyy-MM-dd‘T‘HH:mm:ss.SSSZ"); 4 Gson gson = gsonBuilder.create(); 5 Settings settings = ImmutableSettings.settingsBuilder().put("client.transport.sniff", true).put("cluster.name", clusterName).put("node.client", true).build(); 6 Client client = new TransportClient(settings).addTransportAddress(new InetSocketTransportAddress(esHosts, 9300)); 7 BulkRequestBuilder bulkRequest = client.prepareBulk(); 8 List<Stock> stocks = new ArrayList(); 9 stocks.add(new Stock("601390", "中国中铁")); 10 stocks.add(new Stock("601186", "中国铁建")); 11 stocks.add(new Stock("601766", "中国中车")); 12 stocks.add(new Stock("600115", "东方航空")); 13 stocks.add(new Stock("000585", "东北电气")); 14 stocks.add(new Stock("000527", "美的电器")); 15 for (Stock stock : stocks) { 16 List<String> input = new ArrayList<String>(); 17 input.add(stock.getName()); 18 input.add(stock.getId()); 19 Map<Object, Object> payload = new HashMap<Object, Object>(); 20 payload.put("id", stock.getId()); 21 payload.put("name", stock.getName()); 22 payload.put("type", "stock"); 23 NameSuggest nameSuggest = new NameSuggest(input, payload, stock.getId()); 24 stock.setNameSuggest(nameSuggest); 25 JsonObject jo = (JsonObject) gson.toJsonTree(stock); 26 String jsonSource = gson.toJson(jo); 27 bulkRequest.add(client.prepareIndex(index, type, stock.getId()).setSource(jsonSource)); 28 } 29 BulkResponse bulkResponse = bulkRequest.execute().actionGet();
如上所示,我们插入了六条股票数据做为样例。其中第一行的esHosts和第二行的clusterName需要根据自己的ES集群配置自行设定。
3.搜索数据
数据建完索引后,就可以使用自动补齐功能啦。
1 String input = "60" 2 String clusterName = ""; 3 String esHosts = ""; 4 Settings settings = ImmutableSettings.settingsBuilder().put("client.transport.sniff", true).put("cluster.name", clusterName).put("node.client", true).build(); 5 Client client = new TransportClient(settings).addTransportAddress(new InetSocketTransportAddress(esHosts, 9300)); 6 String field = "nameSuggest"; 7 8 SuggestRequestBuilder srb = client.prepareSuggest(index); 9 CompletionSuggestionBuilder csfb = new CompletionSuggestionBuilder(field).field(field).text(input).size(10); 10 srb = srb.addSuggestion(csfb); 11 SuggestResponse response = srb.execute().actionGet(); 12 System.out.println(response);
上段代码的输出效果如下所示:
{ "nameSuggest" : [ { "text" : "60", "offset" : 0, "length" : 2, "options" : [ { "text" : "600115", "score" : 1.0, "payload":{"name":"东方航空","id":"600115","type":"stock"} }, { "text" : "601186", "score" : 1.0, "payload":{"name":"中国铁建","id":"601186","type":"stock"} }, { "text" : "601390", "score" : 1.0, "payload":{"name":"中国中铁","id":"601390","type":"stock"} }, { "text" : "601766", "score" : 1.0, "payload":{"name":"中国中车","id":"601766","type":"stock"} } ] } ] }
可以看出,编号以“60”开头的股票都被返回了。
好了,一个简单示例完成了,其中有些细节及相关原理将在后续文章中详细介绍。
Elasticsearch in java 范例:自动补齐功能(completion suggester)
标签:
原文地址:http://www.cnblogs.com/gumpeng/p/4907717.html