Spring Data Elasticsearch

项目清单

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-13 21:05:36

Spring Data Elasticsearch的相关文章

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 这种连接方式对应于架构图中的RESTful style API这一层,这种客户端的连接方式是RESTful风格的,使用http的方式进行连接 3.2 Transport 连接 端口 9300 这种连接方式对应于架构图中的Transport这一层,这种客户端连接方式是直接连接ES的节点,使用TCP的方式进行连接 4. ES提供了多种

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

elasticsearch 自定义分词器 安装拼音分词器.ik分词器 拼音分词器: https://github.com/medcl/elasticsearch-analysis-pinyin/releases ik分词器:https://github.com/medcl/elasticsearch-analysis-ik/releases 下载源码需要使用maven打包 下载构建好的压缩包解压后放直接在elasticsearch安装目录下 plugins文件夹下,可以重命名 1.在es中设置分

SpringBoot整合Spring Data Elasticsearch

Spring Data Elasticsearch提供了ElasticsearchTemplate工具类,实现了POJO与elasticsearch文档之间的映射 elasticsearch本质也是存储数据,它不支持事物,但是它的速度远比数据库快得多, 可以这样来对比elasticsearch和数据库 索引(indices)--------数据库(databases) 类型(type)------------数据表(table) 文档(Document)---------------- 行(ro

Spring Data ElasticSearch 使用

1.SpringDataES环境搭建        Spring Data ElasticSearch 基于 spring data API 简化 elasticSearch操作,将原始操作elasticSearch的客户端API        进行封装 .Spring Data为Elasticsearch项目提供集成搜索引擎.Spring Data Elasticsearch POJO的关键功能区域        为中心的模型与Elastichsearch交互文档和轻松地编写一个存储库数据访问

使用JAVA操作ElasticSearch(Java API 和Spring Data ElasticSearch)

Java API 我的ElasticSearch集群的版本是6.2.4,导入elasticsearch相关的maven依赖也是6.2.4,不同版本的api可能会有差异 一:maven依赖 <!--elasticsearch核心依赖--> <dependency> <groupId>org.elasticsearch</groupId> <artifactId>elasticsearch</artifactId> <version

Spring Data ElasticSearch的使用十个小案例

1.什么是Spring Data Spring Data是一个用于简化数据库访问,并支持云服务的开源框架.其主要目标是使得对数据的访问变得方便快捷,并支持map-reduce框架和云计算数据服务. Spring Data可以极大的简化JPA的写法,可以在几乎不用写实现的情况下,实现对数据的访问和操作.除了CRUD外,还包括如分页.排序等一些常用的功能. 2.什么是Spring Data ElasticSearch Spring Data ElasticSearch 基于 spring data

Spring Data ElasticSearch的使用

1.什么是Spring Data Spring Data是一个用于简化数据库访问,并支持云服务的开源框架.其主要目标是使得对数据的访问变得方便快捷,并支持map-reduce框架和云计算数据服务. Spring Data可以极大的简化JPA的写法,可以在几乎不用写实现的情况下,实现对数据的访问和操作.除了CRUD外,还包括如分页.排序等一些常用的功能. 2.什么是Spring Data ElasticSearch Spring Data ElasticSearch 基于 spring data

Spring data elasticsearch使用

一.准备 1.添加依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-elasticsearch</artifactId> </dependency> 2.application.yml spring: application: name: search-service data: elasti

spring data elasticsearch多索引查询

一次查询多个索引数据 es里可以这样写 GET 索引1,索引2,索引3/_search 也可以这样 给索引创建别名,多个索引可以使用一个别名 POST /_aliases { "actions": [ { "add": { "index": "myindex2", "alias": "myalias" } },{ "add": { "index":