elasticsearch spring 集成

elasticsearch spring 集成

项目清单

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

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

项目源码

资源文件

app.properties

[html] view plain copy

print?

  1. elasticsearch.esNodes=localhost:9300
  2. elasticsearch.cluster.name=heroscluster

app.xml

[html] view plain copy

print?

  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

[html] view plain copy

print?

  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>

log4j.properties

[html] view plain copy

print?

  1. ### \u8bbe\u7f6eLogger\u8f93\u51fa\u7ea7\u522b\u548c\u8f93\u51fa\u76ee\u7684\u5730 ###
  2. log4j.rootLogger=info,logfile
  3. log4j.appender.console=org.apache.log4j.ConsoleAppender
  4. log4j.appender.console.Threshold=info
  5. log4j.appender.console.layout=org.apache.log4j.PatternLayout
  6. log4j.appender.console.layout.ConversionPattern=[%-5p] %d{yyyy-MM-dd HH:mm:ss,SSS} - %m%n
  7. log4j.appender.logfile=org.apache.log4j.DailyRollingFileAppender
  8. log4j.appender.logfile.File=/app/logs/mq_consumer.log
  9. log4j.appender.logfile.datePattern=‘.‘yyyy-MM-dd‘.‘
  10. log4j.appender.logfile.append=true
  11. log4j.appender.logfile.Threshold=debug
  12. log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
  13. log4j.appender.logfile.layout.ConversionPattern=[%-5p] %d{yyyy-MM-dd HH:mm:ss,SSS} - %m%n

maven管理

[html] view plain copy

print?

  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配置

[html] view plain copy

print?

  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. }

其余的类在源码中下载,此处不列出了...

常量管理

[html] view plain copy

print?

  1. package com.sf.heros.mq.consumer.utils;
  2. import java.util.HashMap;
  3. import java.util.Map;
  4. public interface APP {
  5. public static final Map<String, String> map = new HashMap<String, String>();
  6. public static final String CLOSED_MSG = "#################closed####################";
  7. public static final long DELIVERIED_TAG = -1;
  8. class ESProp {
  9. public static final String INDEX_NAME = "heros";
  10. public static final String DAIDONGXI_INDEX_NAME = "daidongxi";
  11. public static final String TYPE_NEWS_INFO = "news_info";
  12. public static final String TYPE_PRODUCT_INFO = "product_info";
  13. public static final String TYPE_STORY_INFO = "story_info";
  14. public static final String TYPE_TASK_INFO = "task_info";
  15. public static final String TYPE_USER_INFO = "user_info";
  16. public static final String TYPE_BRANDCASE_INFO = "brandcase_info";
  17. public static final String INDEX_STORE_TYPE = "memory";
  18. public static final int SHARDS = 2;
  19. public static final int REPLICAS = 1;
  20. public static final String REFRESH_INTERVAL = "-1";
  21. }
  22. }

增删改类

[html] view plain copy

print?

  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. }

查询类

[html] view plain copy

print?

  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. }

测试

[html] view plain copy

print?

  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/u014201191/8812073

http://blog.csdn.net/u014201191/article/details/46508311

项目清单

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

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

项目源码

资源文件

app.properties

[java] view plain copy

print?

  1. elasticsearch.esNodes=localhost:9300
  2. elasticsearch.cluster.name=heroscluster

app.xml

[java] view plain copy

print?

  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

[java] view plain copy

print?

  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

[java] view plain copy

print?

  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配置

[java] view plain copy

print?

  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. }

增删改类

[java] view plain copy

print?

  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. }

查询类

[java] view plain copy

print?

  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. }

测试

[java] view plain copy

print?

  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

时间: 2024-10-08 07:49:43

elasticsearch spring 集成的相关文章

RabbitMQ安装和使用(和Spring集成)

一.安装Rabbit MQ Rabbit MQ 是建立在强大的Erlang OTP平台上,因此安装Rabbit MQ的前提是安装Erlang.通过下面两个连接下载安装3.2.3 版本: 下载并安装 Eralng OTP For Windows (vR16B03) 运行安装 Rabbit MQ Server Windows Installer (v3.2.3) 具体操作步骤参考:在 Windows 上安装Rabbit MQ 指南 本人遇到的问题 当安装RabbitMQ后,使用rabbitmqctl

spring集成quartz

spring集成quartz 注意:出现异常"Caused by: java.lang.IncompatibleClassChangeError: class org.springframework.scheduling.quartz.CronTriggerBean has interface org.quartz.CronTrigger as super class" Spring3.0不支持Quartz2.0,因为org.quartz.CronTrigger在2.0从class变成

Hessian入门(包括与Spring集成)

A.纯Hessian接口开发步骤 Hessian-JAVA服务器端必须具备以下几点: * 1.包含Hessian的jar包(hessian-4.0.37.jar) * 2.设计一个接口,用来给客户端调用(IHessian.java) * 3.实现该接口的功能(IHessianImpl.java) * 4.配置web.xml,配好相应的Servlet(web.xml) * 5.对象必须实现Serializable接口(Foo.java) Hessian-JAVA服务端代码预览图: 1.包含Hess

Struts2+Spring集成合并

前边单独总结了Struts2,Spring和Ibaits框架了,那么怎么结合使用呢?这次先来看一下Sturts2和Spring的集成合并.其实挺简单的,就是导入各自的jar包以及连接彼此的jar包,分好彼此的工作就可以了. 好看一下Struts2+Spring的集成方案!  Struts2和Spring集成有两种方案,是根据action的创建来划分的!  方案一,Struts2负责流程,Spring负责对象的创建:Action由Struts2框架负责创建:Service由Spring框架负责创建

Java Persistence with MyBatis 3(中文版) 第五章 与Spring集成

MyBatis-Spring是MyBatis框架的子模块,用来提供与当前流行的依赖注入框架Spring的无缝集成. Spring框架是一个基于依赖注入(Dependency Injection)和面向切面编程(Aspect Oriented Programming,AOP)的Java框架,鼓励使用基于POJO的编程模型.另外,Spring提供了声明式和编程式的事务管理能力,可以很大程度上简化应用程序的数据访问层(data access layer)的实现.在本章中,我们将看到在基于Spring的

spring集成Log4j以及log4j配置简要说明

Spring集成: web.xml中配置log4j <context-param> <param-name>log4jConfigLocation</param-name> <param-value>WEB-INF/log4j.xml</param-value></context-param> <!-- 加载Spring框架中的log4j监听器Log4jConfigListener --><listener>

rabbitMQ第五篇:Spring集成RabbitMQ

前面几篇讲解了如何使用rabbitMq,这一篇主要讲解spring集成rabbitmq. 首先引入配置文件org.springframework.amqp,如下 <dependency> <groupId>org.springframework.amqp</groupId> <artifactId>spring-rabbit</artifactId> <version>1.6.0.RELEASE</version> <

Spring 集成 RMI

Maven <dependency> <groupId>org.springframework</groupId> <artifactId>spring-remoting</artifactId> </dependency> 服务端 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.spri

activiti搭建(二)与Spring集成

转载请注明源地址:http://www.cnblogs.com/lighten/p/5876773.html 本文主要讲解如何将Activiti和Spring框架集成,再过一段时间将会将一个基础的demo放在github上,地址将会在activiti系列导读文章中给出,如果未给出,则是因为没有构建完成(学习中,更新较慢).按照搭建篇章的顺序,也能搭建起来. 上一章将了数据库的初始化,继续那章建立的maven项目.Activiti与Spring集成需要相应的jar包,下面将补充其他maven依赖: