项目清单
elasticsearch服务下载包括其中插件和分词
项目源码
资源文件
app.properties
1. elasticsearch.esNodes=localhost:9300
2. elasticsearch.cluster.name=heroscluster
app.xml
1. <?xml version="1.0" encoding="UTF-8"?>
2. <beans xmlns="http://www.springframework.org/schema/beans"
3. "http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util"
4. "http://www.springframework.org/schema/context"
5. "http://www.pilato.fr/schema/elasticsearch"
6. //www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
7. //www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd
8. //www.pilato.fr/schema/elasticsearch http://www.pilato.fr/schema/elasticsearch/elasticsearch-0.3.xsd
9. //www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">
10. <context:annotation-config />
11. <!-- 自动扫描所有注解该路径 -->
12. package="com.sf.heros.mq.*" /> -->
13. "classpath:/app.properties" />
14.
15. import resource="elasticseach.xml" />
16. </beans>
elasticseach.xml
1. <?xml version="1.0" encoding="UTF-8"?>
2. <beans xmlns="http://www.springframework.org/schema/beans"
3. "http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util"
4. "http://www.springframework.org/schema/context"
5. "http://www.pilato.fr/schema/elasticsearch"
6. //www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
7. //www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd
8. //www.pilato.fr/schema/elasticsearch http://www.pilato.fr/schema/elasticsearch/elasticsearch-0.3.xsd
9. //www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">
10.
11. "esproperties">
12. "cluster.name" value="${elasticsearch.cluster.name}" />
13. </util:map>
14.
15. "client" properties="esproperties"
16. "${elasticsearch.esNodes}" />
17.
18. "elasticsearchTemplate"
19. class="org.springframework.data.elasticsearch.core.ElasticsearchTemplate">
20. "client" ref="client" />
21. </bean>
22.
23. "elasticsearchService" class="com.sf.heros.mq.consumer.service.ElasticsearchService"
24. "init" />
25.
26. "es" class="com.sf.daidongxi.web.service.ElasticsearchService"></bean>
27. </beans>
maven
1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2. "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
3. 4.0.0</modelVersion>
4. <groupId>com.elasticsearch</groupId>
5. <artifactId>elasticsearch</artifactId>
6. <packaging>war</packaging>
7. 0.0.1-SNAPSHOT</version>
8. <name>elasticsearch Maven Webapp</name>
9. //maven.apache.org</url>
10. <properties>
11. 3.1.1.RELEASE</spring.version>
12. 2.0.0</findbugs.annotations>
13. 2.11</checkstyle.maven.plugin>
14. 3.0</pmd.maven.plugin>
15. 2.5.3</findbugs.maven.plugin>
16. 1.7</java.version>
17. </properties>
18. <dependencies>
19. <dependency>
20. <groupId>junit</groupId>
21. <artifactId>junit</artifactId>
22. 3.8.1</version>
23. <scope>test</scope>
24. </dependency>
25. <!-- spring begin -->
26. <dependency>
27. <groupId>org.springframework</groupId>
28. <artifactId>spring-context</artifactId>
29. <version>${spring.version}</version>
30. </dependency>
31. <dependency>
32. <groupId>org.springframework</groupId>
33. <artifactId>spring-context-support</artifactId>
34. <version>${spring.version}</version>
35. </dependency>
36.
37. <dependency>
38. <groupId>org.springframework</groupId>
39. <artifactId>spring-aop</artifactId>
40. <version>${spring.version}</version>
41. </dependency>
42.
43. <dependency>
44. <groupId>org.springframework</groupId>
45. <artifactId>spring-core</artifactId>
46. <version>${spring.version}</version>
47. </dependency>
48.
49. <dependency>
50. <groupId>org.springframework</groupId>
51. <artifactId>spring-jdbc</artifactId>
52. <version>${spring.version}</version>
53. </dependency>
54.
55. <!-- spring end -->
56.
57. package -->
58. <dependency>
59. <groupId>fr.pilato.spring</groupId>
60. <artifactId>spring-elasticsearch</artifactId>
61. 1.0.0</version>
62. </dependency>
63.
64. <dependency>
65. <groupId>org.elasticsearch</groupId>
66. <artifactId>elasticsearch</artifactId>
67. 1.0.0</version>
68. </dependency>
69.
70. <dependency>
71. <groupId>org.springframework.data</groupId>
72. <artifactId>spring-data-elasticsearch</artifactId>
73. 1.0.0.RELEASE</version>
74. </dependency>
75.
76. <dependency>
77. <groupId>com.alibaba</groupId>
78. <artifactId>druid</artifactId>
79. 1.0.5</version>
80. </dependency>
81.
82. <!--json-lib -->
83. <dependency>
84. <groupId>net.sf.json-lib</groupId>
85. <artifactId>json-lib</artifactId>
86. 2.4</version>
87. <classifier>jdk15</classifier>
88. </dependency>
89.
90. <!-- quartz job -->
91. <dependency>
92. <groupId>org.quartz-scheduler</groupId>
93. <artifactId>quartz</artifactId>
94. 2.2.1</version>
95. </dependency>
96.
97. <!-- log4j -->
98. <dependency>
99. <groupId>org.slf4j</groupId>
100. <artifactId>slf4j-log4j12</artifactId>
101. 1.7.5</version>
102. </dependency>
103. </dependencies>
104. <build>
105. <finalName>elasticsearch</finalName>
106. </build>
107. </project>
Java.class
Bean配置
1. package com.sf.heros.mq.consumer.vo;
2.
3. import org.springframework.data.annotation.Id;
4. import org.springframework.data.elasticsearch.annotations.Document;
5. import org.springframework.data.elasticsearch.annotations.Field;
6. import org.springframework.data.elasticsearch.annotations.FieldIndex;
7. import org.springframework.data.elasticsearch.annotations.FieldType;
8.
9. import com.sf.heros.mq.consumer.utils.APP;
10.
11. //@Document(indexName = APP.ESProp.INDEX_NAME, type = APP.ESProp.TYPE_TASK_INFO, indexStoreType = APP.ESProp.INDEX_STORE_TYPE, shards = APP.ESProp.SHARDS, replicas = APP.ESProp.REPLICAS, refreshInterval = APP.ESProp.REFRESH_INTERVAL)
12. @Document(indexName = APP.ESProp.INDEX_NAME, type = APP.ESProp.TYPE_TASK_INFO)
13. public class TaskInfo {
14. @Id
15. @Field(index = FieldIndex.not_analyzed, store = true)
16. private String taskId;
17.
18. @Field(type = FieldType.Integer, index = FieldIndex.not_analyzed, store = true)
19. private Integer userId;
20.
21. @Field(type = FieldType.String, indexAnalyzer="ik", searchAnalyzer="ik", store = true)
22. private String taskContent;
23.
24. @Field(type = FieldType.String, indexAnalyzer="ik", searchAnalyzer="ik", store = true)
25. private String taskArea;
26.
27. @Field(type = FieldType.String, indexAnalyzer="ik", searchAnalyzer="ik", store = true)
28. private String taskTags;
29.
30. @Field(type = FieldType.Integer, index = FieldIndex.not_analyzed, store = true)
31. private Integer taskState;
32.
33. @Field(type = FieldType.String, index = FieldIndex.not_analyzed, store = true)
34. private String updateTime;
35.
36. @Field(type = FieldType.String, indexAnalyzer="ik", searchAnalyzer="ik", store = true)
37. private String userNickName;
38.
39. public String getTaskId() {
40. return taskId;
41. }
42.
43. public void setTaskId(String taskId) {
44. this.taskId = taskId;
45. }
46.
47. public Integer getUserId() {
48. return userId;
49. }
50.
51. public void setUserId(Integer userId) {
52. this.userId = userId;
53. }
54.
55. public String getTaskContent() {
56. return taskContent;
57. }
58.
59. public void setTaskContent(String taskContent) {
60. this.taskContent = taskContent;
61. }
62.
63. public String getTaskArea() {
64. return taskArea;
65. }
66.
67. public void setTaskArea(String taskArea) {
68. this.taskArea = taskArea;
69. }
70.
71. public String getTaskTags() {
72. return taskTags;
73. }
74.
75. public void setTaskTags(String taskTags) {
76. this.taskTags = taskTags;
77. }
78.
79. public Integer getTaskState() {
80. return taskState;
81. }
82.
83. public void setTaskState(Integer taskState) {
84. this.taskState = taskState;
85. }
86.
87. public String getUpdateTime() {
88. return updateTime;
89. }
90.
91. public void setUpdateTime(String updateTime) {
92. this.updateTime = updateTime;
93. }
94.
95. public String getUserNickName() {
96. return userNickName;
97. }
98.
99. public void setUserNickName(String userNickName) {
100. this.userNickName = userNickName;
101. }
102.
103. @Override
104. public String toString() {
105. return "TaskInfo [taskId=" + taskId + ", userId=" + userId
106. ", taskContent=" + taskContent + ", taskArea=" + taskArea
107. ", taskState=" + taskState
108. ", updateTime=" + updateTime + ", userNickName="
109. "]";
110. }
111.
112. public TaskInfo(String taskId, Integer userId, String taskContent,
113. String taskArea, String taskTags, Integer taskState,
114. String updateTime, String userNickName) {
115. this.taskId = taskId;
116. this.userId = userId;
117. this.taskContent = taskContent;
118. this.taskArea = taskArea;
119. this.taskTags = taskTags;
120. this.taskState = taskState;
121. this.updateTime = updateTime;
122. this.userNickName = userNickName;
123. }
124. public TaskInfo() {
125. // TODO Auto-generated constructor stub
126. }
127. }
增删改类
1. /**
2. *@Pr锛歨eros
3. *@Date: 2014-5-4 涓婂崍9:21:27
4. *@Author: seaphy
5. *@Copyright: 漏 2012 sf-express.com Inc. All rights reserved
6. *娉ㄦ剰锛氭湰鍐呭浠呴檺浜庨『涓伴�熻繍鍏徃鍐呴儴浼犻槄锛岀姝㈠娉勪互鍙婄敤浜庡叾浠栫殑鍟嗕笟鐩殑
7. */
8. package com.sf.heros.mq.consumer.service;
9.
10. import java.util.ArrayList;
11. import java.util.List;
12.
13. import org.apache.log4j.Logger;
14. import org.elasticsearch.action.ActionFuture;
15. import org.elasticsearch.action.admin.cluster.health.ClusterHealthRequest;
16. import org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse;
17. import org.elasticsearch.action.admin.cluster.health.ClusterHealthStatus;
18. import org.elasticsearch.client.Client;
19. import org.springframework.beans.factory.annotation.Autowired;
20. import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
21. import org.springframework.data.elasticsearch.core.query.IndexQuery;
22. import org.springframework.data.elasticsearch.core.query.IndexQueryBuilder;
23.
24. import com.sf.heros.mq.consumer.utils.APP;
25. import com.sf.heros.mq.consumer.vo.BrandCaseInfo;
26. import com.sf.heros.mq.consumer.vo.NewsInfo;
27. import com.sf.heros.mq.consumer.vo.TaskInfo;
28. import com.sf.heros.mq.consumer.vo.UserInfo;
29.
30. /**
31. * @author seaphy
32. * @date 2014-5-4
33. */
34. public class ElasticsearchService {
35.
36. private static final Logger logger = Logger.getLogger(ElasticsearchService.class);
37.
38. @Autowired
39. private ElasticsearchTemplate elasticsearchTemplate;
40.
41. @Autowired
42. private Client esClient;
43.
44. public void init() {
45. if (!elasticsearchTemplate.indexExists(APP.ESProp.INDEX_NAME)) {
46. elasticsearchTemplate.createIndex(APP.ESProp.INDEX_NAME);
47. }
48. class);
49. class);
50. }
51.
52. public boolean update(List<TaskInfo> taskInfoList) {
53. new ArrayList<IndexQuery>();
54. for (TaskInfo taskInfo : taskInfoList) {
55. new IndexQueryBuilder().withId(taskInfo.getTaskId()).withObject(taskInfo).build();
56. queries.add(indexQuery);
57. }
58. elasticsearchTemplate.bulkIndex(queries);
59. return true;
60. }
61.
62. public boolean insertOrUpdateTaskInfo(List<TaskInfo> taskInfoList) {
63. new ArrayList<IndexQuery>();
64. for (TaskInfo taskInfo : taskInfoList) {
65. new IndexQueryBuilder().withId(taskInfo.getTaskId()).withObject(taskInfo).build();
66. queries.add(indexQuery);
67. }
68. elasticsearchTemplate.bulkIndex(queries);
69. return true;
70. }
71.
72. public boolean insertOrUpdateNewsInfo(List<NewsInfo> newsInfos) {
73. new ArrayList<IndexQuery>();
74. for (NewsInfo newsInfo : newsInfos) {
75. new IndexQueryBuilder().withId(newsInfo.getNewsId()).withObject(newsInfo).build();
76. queries.add(indexQuery);
77. }
78. elasticsearchTemplate.bulkIndex(queries);
79. return true;
80. }
81.
82. public boolean insertOrUpdateNewsInfo(NewsInfo newsInfo) {
83. try {
84. new IndexQueryBuilder().withId(newsInfo.getNewsId()).withObject(newsInfo).build();
85. elasticsearchTemplate.index(indexQuery);
86. return true;
87. catch (Exception e) {
88. "insert or update news info error.", e);
89. return false;
90. }
91. }
92.
93. public boolean insertOrUpdateTaskInfo(TaskInfo taskInfo) {
94. try {
95. new IndexQueryBuilder().withId(taskInfo.getTaskId()).withObject(taskInfo).build();
96. elasticsearchTemplate.index(indexQuery);
97. return true;
98. catch (Exception e) {
99. "insert or update task info error.", e);
100. return false;
101. }
102. }
103.
104. public boolean insertOrUpdateUserInfo(UserInfo userInfo) {
105. try {
106. new IndexQueryBuilder().withId(userInfo.getUserId()).withObject(userInfo).build();
107. elasticsearchTemplate.index(indexQuery);
108. return true;
109. catch (Exception e) {
110. "insert or update user info error.", e);
111. return false;
112. }
113. }
114.
115. public <T> boolean deleteById(String id, Class<T> clzz) {
116. try {
117. elasticsearchTemplate.delete(clzz, id);
118. return true;
119. catch (Exception e) {
120. "delete " + clzz + " by id " + id + " error.", e);
121. return false;
122. }
123. }
124.
125. /**
126. * 检查健康状态
127. * @author 高国藩
128. * @date 2015年6月15日 下午6:59:47
129. * @return
130. */
131. public boolean ping() {
132. try {
133. new ClusterHealthRequest());
134. ClusterHealthStatus status = health.actionGet().getStatus();
135. if (status.value() == ClusterHealthStatus.RED.value()) {
136. throw new RuntimeException("elasticsearch cluster health status is red.");
137. }
138. return true;
139. catch (Exception e) {
140. "ping elasticsearch error.", e);
141. return false;
142. }
143. }
144.
145. public boolean insertOrUpdateBrandCaseInfo(BrandCaseInfo brandCaseInfo) {
146. try {
147. new IndexQueryBuilder()
148. .withId(brandCaseInfo.getId()).withObject(brandCaseInfo).build();
149. elasticsearchTemplate.index(indexQuery);
150. return true;
151. catch (Exception e) {
152. "insert or update brandcase info error.", e);
153. return false;
154. }
155. }
156. }
查询类
1. package com.sf.daidongxi.web.service;
2.
3. import java.util.ArrayList;
4. import java.util.Collection;
5. import java.util.List;
6. import java.util.Map;
7.
8. import org.apache.commons.lang.StringUtils;
9. import org.apache.log4j.Logger;
10. import org.apache.lucene.queries.TermFilter;
11. import org.apache.lucene.queryparser.xml.builders.FilteredQueryBuilder;
12. import org.elasticsearch.action.search.SearchRequestBuilder;
13. import org.elasticsearch.action.search.SearchResponse;
14. import org.elasticsearch.action.search.SearchType;
15. import org.elasticsearch.client.Client;
16. import org.elasticsearch.index.query.BoolFilterBuilder;
17. import org.elasticsearch.index.query.FilterBuilder;
18. import org.elasticsearch.index.query.FilterBuilders;
19. import org.elasticsearch.index.query.MatchQueryBuilder;
20. import org.elasticsearch.index.query.QueryBuilder;
21. import org.elasticsearch.index.query.QueryBuilders;
22. import org.elasticsearch.index.query.QueryStringQueryBuilder;
23. import org.elasticsearch.index.query.RangeFilterBuilder;
24. import org.elasticsearch.index.query.TermsQueryBuilder;
25. import org.elasticsearch.search.SearchHit;
26. import org.elasticsearch.search.sort.SortOrder;
27. import org.springframework.beans.factory.InitializingBean;
28. import org.springframework.beans.factory.annotation.Autowired;
29. import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
30.
31. import sun.misc.Contended;
32.
33. public class ElasticsearchService implements InitializingBean {
34.
35. private static final Logger logger = Logger
36. class);
37.
38. @Autowired
39. private Client client;
40.
41. private String esIndexName = "heros";
42.
43. @Autowired
44. private ElasticsearchTemplate elasticsearchTemplate;
45.
46. @Autowired
47. private Client esClient;
48.
49. /** 查询 id */
50. public List<String> queryId(String type, String[] fields, String content,
51. int from, int size) {
52. SearchRequestBuilder reqBuilder = client.prepareSearch(esIndexName)
53. .setTypes(type).setSearchType(SearchType.DEFAULT)
54. true);
55. "\""
56. "\"");
57. for (String k : fields) {
58. queryString.field(k);
59. }
60. "10");
61. reqBuilder.setQuery(QueryBuilders.boolQuery().should(queryString))
62. true);
63. if (StringUtils.isNotEmpty(sortField) && order != null) {
64. reqBuilder.addSort(sortField, order);
65. }
66. if (from >= 0 && size > 0) {
67. reqBuilder.setFrom(from).setSize(size);
68. }
69. SearchResponse resp = reqBuilder.execute().actionGet();
70. SearchHit[] hits = resp.getHits().getHits();
71. new ArrayList<String>();
72. for (SearchHit hit : hits) {
73. results.add(hit.getId());
74. }
75. return results;
76. }
77.
78. /**
79. * 查询得到结果为Map集合
80. *
81. * @author 高国藩
82. * @date 2015年6月15日 下午8:46:13
83. * @param type
84. * 表
85. * @param fields
86. * 字段索引
87. * @param content
88. * 查询的值
89. * @param sortField
90. * 排序的字段
91. * @param order
92. * 排序的規則
93. * @param from
94. * 分頁
95. * @param size
96. * @return
97. */
98. public List<Map<String, Object>> queryForObject(String type,
99. String[] fields, String content, String sortField, SortOrder order,
100. int from, int size) {
101. SearchRequestBuilder reqBuilder = client.prepareSearch(esIndexName)
102. .setTypes(type).setSearchType(SearchType.DEFAULT)
103. true);
104. "\""
105. "\"");
106. for (String k : fields) {
107. queryString.field(k);
108. }
109. "10");
110. reqBuilder.setQuery(QueryBuilders.boolQuery().should(queryString))
111. true);
112. if (StringUtils.isNotEmpty(sortField) && order != null) {
113. reqBuilder.addSort(sortField, order);
114. }
115. if (from >= 0 && size > 0) {
116. reqBuilder.setFrom(from).setSize(size);
117. }
118.
119. SearchResponse resp = reqBuilder.execute().actionGet();
120. SearchHit[] hits = resp.getHits().getHits();
121.
122. new ArrayList<Map<String, Object>>();
123. for (SearchHit hit : hits) {
124. results.add(hit.getSource());
125. }
126. return results;
127. }
128.
129. /**
130. * QueryBuilders 所有查询入口
131. */
132. public List<Map<String, Object>> queryForObjectEq(String type,
133. String[] fields, String content, String sortField, SortOrder order,
134. int from, int size) {
135. SearchRequestBuilder reqBuilder = client.prepareSearch(esIndexName)
136. .setTypes(type).setSearchType(SearchType.DEFAULT)
137. true);
138. "\""
139. "\"");
140. for (String k : fields) {
141. queryString.field(k);
142. }
143. "10");
144. reqBuilder.setQuery(QueryBuilders.boolQuery().must(queryString))
145. true);
146. if (StringUtils.isNotEmpty(sortField) && order != null) {
147. reqBuilder.addSort(sortField, order);
148. }
149. if (from >= 0 && size > 0) {
150. reqBuilder.setFrom(from).setSize(size);
151. }
152.
153. SearchResponse resp = reqBuilder.execute().actionGet();
154. SearchHit[] hits = resp.getHits().getHits();
155.
156. new ArrayList<Map<String, Object>>();
157. for (SearchHit hit : hits) {
158. results.add(hit.getSource());
159. }
160. return results;
161. }
162.
163. /**
164. * 多个文字记不清是那些字,然后放进去查询
165. *
166. * @author 高国藩
167. * @date 2015年6月16日 上午9:56:08
168. * @param type
169. * @param field
170. * @param countents
171. * @param sortField
172. * @param order
173. * @param from
174. * @param size
175. * @return
176. */
177. public List<Map<String, Object>> queryForObjectNotEq(String type,
178. String field, Collection<String> countents, String sortField,
179. int from, int size) {
180.
181. SearchRequestBuilder reqBuilder = client.prepareSearch(esIndexName)
182. .setTypes(type).setSearchType(SearchType.DEFAULT)
183. true);
184. new ArrayList<String>();
185. for (String content : countents) {
186. "\"" + content + "\"");
187. }
188. TermsQueryBuilder inQuery = QueryBuilders.inQuery(field, contents);
189. "10");
190. reqBuilder.setQuery(QueryBuilders.boolQuery().mustNot(inQuery))
191. true);
192. if (StringUtils.isNotEmpty(sortField) && order != null) {
193. reqBuilder.addSort(sortField, order);
194. }
195. if (from >= 0 && size > 0) {
196. reqBuilder.setFrom(from).setSize(size);
197. }
198.
199. SearchResponse resp = reqBuilder.execute().actionGet();
200. SearchHit[] hits = resp.getHits().getHits();
201.
202. new ArrayList<Map<String, Object>>();
203. for (SearchHit hit : hits) {
204. results.add(hit.getSource());
205. }
206. return results;
207. }
208.
209. /**
210. * Filters 查询方式
211. *
212. * 1. 1)QueryBuilders.queryString 获得基本查询
213. * 2)FilteredQueryBuilder query = QueryBuilders.filteredQuery(queryString,FilterBuilder)
214. * 3)通过上面封装成为查询,将这个query插入到reqBuilder中;完成操作
215. *
216. * 2.在 reqBuilder.setQuery(query);
217. *
218. * 3.介绍在2)中的FilterBuilder各种构造方式-参数都可以传String类型即可
219. * FilterBuilders.rangeFilter("taskState").lt(20) 小于 、 lte(20) 小于等于
220. * FilterBuilders.rangeFilter("taskState").gt(20)) 大于 、 gte(20) 大于等于
221. * FilterBuilders.rangeFilter("taskState").from(start).to(end)) 范围,也可以指定日期,用字符串就ok了
222. * @author 高国藩
223. * @date 2015年6月15日 下午10:06:05
224. * @param type
225. * @param field
226. * @param countents
227. * @param sortField
228. * @param order
229. * @param from
230. * @param size
231. * @return
232. */
233. public List<Map<String, Object>> queryForObjectForElasticSerch(String type,
234. int start,int end) {
235.
236. SearchRequestBuilder reqBuilder = client.prepareSearch(esIndexName)
237. .setTypes(type).setSearchType(SearchType.DEFAULT)
238. true);
239. "\""
240. "\"");
241. queryString.field(field);
242. "10");
243.
244. "taskState").from(start).to(end)))
245. true);
246.
247. SearchResponse resp = reqBuilder.execute().actionGet();
248. SearchHit[] hits = resp.getHits().getHits();
249.
250. new ArrayList<Map<String, Object>>();
251. for (SearchHit hit : hits) {
252. results.add(hit.getSource());
253. }
254. return results;
255. }
256.
257. public void afterPropertiesSet() throws Exception {
258. "init...");
259.
260. }
261.
262. }
测试
1. package com.sf.heros.mq.consumer;
2.
3. import java.util.ArrayList;
4. import java.util.Collection;
5. import java.util.HashSet;
6. import java.util.List;
7. import java.util.Map;
8.
9. import org.apache.log4j.Logger;
10. import org.elasticsearch.search.sort.SortOrder;
11. import org.junit.Test;
12. import org.springframework.context.support.ClassPathXmlApplicationContext;
13.
14. import com.sf.heros.mq.consumer.service.ElasticsearchService;
15. import com.sf.heros.mq.consumer.utils.APP;
16. import com.sf.heros.mq.consumer.vo.TaskInfo;
17.
18. public class AppMain {
19.
20. private static final Logger logger = Logger.getLogger(AppMain.class);
21.
22. public void start() {
23. null;
24. try {
25. new ClassPathXmlApplicationContext("classpath:app.xml");
26. catch (Exception e) {
27. "An error occurred, applicationContext will close.", e);
28. if (context != null) {
29. context.close();
30. }
31. null;
32. logger.error(APP.CLOSED_MSG);
33. }
34. }
35.
36. /**
37. * 插入
38. * @author 高国藩
39. * @date 2015年6月16日 上午10:14:21
40. */
41. @Test
42. public void insertNo() {
43. new ClassPathXmlApplicationContext(
44. "classpath:app.xml");
45. ElasticsearchService service = context
46. class);
47. new ArrayList<TaskInfo>();
48. for (int i = 0; i < 20; i++) {
49. new TaskInfo(String.valueOf((i + 5)), i + 5, "高国藩"
50. "taskArea", "taskTags", i + 5, "1996-02-03", "霍华德"));
51. }
52. service.insertOrUpdateTaskInfo(taskInfoList);
53. }
54.
55. /**
56. * 查询
57. * @author 高国藩
58. * @date 2015年6月16日 上午10:14:21
59. */
60. @Test
61. public void serchNo() {
62. new ClassPathXmlApplicationContext(
63. "classpath:app.xml");
64. com.sf.daidongxi.web.service.ElasticsearchService service = (com.sf.daidongxi.web.service.ElasticsearchService) context
65. "es");
66. "task_info",
67. new String[] { "taskContent", "taskArea" }, "高国藩", "taskArea", SortOrder.DESC,
68. 0, 2);
69.
70. for (int i = 0; i < al.size(); i++) {
71. System.out.println(al.get(i));
72. }
73.
74. }
75.
76. /**
77. * filter查询
78. * @author 高国藩
79. * @date 2015年6月16日 上午10:14:21
80. */
81. @Test
82. public void serchFilter() {
83. new ClassPathXmlApplicationContext(
84. "classpath:app.xml");
85. com.sf.daidongxi.web.service.ElasticsearchService service = (com.sf.daidongxi.web.service.ElasticsearchService) context
86. "es");
87. "task_info", "taskContent", "高",19,20);
88.
89. for (int i = 0; i < al.size(); i++) {
90. System.out.println(al.get(i));
91. }
92.
93. }
94. }