项目清单

 

elasticsearch服务下载包括其中插件和分词

 
http://download.csdn.net/detail/u014201191/8809619
 

项目源码

 

资源文件

 

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. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:elasticsearch="http://www.pilato.fr/schema/elasticsearch"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  7. http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd
  8. http://www.pilato.fr/schema/elasticsearch http://www.pilato.fr/schema/elasticsearch/elasticsearch-0.3.xsd
  9. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">
  10. <context:annotation-config />
  11. <!-- 自动扫描所有注解该路径 -->
  12. <!-- <context:component-scan base-package="com.sf.heros.mq.*" /> -->
  13. <context:property-placeholder location="classpath:/app.properties" />
  14. <import resource="elasticseach.xml" />
  15. </beans>

elasticseach.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:elasticsearch="http://www.pilato.fr/schema/elasticsearch"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  7. http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd
  8. http://www.pilato.fr/schema/elasticsearch http://www.pilato.fr/schema/elasticsearch/elasticsearch-0.3.xsd
  9. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">
  10. <util:map id="esproperties">
  11. <entry key="cluster.name" value="${elasticsearch.cluster.name}" />
  12. </util:map>
  13. <elasticsearch:client id="client" properties="esproperties"
  14. esNodes="${elasticsearch.esNodes}" />
  15. <bean name="elasticsearchTemplate"
  16. class="org.springframework.data.elasticsearch.core.ElasticsearchTemplate">
  17. <constructor-arg name="client" ref="client" />
  18. </bean>
  19. <bean name="elasticsearchService" class="com.sf.heros.mq.consumer.service.ElasticsearchService"
  20. init-method="init" />
  21. <bean name="es" class="com.sf.daidongxi.web.service.ElasticsearchService"></bean>
  22. </beans>

maven

  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  3. <modelVersion>4.0.0</modelVersion>
  4. <groupId>com.elasticsearch</groupId>
  5. <artifactId>elasticsearch</artifactId>
  6. <packaging>war</packaging>
  7. <version>0.0.1-SNAPSHOT</version>
  8. <name>elasticsearch Maven Webapp</name>
  9. <url>http://maven.apache.org</url>
  10. <properties>
  11. <spring.version>3.1.1.RELEASE</spring.version>
  12. <findbugs.annotations>2.0.0</findbugs.annotations>
  13. <checkstyle.maven.plugin>2.11</checkstyle.maven.plugin>
  14. <pmd.maven.plugin>3.0</pmd.maven.plugin>
  15. <findbugs.maven.plugin>2.5.3</findbugs.maven.plugin>
  16. <java.version>1.7</java.version>
  17. </properties>
  18. <dependencies>
  19. <dependency>
  20. <groupId>junit</groupId>
  21. <artifactId>junit</artifactId>
  22. <version>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. <dependency>
  37. <groupId>org.springframework</groupId>
  38. <artifactId>spring-aop</artifactId>
  39. <version>${spring.version}</version>
  40. </dependency>
  41. <dependency>
  42. <groupId>org.springframework</groupId>
  43. <artifactId>spring-core</artifactId>
  44. <version>${spring.version}</version>
  45. </dependency>
  46. <dependency>
  47. <groupId>org.springframework</groupId>
  48. <artifactId>spring-jdbc</artifactId>
  49. <version>${spring.version}</version>
  50. </dependency>
  51. <!-- spring end -->
  52. <!-- elasticsearch package -->
  53. <dependency>
  54. <groupId>fr.pilato.spring</groupId>
  55. <artifactId>spring-elasticsearch</artifactId>
  56. <version>1.0.0</version>
  57. </dependency>
  58. <dependency>
  59. <groupId>org.elasticsearch</groupId>
  60. <artifactId>elasticsearch</artifactId>
  61. <version>1.0.0</version>
  62. </dependency>
  63. <dependency>
  64. <groupId>org.springframework.data</groupId>
  65. <artifactId>spring-data-elasticsearch</artifactId>
  66. <version>1.0.0.RELEASE</version>
  67. </dependency>
  68. <dependency>
  69. <groupId>com.alibaba</groupId>
  70. <artifactId>druid</artifactId>
  71. <version>1.0.5</version>
  72. </dependency>
  73. <!--json-lib -->
  74. <dependency>
  75. <groupId>net.sf.json-lib</groupId>
  76. <artifactId>json-lib</artifactId>
  77. <version>2.4</version>
  78. <classifier>jdk15</classifier>
  79. </dependency>
  80. <!-- quartz job -->
  81. <dependency>
  82. <groupId>org.quartz-scheduler</groupId>
  83. <artifactId>quartz</artifactId>
  84. <version>2.2.1</version>
  85. </dependency>
  86. <!-- log4j -->
  87. <dependency>
  88. <groupId>org.slf4j</groupId>
  89. <artifactId>slf4j-log4j12</artifactId>
  90. <version>1.7.5</version>
  91. </dependency>
  92. </dependencies>
  93. <build>
  94. <finalName>elasticsearch</finalName>
  95. </build>
  96. </project>

Java.class

 

Bean配置

  1. package com.sf.heros.mq.consumer.vo;
  2. import org.springframework.data.annotation.Id;
  3. import org.springframework.data.elasticsearch.annotations.Document;
  4. import org.springframework.data.elasticsearch.annotations.Field;
  5. import org.springframework.data.elasticsearch.annotations.FieldIndex;
  6. import org.springframework.data.elasticsearch.annotations.FieldType;
  7. import com.sf.heros.mq.consumer.utils.APP;
  8. //@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)
  9. @Document(indexName = APP.ESProp.INDEX_NAME, type = APP.ESProp.TYPE_TASK_INFO)
  10. public class TaskInfo {
  11. @Id
  12. @Field(index = FieldIndex.not_analyzed, store = true)
  13. private String taskId;
  14. @Field(type = FieldType.Integer, index = FieldIndex.not_analyzed, store = true)
  15. private Integer userId;
  16. @Field(type = FieldType.String, indexAnalyzer="ik", searchAnalyzer="ik", store = true)
  17. private String taskContent;
  18. @Field(type = FieldType.String, indexAnalyzer="ik", searchAnalyzer="ik", store = true)
  19. private String taskArea;
  20. @Field(type = FieldType.String, indexAnalyzer="ik", searchAnalyzer="ik", store = true)
  21. private String taskTags;
  22. @Field(type = FieldType.Integer, index = FieldIndex.not_analyzed, store = true)
  23. private Integer taskState;
  24. @Field(type = FieldType.String, index = FieldIndex.not_analyzed, store = true)
  25. private String updateTime;
  26. @Field(type = FieldType.String, indexAnalyzer="ik", searchAnalyzer="ik", store = true)
  27. private String userNickName;
  28. public String getTaskId() {
  29. return taskId;
  30. }
  31. public void setTaskId(String taskId) {
  32. this.taskId = taskId;
  33. }
  34. public Integer getUserId() {
  35. return userId;
  36. }
  37. public void setUserId(Integer userId) {
  38. this.userId = userId;
  39. }
  40. public String getTaskContent() {
  41. return taskContent;
  42. }
  43. public void setTaskContent(String taskContent) {
  44. this.taskContent = taskContent;
  45. }
  46. public String getTaskArea() {
  47. return taskArea;
  48. }
  49. public void setTaskArea(String taskArea) {
  50. this.taskArea = taskArea;
  51. }
  52. public String getTaskTags() {
  53. return taskTags;
  54. }
  55. public void setTaskTags(String taskTags) {
  56. this.taskTags = taskTags;
  57. }
  58. public Integer getTaskState() {
  59. return taskState;
  60. }
  61. public void setTaskState(Integer taskState) {
  62. this.taskState = taskState;
  63. }
  64. public String getUpdateTime() {
  65. return updateTime;
  66. }
  67. public void setUpdateTime(String updateTime) {
  68. this.updateTime = updateTime;
  69. }
  70. public String getUserNickName() {
  71. return userNickName;
  72. }
  73. public void setUserNickName(String userNickName) {
  74. this.userNickName = userNickName;
  75. }
  76. @Override
  77. public String toString() {
  78. return "TaskInfo [taskId=" + taskId + ", userId=" + userId
  79. + ", taskContent=" + taskContent + ", taskArea=" + taskArea
  80. + ", taskState=" + taskState
  81. + ", updateTime=" + updateTime + ", userNickName="
  82. + userNickName + "]";
  83. }
  84. public TaskInfo(String taskId, Integer userId, String taskContent,
  85. String taskArea, String taskTags, Integer taskState,
  86. String updateTime, String userNickName) {
  87. this.taskId = taskId;
  88. this.userId = userId;
  89. this.taskContent = taskContent;
  90. this.taskArea = taskArea;
  91. this.taskTags = taskTags;
  92. this.taskState = taskState;
  93. this.updateTime = updateTime;
  94. this.userNickName = userNickName;
  95. }
  96. public TaskInfo() {
  97. // TODO Auto-generated constructor stub
  98. }
  99. }

