elasticsearch spring 集成

 

 

项目清单

 

elasticsearch spring 集成_xml

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

 

 

项目源码

 

资源文件

 

app.properties

 

​​print​​​​?​
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. >
10. <context:annotation-config />
11. <!-- 自动扫描所有注解该路径 -->
12. <!-- <context:component-scan base-package="com.sf.heros.mq.*" /> -->
13. <context:property-placeholder location="classpath:/app.properties" />
14.
15. <import resource="elasticseach.xml" />
16. </beans>


elasticseach.xml

 


    1. <?xml version="1.0" encoding="UTF-8"?>  
    2. <beans xmlns="http://www.springframework.org/schema/beans"
    3. 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. >
    10.
    11. <util:map id="esproperties">
    12. <entry key="cluster.name" value="${elasticsearch.cluster.name}" />
    13. </util:map>
    14.
    15. <elasticsearch:client id="client" properties="esproperties"
    16. esNodes="${elasticsearch.esNodes}" />
    17.
    18. <bean name="elasticsearchTemplate"
    19. class="org.springframework.data.elasticsearch.core.ElasticsearchTemplate">
    20. <constructor-arg name="client" ref="client" />
    21. </bean>
    22.
    23. <bean name="elasticsearchService" class="com.sf.heros.mq.consumer.service.ElasticsearchService"
    24. init-method="init" />
    25.
    26. <bean name="es" class="com.sf.daidongxi.web.service.ElasticsearchService"></bean>
    27. </beans>


    log4j.properties

     

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


    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.
      37. <dependency>
      38. <groupId>org.springframework</groupId>
      39. <artifactId>spring-aop</artifactId>
      40. <version>${spring.version}</version>
      41. </dependency>
      42.
      43. <dependency>
      44. <groupId>org.springframework</groupId>
      45. <artifactId>spring-core</artifactId>
      46. <version>${spring.version}</version>
      47. </dependency>
      48.
      49. <dependency>
      50. <groupId>org.springframework</groupId>
      51. <artifactId>spring-jdbc</artifactId>
      52. <version>${spring.version}</version>
      53. </dependency>
      54.
      55. <!-- spring end -->
      56.
      57. <!-- elasticsearch package -->
      58. <dependency>
      59. <groupId>fr.pilato.spring</groupId>
      60. <artifactId>spring-elasticsearch</artifactId>
      61. <version>1.0.0</version>
      62. </dependency>
      63.
      64. <dependency>
      65. <groupId>org.elasticsearch</groupId>
      66. <artifactId>elasticsearch</artifactId>
      67. <version>1.0.0</version>
      68. </dependency>
      69.
      70. <dependency>
      71. <groupId>org.springframework.data</groupId>
      72. <artifactId>spring-data-elasticsearch</artifactId>
      73. <version>1.0.0.RELEASE</version>
      74. </dependency>
      75.
      76. <dependency>
      77. <groupId>com.alibaba</groupId>
      78. <artifactId>druid</artifactId>
      79. <version>1.0.5</version>
      80. </dependency>
      81.
      82. <!--json-lib -->
      83. <dependency>
      84. <groupId>net.sf.json-lib</groupId>
      85. <artifactId>json-lib</artifactId>
      86. <version>2.4</version>
      87. <classifier>jdk15</classifier>
      88. </dependency>
      89.
      90. <!-- quartz job -->
      91. <dependency>
      92. <groupId>org.quartz-scheduler</groupId>
      93. <artifactId>quartz</artifactId>
      94. <version>2.2.1</version>
      95. </dependency>
      96.
      97. <!-- log4j -->
      98. <dependency>
      99. <groupId>org.slf4j</groupId>
      100. <artifactId>slf4j-log4j12</artifactId>
      101. <version>1.7.5</version>
      102. </dependency>
      103. </dependencies>
      104. <build>
      105. <finalName>elasticsearch</finalName>
      106. </build>
      107. </project>

      Java.class

       

      Bean配置

       

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



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

       

      常量管理

       


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



      增删改类

       

        1.  *@Pr锛歨eros  
        2. *@Date: 2014-5-4 涓婂崍9:21:27
        3. *@Author: seaphy
        4. *@Copyright: 漏 2012 sf-express.com Inc. All rights reserved
        5. *娉ㄦ剰锛氭湰鍐呭浠呴檺浜庨『涓伴�熻繍鍏徃鍐呴儴浼犻槄锛岀姝㈠娉勪互鍙婄敤浜庡叾浠栫殑鍟嗕笟鐩殑
        6. */
        7. package com.sf.heros.mq.consumer.service;
        8.
        9. import java.util.ArrayList;
        10. import java.util.List;
        11.
        12. import org.apache.log4j.Logger;
        13. import org.elasticsearch.action.ActionFuture;
        14. import org.elasticsearch.action.admin.cluster.health.ClusterHealthRequest;
        15. import org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse;
        16. import org.elasticsearch.action.admin.cluster.health.ClusterHealthStatus;
        17. import org.elasticsearch.client.Client;
        18. import org.springframework.beans.factory.annotation.Autowired;
        19. import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
        20. import org.springframework.data.elasticsearch.core.query.IndexQuery;
        21. import org.springframework.data.elasticsearch.core.query.IndexQueryBuilder;
        22.
        23. import com.sf.heros.mq.consumer.utils.APP;
        24. import com.sf.heros.mq.consumer.vo.BrandCaseInfo;
        25. import com.sf.heros.mq.consumer.vo.NewsInfo;
        26. import com.sf.heros.mq.consumer.vo.TaskInfo;
        27. import com.sf.heros.mq.consumer.vo.UserInfo;
        28.
        29. /**
        30. * @author seaphy
        31. * @date 2014-5-4
        32. */
        33. public class ElasticsearchService {
        34.
        35. logger = Logger.getLogger(ElasticsearchService.class);
        36.
        37. @Autowired
        38. private ElasticsearchTemplate elasticsearchTemplate;
        39.
        40. @Autowired
        41. private Client esClient;
        42.
        43. public void init() {
        44. if (!elasticsearchTemplate.indexExists(APP.ESProp.INDEX_NAME)) {
        45. elasticsearchTemplate.createIndex(APP.ESProp.INDEX_NAME);
        46. }
        47. elasticsearchTemplate.putMapping(TaskInfo.class);
        48. elasticsearchTemplate.putMapping(NewsInfo.class);
        49. }
        50.
        51. <TaskInfo> taskInfoList) {
        52. <IndexQuery> queries = new ArrayList<IndexQuery>();
        53. for (TaskInfo taskInfo : taskInfoList) {
        54. indexQuery = new IndexQueryBuilder().withId(taskInfo.getTaskId()).withObject(taskInfo).build();
        55. queries.add(indexQuery);
        56. }
        57. elasticsearchTemplate.bulkIndex(queries);
        58. return true;
        59. }
        60.
        61. <TaskInfo> taskInfoList) {
        62. <IndexQuery> queries = new ArrayList<IndexQuery>();
        63. for (TaskInfo taskInfo : taskInfoList) {
        64. indexQuery = new IndexQueryBuilder().withId(taskInfo.getTaskId()).withObject(taskInfo).build();
        65. queries.add(indexQuery);
        66. }
        67. elasticsearchTemplate.bulkIndex(queries);
        68. return true;
        69. }
        70.
        71. <NewsInfo> newsInfos) {
        72. <IndexQuery> queries = new ArrayList<IndexQuery>();
        73. for (NewsInfo newsInfo : newsInfos) {
        74. indexQuery = new IndexQueryBuilder().withId(newsInfo.getNewsId()).withObject(newsInfo).build();
        75. queries.add(indexQuery);
        76. }
        77. elasticsearchTemplate.bulkIndex(queries);
        78. return true;
        79. }
        80.
        81. public boolean insertOrUpdateNewsInfo(NewsInfo newsInfo) {
        82. try {
        83. indexQuery = new IndexQueryBuilder().withId(newsInfo.getNewsId()).withObject(newsInfo).build();
        84. elasticsearchTemplate.index(indexQuery);
        85. return true;
        86. } catch (Exception e) {
        87. logger.error("insert or update news info error.", e);
        88. return false;
        89. }
        90. }
        91.
        92. public boolean insertOrUpdateTaskInfo(TaskInfo taskInfo) {
        93. try {
        94. indexQuery = new IndexQueryBuilder().withId(taskInfo.getTaskId()).withObject(taskInfo).build();
        95. elasticsearchTemplate.index(indexQuery);
        96. return true;
        97. } catch (Exception e) {
        98. logger.error("insert or update task info error.", e);
        99. return false;
        100. }
        101. }
        102.
        103. public boolean insertOrUpdateUserInfo(UserInfo userInfo) {
        104. try {
        105. indexQuery = new IndexQueryBuilder().withId(userInfo.getUserId()).withObject(userInfo).build();
        106. elasticsearchTemplate.index(indexQuery);
        107. return true;
        108. } catch (Exception e) {
        109. logger.error("insert or update user info error.", e);
        110. return false;
        111. }
        112. }
        113.
        114. <T> boolean deleteById(String id, Class<T> clzz) {
        115. try {
        116. elasticsearchTemplate.delete(clzz, id);
        117. return true;
        118. } catch (Exception e) {
        119. logger.error("delete " + clzz + " by id " + id + " error.", e);
        120. return false;
        121. }
        122. }
        123.
        124. /**
        125. * 检查健康状态
        126. * @author 高国藩
        127. * @date 2015年6月15日 下午6:59:47
        128. * @return
        129. */
        130. public boolean ping() {
        131. try {
        132. <ClusterHealthResponse> health = esClient.admin().cluster().health(new ClusterHealthRequest());
        133. status = health.actionGet().getStatus();
        134. if (status.value() == ClusterHealthStatus.RED.value()) {
        135. throw new RuntimeException("elasticsearch cluster health status is red.");
        136. }
        137. return true;
        138. } catch (Exception e) {
        139. logger.error("ping elasticsearch error.", e);
        140. return false;
        141. }
        142. }
        143.
        144. public boolean insertOrUpdateBrandCaseInfo(BrandCaseInfo brandCaseInfo) {
        145. try {
        146. indexQuery = new IndexQueryBuilder()
        147. .withId(brandCaseInfo.getId()).withObject(brandCaseInfo).build();
        148. elasticsearchTemplate.index(indexQuery);
        149. return true;
        150. } catch (Exception e) {
        151. logger.error("insert or update brandcase info error.", e);
        152. return false;
        153. }
        154. }
        155. }


        查询类

         

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

         

        测试

         

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



        源码项目下载

         

         

         

        项目清单

         

        elasticsearch spring 集成_elasticsearch_02

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

         

         

        项目源码

         

        资源文件

         

        app.properties

         



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

        app.xml


        1. <?xml version="1.0" encoding="UTF-8"?>    
        2. <beans xmlns="http://www.springframework.org/schema/beans"
        3. "http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util"
        4. "http://www.springframework.org/schema/context"
        5. "http://www.pilato.fr/schema/elasticsearch"
        6. //www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        7. //www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd
        8. //www.pilato.fr/schema/elasticsearch http://www.pilato.fr/schema/elasticsearch/elasticsearch-0.3.xsd
        9. //www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">
        10. <context:annotation-config />
        11. <!-- 自动扫描所有注解该路径 -->
        12. package="com.sf.heros.mq.*" /> -->
        13. "classpath:/app.properties" />
        14.
        15. import resource="elasticseach.xml" />
        16. </beans>

        elasticseach.xml


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

        maven

         

         

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

          Java.class

           

          Bean配置

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

           

          增删改类

          1.  *@Pr锛歨eros   
          2. *@Date: 2014-5-4 涓婂崍9:21:27
          3. *@Author: seaphy
          4. *@Copyright: 漏 2012 sf-express.com Inc. All rights reserved
          5. *娉ㄦ剰锛氭湰鍐呭浠呴檺浜庨『涓伴�熻繍鍏徃鍐呴儴浼犻槄锛岀姝㈠娉勪互鍙婄敤浜庡叾浠栫殑鍟嗕笟鐩殑
          6. */
          7. package com.sf.heros.mq.consumer.service;
          8.
          9. import java.util.ArrayList;
          10. import java.util.List;
          11.
          12. import org.apache.log4j.Logger;
          13. import org.elasticsearch.action.ActionFuture;
          14. import org.elasticsearch.action.admin.cluster.health.ClusterHealthRequest;
          15. import org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse;
          16. import org.elasticsearch.action.admin.cluster.health.ClusterHealthStatus;
          17. import org.elasticsearch.client.Client;
          18. import org.springframework.beans.factory.annotation.Autowired;
          19. import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
          20. import org.springframework.data.elasticsearch.core.query.IndexQuery;
          21. import org.springframework.data.elasticsearch.core.query.IndexQueryBuilder;
          22.
          23. import com.sf.heros.mq.consumer.utils.APP;
          24. import com.sf.heros.mq.consumer.vo.BrandCaseInfo;
          25. import com.sf.heros.mq.consumer.vo.NewsInfo;
          26. import com.sf.heros.mq.consumer.vo.TaskInfo;
          27. import com.sf.heros.mq.consumer.vo.UserInfo;
          28.
          29. /**
          30. * @author seaphy
          31. * @date 2014-5-4
          32. */
          33. public class ElasticsearchService {
          34.
          35. private static final Logger logger = Logger.getLogger(ElasticsearchService.class);
          36.
          37. @Autowired
          38. private ElasticsearchTemplate elasticsearchTemplate;
          39.
          40. @Autowired
          41. private Client esClient;
          42.
          43. public void init() {
          44. if (!elasticsearchTemplate.indexExists(APP.ESProp.INDEX_NAME)) {
          45. elasticsearchTemplate.createIndex(APP.ESProp.INDEX_NAME);
          46. }
          47. class);
          48. class);
          49. }
          50.
          51. public boolean update(List<TaskInfo> taskInfoList) {
          52. new ArrayList<IndexQuery>();
          53. for (TaskInfo taskInfo : taskInfoList) {
          54. new IndexQueryBuilder().withId(taskInfo.getTaskId()).withObject(taskInfo).build();
          55. queries.add(indexQuery);
          56. }
          57. elasticsearchTemplate.bulkIndex(queries);
          58. return true;
          59. }
          60.
          61. public boolean insertOrUpdateTaskInfo(List<TaskInfo> taskInfoList) {
          62. new ArrayList<IndexQuery>();
          63. for (TaskInfo taskInfo : taskInfoList) {
          64. new IndexQueryBuilder().withId(taskInfo.getTaskId()).withObject(taskInfo).build();
          65. queries.add(indexQuery);
          66. }
          67. elasticsearchTemplate.bulkIndex(queries);
          68. return true;
          69. }
          70.
          71. public boolean insertOrUpdateNewsInfo(List<NewsInfo> newsInfos) {
          72. new ArrayList<IndexQuery>();
          73. for (NewsInfo newsInfo : newsInfos) {
          74. new IndexQueryBuilder().withId(newsInfo.getNewsId()).withObject(newsInfo).build();
          75. queries.add(indexQuery);
          76. }
          77. elasticsearchTemplate.bulkIndex(queries);
          78. return true;
          79. }
          80.
          81. public boolean insertOrUpdateNewsInfo(NewsInfo newsInfo) {
          82. try {
          83. new IndexQueryBuilder().withId(newsInfo.getNewsId()).withObject(newsInfo).build();
          84. elasticsearchTemplate.index(indexQuery);
          85. return true;
          86. catch (Exception e) {
          87. "insert or update news info error.", e);
          88. return false;
          89. }
          90. }
          91.
          92. public boolean insertOrUpdateTaskInfo(TaskInfo taskInfo) {
          93. try {
          94. new IndexQueryBuilder().withId(taskInfo.getTaskId()).withObject(taskInfo).build();
          95. elasticsearchTemplate.index(indexQuery);
          96. return true;
          97. catch (Exception e) {
          98. "insert or update task info error.", e);
          99. return false;
          100. }
          101. }
          102.
          103. public boolean insertOrUpdateUserInfo(UserInfo userInfo) {
          104. try {
          105. new IndexQueryBuilder().withId(userInfo.getUserId()).withObject(userInfo).build();
          106. elasticsearchTemplate.index(indexQuery);
          107. return true;
          108. catch (Exception e) {
          109. "insert or update user info error.", e);
          110. return false;
          111. }
          112. }
          113.
          114. public <T> boolean deleteById(String id, Class<T> clzz) {
          115. try {
          116. elasticsearchTemplate.delete(clzz, id);
          117. return true;
          118. catch (Exception e) {
          119. "delete " + clzz + " by id " + id + " error.", e);
          120. return false;
          121. }
          122. }
          123.
          124. /**
          125. * 检查健康状态
          126. * @author 高国藩
          127. * @date 2015年6月15日 下午6:59:47
          128. * @return
          129. */
          130. public boolean ping() {
          131. try {
          132. new ClusterHealthRequest());
          133. ClusterHealthStatus status = health.actionGet().getStatus();
          134. if (status.value() == ClusterHealthStatus.RED.value()) {
          135. throw new RuntimeException("elasticsearch cluster health status is red.");
          136. }
          137. return true;
          138. catch (Exception e) {
          139. "ping elasticsearch error.", e);
          140. return false;
          141. }
          142. }
          143.
          144. public boolean insertOrUpdateBrandCaseInfo(BrandCaseInfo brandCaseInfo) {
          145. try {
          146. new IndexQueryBuilder()
          147. .withId(brandCaseInfo.getId()).withObject(brandCaseInfo).build();
          148. elasticsearchTemplate.index(indexQuery);
          149. return true;
          150. catch (Exception e) {
          151. "insert or update brandcase info error.", e);
          152. return false;
          153. }
          154. }
          155. }

          查询类

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

          测试

           

           

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