码迷,mamicode.com
首页 > 编程语言 > 详细

elasticsearch spring 集成

时间:2016-09-21 10:21:36      阅读:234      评论:0      收藏:0      [点我收藏+]

标签:

elasticsearch spring 集成

 

 

项目清单

 
技术分享

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

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

项目源码

 

资源文件

 

app.properties

 
[html] view plain copy
 
 print?
  1. elasticsearch.esNodes=localhost:9300  
  2. elasticsearch.cluster.name=heroscluster  


app.xml

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


elasticseach.xml

 
[html] view plain copy
 
 print?
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util"  
  4.     xmlns:context="http://www.springframework.org/schema/context"  
  5.     xmlns:elasticsearch="http://www.pilato.fr/schema/elasticsearch"  
  6.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
  7.         http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd  
  8.         http://www.pilato.fr/schema/elasticsearch http://www.pilato.fr/schema/elasticsearch/elasticsearch-0.3.xsd  
  9.         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">  
  10.   
  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

 
[html] view plain copy
 
 print?
  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管理

 
[html] view plain copy
 
 print?
  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  2.     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">  
  3.     <modelVersion>4.0.0</modelVersion>  
  4.     <groupId>com.elasticsearch</groupId>  
  5.     <artifactId>elasticsearch</artifactId>  
  6.     <packaging>war</packaging>  
  7.     <version>0.0.1-SNAPSHOT</version>  
  8.     <name>elasticsearch Maven Webapp</name>  
  9.     <url>http://maven.apache.org</url>  
  10.     <properties>  
  11.         <spring.version>3.1.1.RELEASE</spring.version>  
  12.         <findbugs.annotations>2.0.0</findbugs.annotations>  
  13.         <checkstyle.maven.plugin>2.11</checkstyle.maven.plugin>  
  14.         <pmd.maven.plugin>3.0</pmd.maven.plugin>  
  15.         <findbugs.maven.plugin>2.5.3</findbugs.maven.plugin>  
  16.         <java.version>1.7</java.version>  
  17.     </properties>  
  18.     <dependencies>  
  19.         <dependency>  
  20.             <groupId>junit</groupId>  
  21.             <artifactId>junit</artifactId>  
  22.             <version>3.8.1</version>  
  23.             <scope>test</scope>  
  24.         </dependency>  
  25.         <!-- spring begin -->  
  26.         <dependency>  
  27.             <groupId>org.springframework</groupId>  
  28.             <artifactId>spring-context</artifactId>  
  29.             <version>${spring.version}</version>  
  30.         </dependency>  
  31.         <dependency>  
  32.             <groupId>org.springframework</groupId>  
  33.             <artifactId>spring-context-support</artifactId>  
  34.             <version>${spring.version}</version>  
  35.         </dependency>  
  36.   
  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配置

 
[html] view plain copy
 
 print?
  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.                 + 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. }  


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

常量管理

 
[html] view plain copy
 
 print?
  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.     public static final Map<String, Stringmap = new HashMap<String, String>();  
  8.     public static final String CLOSED_MSG = "#################closed####################";  
  9.     public static final long DELIVERIED_TAG = -1;  
  10.   
  11.     class ESProp {  
  12.         public static final String INDEX_NAME = "heros";  
  13.         public static final String DAIDONGXI_INDEX_NAME = "daidongxi";  
  14.         public static final String TYPE_NEWS_INFO = "news_info";  
  15.         public static final String TYPE_PRODUCT_INFO = "product_info";  
  16.         public static final String TYPE_STORY_INFO = "story_info";  
  17.         public static final String TYPE_TASK_INFO = "task_info";  
  18.         public static final String TYPE_USER_INFO = "user_info";  
  19.         public static final String TYPE_BRANDCASE_INFO = "brandcase_info";  
  20.         public static final String INDEX_STORE_TYPE = "memory";  
  21.         public static final int SHARDS = 2;  
  22.         public static final int REPLICAS = 1;  
  23.         public static final String REFRESH_INTERVAL = "-1";  
  24.     }  
  25.   
  26. }  


增删改类

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