增删改类

  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. import java.util.ArrayList;
  10. import java.util.List;
  11. import org.apache.log4j.Logger;
  12. import org.elasticsearch.action.ActionFuture;
  13. import org.elasticsearch.action.admin.cluster.health.ClusterHealthRequest;
  14. import org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse;
  15. import org.elasticsearch.action.admin.cluster.health.ClusterHealthStatus;
  16. import org.elasticsearch.client.Client;
  17. import org.springframework.beans.factory.annotation.Autowired;
  18. import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
  19. import org.springframework.data.elasticsearch.core.query.IndexQuery;
  20. import org.springframework.data.elasticsearch.core.query.IndexQueryBuilder;
  21. import com.sf.heros.mq.consumer.utils.APP;
  22. import com.sf.heros.mq.consumer.vo.BrandCaseInfo;
  23. import com.sf.heros.mq.consumer.vo.NewsInfo;
  24. import com.sf.heros.mq.consumer.vo.TaskInfo;
  25. import com.sf.heros.mq.consumer.vo.UserInfo;
  26. /**
  27. * @author seaphy
  28. * @date 2014-5-4
  29. */
  30. public class ElasticsearchService {
  31. private static final Logger logger = Logger.getLogger(ElasticsearchService.class);
  32. @Autowired
  33. private ElasticsearchTemplate elasticsearchTemplate;
  34. @Autowired
  35. private Client esClient;
  36. public void init() {
  37. if (!elasticsearchTemplate.indexExists(APP.ESProp.INDEX_NAME)) {
  38. elasticsearchTemplate.createIndex(APP.ESProp.INDEX_NAME);
  39. }
  40. elasticsearchTemplate.putMapping(TaskInfo.class);
  41. elasticsearchTemplate.putMapping(NewsInfo.class);
  42. }
  43. public boolean update(List<TaskInfo> taskInfoList) {
  44. List<IndexQuery> queries = new ArrayList<IndexQuery>();
  45. for (TaskInfo taskInfo : taskInfoList) {
  46. IndexQuery indexQuery = new IndexQueryBuilder().withId(taskInfo.getTaskId()).withObject(taskInfo).build();
  47. queries.add(indexQuery);
  48. }
  49. elasticsearchTemplate.bulkIndex(queries);
  50. return true;
  51. }
  52. public boolean insertOrUpdateTaskInfo(List<TaskInfo> taskInfoList) {
  53. List<IndexQuery> queries = new ArrayList<IndexQuery>();
  54. for (TaskInfo taskInfo : taskInfoList) {
  55. IndexQuery indexQuery = new IndexQueryBuilder().withId(taskInfo.getTaskId()).withObject(taskInfo).build();
  56. queries.add(indexQuery);
  57. }
  58. elasticsearchTemplate.bulkIndex(queries);
  59. return true;
  60. }
  61. public boolean insertOrUpdateNewsInfo(List<NewsInfo> newsInfos) {
  62. List<IndexQuery> queries = new ArrayList<IndexQuery>();
  63. for (NewsInfo newsInfo : newsInfos) {
  64. IndexQuery indexQuery = new IndexQueryBuilder().withId(newsInfo.getNewsId()).withObject(newsInfo).build();
  65. queries.add(indexQuery);
  66. }
  67. elasticsearchTemplate.bulkIndex(queries);
  68. return true;
  69. }
  70. public boolean insertOrUpdateNewsInfo(NewsInfo newsInfo) {
  71. try {
  72. IndexQuery indexQuery = new IndexQueryBuilder().withId(newsInfo.getNewsId()).withObject(newsInfo).build();
  73. elasticsearchTemplate.index(indexQuery);
  74. return true;
  75. } catch (Exception e) {
  76. logger.error("insert or update news info error.", e);
  77. return false;
  78. }
  79. }
  80. public boolean insertOrUpdateTaskInfo(TaskInfo taskInfo) {
  81. try {
  82. IndexQuery indexQuery = new IndexQueryBuilder().withId(taskInfo.getTaskId()).withObject(taskInfo).build();
  83. elasticsearchTemplate.index(indexQuery);
  84. return true;
  85. } catch (Exception e) {
  86. logger.error("insert or update task info error.", e);
  87. return false;
  88. }
  89. }
  90. public boolean insertOrUpdateUserInfo(UserInfo userInfo) {
  91. try {
  92. IndexQuery indexQuery = new IndexQueryBuilder().withId(userInfo.getUserId()).withObject(userInfo).build();
  93. elasticsearchTemplate.index(indexQuery);
  94. return true;
  95. } catch (Exception e) {
  96. logger.error("insert or update user info error.", e);
  97. return false;
  98. }
  99. }
  100. public <T> boolean deleteById(String id, Class<T> clzz) {
  101. try {
  102. elasticsearchTemplate.delete(clzz, id);
  103. return true;
  104. } catch (Exception e) {
  105. logger.error("delete " + clzz + " by id " + id + " error.", e);
  106. return false;
  107. }
  108. }
  109. /**
  110. * 检查健康状态
  111. * @author 高国藩
  112. * @date 2015年6月15日 下午6:59:47
  113. * @return
  114. */
  115. public boolean ping() {
  116. try {
  117. ActionFuture<ClusterHealthResponse> health = esClient.admin().cluster().health(new ClusterHealthRequest());
  118. ClusterHealthStatus status = health.actionGet().getStatus();
  119. if (status.value() == ClusterHealthStatus.RED.value()) {
  120. throw new RuntimeException("elasticsearch cluster health status is red.");
  121. }
  122. return true;
  123. } catch (Exception e) {
  124. logger.error("ping elasticsearch error.", e);
  125. return false;
  126. }
  127. }
  128. public boolean insertOrUpdateBrandCaseInfo(BrandCaseInfo brandCaseInfo) {
  129. try {
  130. IndexQuery indexQuery = new IndexQueryBuilder()
  131. .withId(brandCaseInfo.getId()).withObject(brandCaseInfo).build();
  132. elasticsearchTemplate.index(indexQuery);
  133. return true;
  134. } catch (Exception e) {
  135. logger.error("insert or update brandcase info error.", e);
  136. return false;
  137. }
  138. }
  139. }

