基本转于 http://www.cnblogs.com/dennisit/p/3346228.html
1.在pom.xml添加maven依赖
1 <dependency> 2 <groupId>org.elasticsearch</groupId> 3 <artifactId>elasticsearch</artifactId> 4 <version>2.4.1</version> 5 </dependency>
2.创建实体类
1 package org.dennisit.entity; 2 3 public class Medicine { 4 private Integer id; 5 private String name; 6 private String function; 7 8 public Medicine() { 9 super(); 10 } 11 12 public Medicine(Integer id, String name, String function) { 13 super(); 14 this.setId(id); 15 this.setName(name); 16 this.setFunction(function); 17 } 18 19 20 //getter and setter () 21 public Integer getId() { 22 return id; 23 } 24 25 public void setId(Integer id) { 26 this.id = id; 27 } 28 29 public String getName() { 30 return name; 31 } 32 33 public void setName(String name) { 34 this.name = name; 35 } 36 37 public String getFunction() { 38 return function; 39 } 40 41 public void setFunction(String function) { 42 this.function = function; 43 } 44 }
3.模拟数据集合
1 package org.dennisit.entity; 2 3 import java.util.ArrayList; 4 import java.util.List; 5 6 import org.dennisit.util.JsonUtil; 7 8 9 10 public class DataFactory { 11 public static DataFactory dataFactory = new DataFactory(); 12 13 private DataFactory(){ 14 15 } 16 17 public DataFactory getInstance(){ 18 return dataFactory; 19 } 20 21 public static List<String> getInitJsonData(){ 22 List<String> list = new ArrayList<String>(); 23 String data1 = JsonUtil.obj2JsonData(new Medicine(1,"银花 感冒 颗粒","功能主治:银花感冒颗粒 ,头痛,清热,解表,利咽。")); 24 String data2 = JsonUtil.obj2JsonData(new Medicine(2,"感冒 止咳糖浆","功能主治:感冒止咳糖浆,解表清热,止咳化痰。")); 25 String data3 = JsonUtil.obj2JsonData(new Medicine(3,"感冒灵颗粒","功能主治:解热镇痛。头痛 ,清热。")); 26 String data4 = JsonUtil.obj2JsonData(new Medicine(4,"感冒 灵胶囊","功能主治:银花感冒颗粒 ,头痛,清热,解表,利咽。")); 27 String data5 = JsonUtil.obj2JsonData(new Medicine(5,"仁和 感冒 颗粒","功能主治:疏风清热,宣肺止咳,解表清热,止咳化痰。")); 28 list.add(data1); 29 list.add(data2); 30 list.add(data3); 31 list.add(data4); 32 list.add(data5); 33 return list; 34 } 35 }
4.将实体对象转为json对象
1 1 package org.dennisit.util; 2 2 3 3 import java.io.IOException; 4 4 5 5 import org.dennisit.entity.Medicine; 6 6 import org.elasticsearch.common.xcontent.XContentBuilder; 7 7 import org.elasticsearch.common.xcontent.XContentFactory; 8 8 9 9 10 10 public class JsonUtil { 11 11 /** 12 12 * 实现将实体对象转换成json对象 13 13 * @param medicine Medicine对象 14 14 * @return 15 15 */ 16 16 public static String obj2JsonData(Medicine medicine){ 17 17 String jsonData = null; 18 18 try { 19 19 //使用XContentBuilder创建json数据 20 20 XContentBuilder jsonBuild = XContentFactory.jsonBuilder(); 21 21 jsonBuild.startObject() 22 22 .field("id",medicine.getId()) 23 23 .field("name", medicine.getName()) 24 24 .field("funciton",medicine.getFunction()) 25 25 .endObject(); 26 26 jsonData = jsonBuild.string(); 27 27 System.out.println(jsonData); 28 28 } catch (IOException e) { 29 29 e.printStackTrace(); 30 30 } 31 31 return jsonData; 32 32 } 33 33 }
5.搜索引擎封装
1 package org.dennisit.elastic.process; 2 3 import java.net.InetAddress; 4 import java.net.UnknownHostException; 5 import java.util.ArrayList; 6 import java.util.List; 7 8 import org.dennisit.entity.Medicine; 9 import org.elasticsearch.action.index.IndexRequestBuilder; 10 import org.elasticsearch.action.index.IndexResponse; 11 import org.elasticsearch.action.search.SearchResponse; 12 import org.elasticsearch.client.Client; 13 import org.elasticsearch.client.transport.TransportClient; 14 import org.elasticsearch.common.settings.Settings; 15 import org.elasticsearch.common.transport.InetSocketTransportAddress; 16 import org.elasticsearch.index.query.QueryBuilder; 17 import org.elasticsearch.search.SearchHit; 18 import org.elasticsearch.search.SearchHits; 19 20 public class ElasticSearchHandler { 21 private Client client; 22 23 24 public ElasticSearchHandler() { 25 /** 26 * 设置 27 */ 28 Settings settings = Settings.settingsBuilder() 29 .put("cluster.name","elasticsearch")//我的集群名 30 .put("client.transport.sniff",true) //启动嗅探功能 31 .build(); 32 try { 33 client = TransportClient.builder().settings(settings).build() 34 .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("localhost"), 9300)); 35 } catch (UnknownHostException e) { 36 e.printStackTrace(); 37 } 38 } 39 40 /** 41 * 建立索引,索引建立好之后,会在elasticsearch-2.4.1dataelasticsearch odes 创建所以你看 42 * @param indexName 为索引库名,一个es集群中可以有多个索引库。 名称必须为小写 43 * @param indexType Type为索引类型,是用来区分同索引库下不同类型的数据的,一个索引库下可以有多个索引类型。 44 * @param jsondata json格式的数据集合 45 * 46 * @return 47 */ 48 public void createIndexResponse(String indexname, String type, List<String> jsondata){ 49 //创建索引库 需要注意的是.setRefresh(true)这里一定要设置,否则第一次建立索引查找不到数据 50 IndexRequestBuilder requestBuilder = client.prepareIndex(indexname, type).setRefresh(true); 51 for(int i=0; i<jsondata.size(); i++){ 52 requestBuilder.setSource(jsondata.get(i)).execute().actionGet(); 53 } 54 55 } 56 57 /** 58 * 创建索引 59 * @param client 60 * @param jsondata 61 * @return 62 */ 63 public IndexResponse createIndexResponse(String indexname, String type,String jsondata){ 64 IndexResponse response = client.prepareIndex(indexname, type) 65 .setSource(jsondata) 66 .execute() 67 .actionGet(); 68 return response; 69 } 70 71 /** 72 * 执行搜索 73 * @param queryBuilder 74 * @param indexname 75 * @param type 76 * @return 77 */ 78 public List<Medicine> searcher(QueryBuilder queryBuilder, String indexname, String type){ 79 List<Medicine> list = new ArrayList<Medicine>(); 80 SearchResponse searchResponse = client.prepareSearch(indexname).setTypes(type) 81 .setQuery(queryBuilder) 82 .execute() 83 .actionGet(); 84 SearchHits hits = searchResponse.getHits(); 85 System.out.println("查询到记录数=" + hits.getTotalHits()); 86 SearchHit[] searchHists = hits.getHits(); 87 if(searchHists.length>0){ 88 for(SearchHit hit:searchHists){ 89 Integer id = (Integer)hit.getSource().get("id"); 90 String name = (String) hit.getSource().get("name"); 91 String function = (String) hit.getSource().get("funciton"); 92 list.add(new Medicine(id, name, function)); 93 } 94 } 95 return list; 96 } 97 }
6.搜索
1 package org.dennisit.main; 2 3 import java.util.List; 4 5 import org.dennisit.elastic.process.ElasticSearchHandler; 6 import org.dennisit.entity.DataFactory; 7 import org.dennisit.entity.Medicine; 8 import org.elasticsearch.index.query.QueryBuilder; 9 import org.elasticsearch.index.query.QueryBuilders; 10 11 public class ElasticsearchTest { 12 13 public static void main(String[] args) { 14 ElasticSearchHandler esHandler = new ElasticSearchHandler(); 15 List<String> jsondata = DataFactory.getInitJsonData(); 16 //索引名称 17 String indexname = "eindexdemo"; 18 19 String type = "etypedemo"; 20 //创建索引 21 esHandler.createIndexResponse(indexname, type, jsondata); 22 23 //查询条件 24 QueryBuilder queryBuilder = QueryBuilders.termQuery("name", "text"); 25 List<Medicine> result = esHandler.searcher(queryBuilder, indexname, type); 26 for(int i=0; i<result.size(); i++){ 27 Medicine medicine = result.get(i); 28 System.out.println("(" + medicine.getId() + ")药品名称:" +medicine.getName() + " " + medicine.getFunction()); 29 } 30 } 31 32 }