查询类

 
[html] view plain copy
 
 print?
  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.             .getLogger(ElasticsearchService.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.             String sortField, SortOrder order, int from, int size) {  
  52.         SearchRequestBuilder reqBuilder = client.prepareSearch(esIndexName)  
  53.                 .setTypes(type).setSearchType(SearchType.DEFAULT)  
  54.                 .setExplain(true);  
  55.         QueryStringQueryBuilder 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.         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.         ArrayList<Stringresults = 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.                 .setExplain(true);  
  104.         QueryStringQueryBuilder 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.         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.         List<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.     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.                 .setExplain(true);  
  138.         QueryStringQueryBuilder 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.         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.         List<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.     public List<Map<String, Object>> queryForObjectNotEq(String type,  
  178.             String field, Collection<String> countents, String sortField,  
  179.             SortOrder order, int from, int size) {  
  180.   
  181.         SearchRequestBuilder reqBuilder = client.prepareSearch(esIndexName)  
  182.                 .setTypes(type).setSearchType(SearchType.DEFAULT)  
  183.                 .setExplain(true);  
  184.         List<Stringcontents = new ArrayList<String>();  
  185.         for (String content : countents) {  
  186.             contents.add("\"" + content + "\"");  
  187.         }  
  188.         TermsQueryBuilder 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.         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.         List<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.      *    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.             String field, String content,int start,int end) {  
  235.   
  236.         SearchRequestBuilder reqBuilder = client.prepareSearch(esIndexName)  
  237.                 .setTypes(type).setSearchType(SearchType.DEFAULT)  
  238.                 .setExplain(true);  
  239.         QueryStringQueryBuilder 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.         SearchResponse resp = reqBuilder.execute().actionGet();  
  248.         SearchHit[] hits = resp.getHits().getHits();  
  249.   
  250.         List<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. }  
 
 

测试

 
[html] view plain copy
 
 print?
  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.         ClassPathXmlApplicationContext 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.         ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(  
  44.                 "classpath:app.xml");  
  45.         ElasticsearchService service = context  
  46.                 .getBean(ElasticsearchService.class);  
  47.         List<TaskInfotaskInfoList = new ArrayList<TaskInfo>();  
  48.         for (int 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.         ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(  
  63.                 "classpath:app.xml");  
  64.         com.sf.daidongxi.web.service.ElasticsearchService service = (com.sf.daidongxi.web.service.ElasticsearchService) context  
  65.                 .getBean("es");  
  66.         List<Map<String, Object>al = service.queryForObject("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.         ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(  
  84.                 "classpath:app.xml");  
  85.         com.sf.daidongxi.web.service.ElasticsearchService service = (com.sf.daidongxi.web.service.ElasticsearchService) context  
  86.                 .getBean("es");  
  87.         List<Map<String, Object>al = service.queryForObjectForElasticSerch("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. }  


源码项目下载

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

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

 

 

项目清单

 
技术分享

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

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

项目源码

 

资源文件

 

app.properties

 

[java] view plain copy
 
 print?技术分享技术分享
  1. elasticsearch.esNodes=localhost:9300  
  2. elasticsearch.cluster.name=heroscluster  

app.xml

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


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

[java] view plain copy
 
 print?技术分享技术分享
  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.                 + 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. }  

 

增删改类

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

查询类

[java] view plain copy
 
 print?技术分享技术分享
  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.             .getLogger(ElasticsearchService.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.             String sortField, SortOrder order, int from, int size) {    
  52.         SearchRequestBuilder reqBuilder = client.prepareSearch(esIndexName)    
  53.                 .setTypes(type).setSearchType(SearchType.DEFAULT)    
  54.                 .setExplain(true);    
  55.         QueryStringQueryBuilder 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.         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.         ArrayList<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.     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.                 .setExplain(true);    
  104.         QueryStringQueryBuilder 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.         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.         List<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.     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.                 .setExplain(true);    
  138.         QueryStringQueryBuilder 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.         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.         List<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.     public List<Map<String, Object>> queryForObjectNotEq(String type,    
  178.             String field, Collection<String> countents, String sortField,    
  179.             SortOrder order, int from, int size) {    
  180.     
  181.         SearchRequestBuilder reqBuilder = client.prepareSearch(esIndexName)    
  182.                 .setTypes(type).setSearchType(SearchType.DEFAULT)    
  183.                 .setExplain(true);    
  184.         List<String> contents = new ArrayList<String>();    
  185.         for (String content : countents) {    
  186.             contents.add("\"" + content + "\"");    
  187.         }    
  188.         TermsQueryBuilder 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.         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.         List<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.      *    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.             String field, String content,int start,int end) {    
  235.     
  236.         SearchRequestBuilder reqBuilder = client.prepareSearch(esIndexName)    
  237.                 .setTypes(type).setSearchType(SearchType.DEFAULT)    
  238.                 .setExplain(true);    
  239.         QueryStringQueryBuilder 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.         SearchResponse resp = reqBuilder.execute().actionGet();    
  248.         SearchHit[] hits = resp.getHits().getHits();    
  249.     
  250.         List<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. }    

测试

 

 

[java] view plain copy
 
 print?技术分享技术分享
  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.         ClassPathXmlApplicationContext 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.         ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(    
  44.                 "classpath:app.xml");    
  45.         ElasticsearchService service = context    
  46.                 .getBean(ElasticsearchService.class);    
  47.         List<TaskInfo> taskInfoList = new ArrayList<TaskInfo>();    
  48.         for (int 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.         ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(    
  63.                 "classpath:app.xml");    
  64.         com.sf.daidongxi.web.service.ElasticsearchService service = (com.sf.daidongxi.web.service.ElasticsearchService) context    
  65.                 .getBean("es");    
  66.         List<Map<String, Object>> al = service.queryForObject("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.         ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(    
  84.                 "classpath:app.xml");    
  85.         com.sf.daidongxi.web.service.ElasticsearchService service = (com.sf.daidongxi.web.service.ElasticsearchService) context    
  86.                 .getBean("es");    
  87.         List<Map<String, Object>> al = service.queryForObjectForElasticSerch("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. }    
技术分享

 

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

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

 

elasticsearch spring 集成

标签:

原文地址:http://www.cnblogs.com/softidea/p/5891552.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!