查询类

  1. package com.sf.daidongxi.web.service;
  2. import java.util.ArrayList;
  3. import java.util.Collection;
  4. import java.util.List;
  5. import java.util.Map;
  6. import org.apache.commons.lang.StringUtils;
  7. import org.apache.log4j.Logger;
  8. import org.apache.lucene.queries.TermFilter;
  9. import org.apache.lucene.queryparser.xml.builders.FilteredQueryBuilder;
  10. import org.elasticsearch.action.search.SearchRequestBuilder;
  11. import org.elasticsearch.action.search.SearchResponse;
  12. import org.elasticsearch.action.search.SearchType;
  13. import org.elasticsearch.client.Client;
  14. import org.elasticsearch.index.query.BoolFilterBuilder;
  15. import org.elasticsearch.index.query.FilterBuilder;
  16. import org.elasticsearch.index.query.FilterBuilders;
  17. import org.elasticsearch.index.query.MatchQueryBuilder;
  18. import org.elasticsearch.index.query.QueryBuilder;
  19. import org.elasticsearch.index.query.QueryBuilders;
  20. import org.elasticsearch.index.query.QueryStringQueryBuilder;
  21. import org.elasticsearch.index.query.RangeFilterBuilder;
  22. import org.elasticsearch.index.query.TermsQueryBuilder;
  23. import org.elasticsearch.search.SearchHit;
  24. import org.elasticsearch.search.sort.SortOrder;
  25. import org.springframework.beans.factory.InitializingBean;
  26. import org.springframework.beans.factory.annotation.Autowired;
  27. import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
  28. import sun.misc.Contended;
  29. public class ElasticsearchService implements InitializingBean {
  30. private static final Logger logger = Logger
  31. .getLogger(ElasticsearchService.class);
  32. @Autowired
  33. private Client client;
  34. private String esIndexName = "heros";
  35. @Autowired
  36. private ElasticsearchTemplate elasticsearchTemplate;
  37. @Autowired
  38. private Client esClient;
  39. /** 查询 id */
  40. public List<String> queryId(String type, String[] fields, String content,
  41. String sortField, SortOrder order, int from, int size) {
  42. SearchRequestBuilder reqBuilder = client.prepareSearch(esIndexName)
  43. .setTypes(type).setSearchType(SearchType.DEFAULT)
  44. .setExplain(true);
  45. QueryStringQueryBuilder queryString = QueryBuilders.queryString("\""
  46. + content + "\"");
  47. for (String k : fields) {
  48. queryString.field(k);
  49. }
  50. queryString.minimumShouldMatch("10");
  51. reqBuilder.setQuery(QueryBuilders.boolQuery().should(queryString))
  52. .setExplain(true);
  53. if (StringUtils.isNotEmpty(sortField) && order != null) {
  54. reqBuilder.addSort(sortField, order);
  55. }
  56. if (from >= 0 && size > 0) {
  57. reqBuilder.setFrom(from).setSize(size);
  58. }
  59. SearchResponse resp = reqBuilder.execute().actionGet();
  60. SearchHit[] hits = resp.getHits().getHits();
  61. ArrayList<String> results = new ArrayList<String>();
  62. for (SearchHit hit : hits) {
  63. results.add(hit.getId());
  64. }
  65. return results;
  66. }
  67. /**
  68. * 查询得到结果为Map集合
  69. *
  70. * @author 高国藩
  71. * @date 2015年6月15日 下午8:46:13
  72. * @param type
  73. *            表
  74. * @param fields
  75. *            字段索引
  76. * @param content
  77. *            查询的值
  78. * @param sortField
  79. *            排序的字段
  80. * @param order
  81. *            排序的規則
  82. * @param from
  83. *            分頁
  84. * @param size
  85. * @return
  86. */
  87. public List<Map<String, Object>> queryForObject(String type,
  88. String[] fields, String content, String sortField, SortOrder order,
  89. int from, int size) {
  90. SearchRequestBuilder reqBuilder = client.prepareSearch(esIndexName)
  91. .setTypes(type).setSearchType(SearchType.DEFAULT)
  92. .setExplain(true);
  93. QueryStringQueryBuilder queryString = QueryBuilders.queryString("\""
  94. + content + "\"");
  95. for (String k : fields) {
  96. queryString.field(k);
  97. }
  98. queryString.minimumShouldMatch("10");
  99. reqBuilder.setQuery(QueryBuilders.boolQuery().should(queryString))
  100. .setExplain(true);
  101. if (StringUtils.isNotEmpty(sortField) && order != null) {
  102. reqBuilder.addSort(sortField, order);
  103. }
  104. if (from >= 0 && size > 0) {
  105. reqBuilder.setFrom(from).setSize(size);
  106. }
  107. SearchResponse resp = reqBuilder.execute().actionGet();
  108. SearchHit[] hits = resp.getHits().getHits();
  109. List<Map<String, Object>> results = new ArrayList<Map<String, Object>>();
  110. for (SearchHit hit : hits) {
  111. results.add(hit.getSource());
  112. }
  113. return results;
  114. }
  115. /**
  116. * QueryBuilders 所有查询入口
  117. */
  118. public List<Map<String, Object>> queryForObjectEq(String type,
  119. String[] fields, String content, String sortField, SortOrder order,
  120. int from, int size) {
  121. SearchRequestBuilder reqBuilder = client.prepareSearch(esIndexName)
  122. .setTypes(type).setSearchType(SearchType.DEFAULT)
  123. .setExplain(true);
  124. QueryStringQueryBuilder queryString = QueryBuilders.queryString("\""
  125. + content + "\"");
  126. for (String k : fields) {
  127. queryString.field(k);
  128. }
  129. queryString.minimumShouldMatch("10");
  130. reqBuilder.setQuery(QueryBuilders.boolQuery().must(queryString))
  131. .setExplain(true);
  132. if (StringUtils.isNotEmpty(sortField) && order != null) {
  133. reqBuilder.addSort(sortField, order);
  134. }
  135. if (from >= 0 && size > 0) {
  136. reqBuilder.setFrom(from).setSize(size);
  137. }
  138. SearchResponse resp = reqBuilder.execute().actionGet();
  139. SearchHit[] hits = resp.getHits().getHits();
  140. List<Map<String, Object>> results = new ArrayList<Map<String, Object>>();
  141. for (SearchHit hit : hits) {
  142. results.add(hit.getSource());
  143. }
  144. return results;
  145. }
  146. /**
  147. * 多个文字记不清是那些字,然后放进去查询
  148. *
  149. * @author 高国藩
  150. * @date 2015年6月16日 上午9:56:08
  151. * @param type
  152. * @param field
  153. * @param countents
  154. * @param sortField
  155. * @param order
  156. * @param from
  157. * @param size
  158. * @return
  159. */
  160. public List<Map<String, Object>> queryForObjectNotEq(String type,
  161. String field, Collection<String> countents, String sortField,
  162. SortOrder order, int from, int size) {
  163. SearchRequestBuilder reqBuilder = client.prepareSearch(esIndexName)
  164. .setTypes(type).setSearchType(SearchType.DEFAULT)
  165. .setExplain(true);
  166. List<String> contents = new ArrayList<String>();
  167. for (String content : countents) {
  168. contents.add("\"" + content + "\"");
  169. }
  170. TermsQueryBuilder inQuery = QueryBuilders.inQuery(field, contents);
  171. inQuery.minimumShouldMatch("10");
  172. reqBuilder.setQuery(QueryBuilders.boolQuery().mustNot(inQuery))
  173. .setExplain(true);
  174. if (StringUtils.isNotEmpty(sortField) && order != null) {
  175. reqBuilder.addSort(sortField, order);
  176. }
  177. if (from >= 0 && size > 0) {
  178. reqBuilder.setFrom(from).setSize(size);
  179. }
  180. SearchResponse resp = reqBuilder.execute().actionGet();
  181. SearchHit[] hits = resp.getHits().getHits();
  182. List<Map<String, Object>> results = new ArrayList<Map<String, Object>>();
  183. for (SearchHit hit : hits) {
  184. results.add(hit.getSource());
  185. }
  186. return results;
  187. }
  188. /**
  189. * Filters 查询方式
  190. *
  191. * 1. 1)QueryBuilders.queryString 获得基本查询
  192. *    2)FilteredQueryBuilder query = QueryBuilders.filteredQuery(queryString,FilterBuilder)
  193. *    3)通过上面封装成为查询,将这个query插入到reqBuilder中;完成操作
  194. *
  195. * 2.在   reqBuilder.setQuery(query);
  196. *
  197. * 3.介绍在2)中的FilterBuilder各种构造方式-参数都可以传String类型即可
  198. * FilterBuilders.rangeFilter("taskState").lt(20) 小于 、 lte(20) 小于等于
  199. * FilterBuilders.rangeFilter("taskState").gt(20)) 大于  、 gte(20) 大于等于
  200. * FilterBuilders.rangeFilter("taskState").from(start).to(end)) 范围,也可以指定日期,用字符串就ok了
  201. * @author 高国藩
  202. * @date 2015年6月15日 下午10:06:05
  203. * @param type
  204. * @param field
  205. * @param countents
  206. * @param sortField
  207. * @param order
  208. * @param from
  209. * @param size
  210. * @return
  211. */
  212. public List<Map<String, Object>> queryForObjectForElasticSerch(String type,
  213. String field, String content,int start,int end) {
  214. SearchRequestBuilder reqBuilder = client.prepareSearch(esIndexName)
  215. .setTypes(type).setSearchType(SearchType.DEFAULT)
  216. .setExplain(true);
  217. QueryStringQueryBuilder queryString = QueryBuilders.queryString("\""
  218. + content + "\"");
  219. queryString.field(field);
  220. queryString.minimumShouldMatch("10");
  221. reqBuilder.setQuery(QueryBuilders.filteredQuery(queryString, FilterBuilders.rangeFilter("taskState").from(start).to(end)))
  222. .setExplain(true);
  223. SearchResponse resp = reqBuilder.execute().actionGet();
  224. SearchHit[] hits = resp.getHits().getHits();
  225. List<Map<String, Object>> results = new ArrayList<Map<String, Object>>();
  226. for (SearchHit hit : hits) {
  227. results.add(hit.getSource());
  228. }
  229. return results;
  230. }
  231. public void afterPropertiesSet() throws Exception {
  232. System.out.println("init...");
  233. }
  234. }

测试

  1. package com.sf.heros.mq.consumer;
  2. import java.util.ArrayList;
  3. import java.util.Collection;
  4. import java.util.HashSet;
  5. import java.util.List;
  6. import java.util.Map;
  7. import org.apache.log4j.Logger;
  8. import org.elasticsearch.search.sort.SortOrder;
  9. import org.junit.Test;
  10. import org.springframework.context.support.ClassPathXmlApplicationContext;
  11. import com.sf.heros.mq.consumer.service.ElasticsearchService;
  12. import com.sf.heros.mq.consumer.utils.APP;
  13. import com.sf.heros.mq.consumer.vo.TaskInfo;
  14. public class AppMain {
  15. private static final Logger logger = Logger.getLogger(AppMain.class);
  16. public void start() {
  17. ClassPathXmlApplicationContext context = null;
  18. try {
  19. context = new ClassPathXmlApplicationContext("classpath:app.xml");
  20. } catch (Exception e) {
  21. logger.error("An error occurred, applicationContext will close.", e);
  22. if (context != null) {
  23. context.close();
  24. }
  25. context = null;
  26. logger.error(APP.CLOSED_MSG);
  27. }
  28. }
  29. /**
  30. * 插入
  31. * @author 高国藩
  32. * @date 2015年6月16日 上午10:14:21
  33. */
  34. @Test
  35. public void insertNo() {
  36. ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
  37. "classpath:app.xml");
  38. ElasticsearchService service = context
  39. .getBean(ElasticsearchService.class);
  40. List<TaskInfo> taskInfoList = new ArrayList<TaskInfo>();
  41. for (int i = 0; i < 20; i++) {
  42. taskInfoList.add(new TaskInfo(String.valueOf((i + 5)), i + 5, "高国藩"
  43. + i, "taskArea", "taskTags", i + 5, "1996-02-03", "霍华德"));
  44. }
  45. service.insertOrUpdateTaskInfo(taskInfoList);
  46. }
  47. /**
  48. * 查询
  49. * @author 高国藩
  50. * @date 2015年6月16日 上午10:14:21
  51. */
  52. @Test
  53. public void serchNo() {
  54. ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
  55. "classpath:app.xml");
  56. com.sf.daidongxi.web.service.ElasticsearchService service = (com.sf.daidongxi.web.service.ElasticsearchService) context
  57. .getBean("es");
  58. List<Map<String, Object>> al = service.queryForObject("task_info",
  59. new String[] { "taskContent", "taskArea" }, "高国藩", "taskArea", SortOrder.DESC,
  60. 0, 2);
  61. for (int i = 0; i < al.size(); i++) {
  62. System.out.println(al.get(i));
  63. }
  64. }
  65. /**
  66. * filter查询
  67. * @author 高国藩
  68. * @date 2015年6月16日 上午10:14:21
  69. */
  70. @Test
  71. public void serchFilter() {
  72. ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
  73. "classpath:app.xml");
  74. com.sf.daidongxi.web.service.ElasticsearchService service = (com.sf.daidongxi.web.service.ElasticsearchService) context
  75. .getBean("es");
  76. List<Map<String, Object>> al = service.queryForObjectForElasticSerch("task_info", "taskContent", "高",19,20);
  77. for (int i = 0; i < al.size(); i++) {
  78. System.out.println(al.get(i));
  79. }
  80. }
  81. }

http://download.csdn.net/detail/liyantianmin/9565012

http://blog.csdn.net/liyantianmin/article/details/51801961

Spring Data Elasticsearch的更多相关文章

  1. elasticsearch系列七:ES Java客户端-Elasticsearch Java client(ES Client 简介、Java REST Client、Java Client、Spring Data Elasticsearch)

    一.ES Client 简介 1. ES是一个服务,采用C/S结构 2. 回顾 ES的架构 3. ES支持的客户端连接方式 3.1 REST API ,端口 9200 这种连接方式对应于架构图中的RE ...

  2. Elasticsearch基本用法(2)--Spring Data Elasticsearch

    Spring Data Elasticsearch是Spring Data项目下的一个子模块. 查看 Spring Data的官网:http://projects.spring.io/spring-d ...

  3. Elasticsearch Java client(ES Client 简介、Java REST Client、Java Client、Spring Data Elasticsearch)

    elasticsearch系列七:ES Java客户端-Elasticsearch Java client(ES Client 简介.Java REST Client.Java Client.Spri ...

  4. spring data elasticsearch 使用

    很久之前就安装了elasticsearch,一直没用java用过,最近看了一下spring data系列的elasticsearch,这里写一篇心得. 如果尚未安装elasticsearch,可以 参 ...

  5. Spring Boot + Spring Data + Elasticsearch实例

    Spring Boot + Spring Data + Elasticsearch实例 学习了:https://blog.csdn.net/huangshulang1234/article/detai ...

  6. elasticsearch 拼音+ik分词,spring data elasticsearch 拼音分词

    elasticsearch 自定义分词器 安装拼音分词器.ik分词器 拼音分词器: https://github.com/medcl/elasticsearch-analysis-pinyin/rel ...

  7. How to provide highlighting with Spring data elasticsearch

    How to provide highlighting with Spring data elasticsearch @Test public void shouldReturnHighlighted ...

  8. springboot集成spring data ElasticSearch

    ES支持SpringBoot使用类似于Spring Data Jpa的方式查询,使得查询更加方便. 1.依赖引入 compile “org.springframework.boot:spring-bo ...

  9. 031 Spring Data Elasticsearch学习笔记---重点掌握第5节高级查询和第6节聚合部分

    Elasticsearch提供的Java客户端有一些不太方便的地方: 很多地方需要拼接Json字符串,在java中拼接字符串有多恐怖你应该懂的 需要自己把对象序列化为json存储 查询到结果也需要自己 ...

  10. SpringBoot整合Spring Data Elasticsearch

    Spring Data Elasticsearch提供了ElasticsearchTemplate工具类,实现了POJO与elasticsearch文档之间的映射 elasticsearch本质也是存 ...

随机推荐

  1. Ubuntu 下 JDK+Tomcat+MySql 环境的搭建

    Linux环境 修改catalina.sh 在“echo "Using CATALINA_BASE: $CATALINA_BASE"”上面加入以下行: JAVA_OPTS=&quo ...

  2. [Angualr 2] Watch for changes

    You can watch for form / control changes by using .valueChanges.observe({...}): this.sku.valueChange ...

  3. Java面试求职之==与equals()差别

    Java中equals和==的差别 java中的数据类型,可分为两类:     1.基本数据类型(也称原始数据类型):byte,short,char,int,long,float,double,boo ...

  4. PHP 計算字符串長度函數

    PHP內置的字符串長度函數strlen無法正確處理中文字符串,它得到的只是字符串所占的字節數.對於GB2312的中文編碼,strlen得到的值是漢字個數的2倍,而對於UTF-8編碼的中文,就是3倍的差 ...

  5. Note | javascript权威指南[第六版] 第2章:词法结构

      语法结构规定了诸如变量名是什么样的.怎么写注释,以及程序语句之间如何分隔等规则.本章用很短的篇幅来介绍JavaScript的词法结构.   2.1.字符集   JavaScript程序是用Unic ...

  6. iOS平台基于ffmpeg的视频直播技术揭秘

    现在非常流行直播,相信很多人都跟我一样十分好奇这个技术是如何实现的,正好最近在做一个ffmpeg的项目,发现这个工具很容易就可以做直播,下面来给大家分享下技术要点: 首先你得编译出ffmpeg运行所需 ...

  7. Javascript 获取url参数,hash值 ,cookie

    /** * 获取请求参数 * @param key * @returns {*} */ function getRequestParameter(key){ var params = getReque ...

  8. carousel

    <!DOCTYPE html> <html lang="en" ng-app="mainApp"> <head> <m ...

  9. CSS代码语法

    css 样式由选择符和声明组成,而声明又由属性和值组成,如下图所示: 选择符:又称选择器,指明网页中要应用样式规则的元素,如本例中是网页中所有的段(p)的文字将变成蓝色,而其他的元素(如ol)不会受到 ...

  10. shareSDK微博分享出现: 分享失败: 错误描述:Insufficient app permissions! 错误码:10014

    这个错误是由于appKey所在账号没有微博高级写入接口权限, 需要申请, 详见: http://www.mamicode.com/info-detail-936938.html