
http://download.csdn.net/detail/u014201191/8809619
elasticsearch.esnodes=localhost:9300
elasticsearch.cluster.name=heroscluster
<?xml version="1.0" encoding="utf-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns:util="http://www.springframework.org/schema/util"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:elasticsearch="http://www.pilato.fr/schema/elasticsearch"
xsi:schemalocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd
http://www.pilato.fr/schema/elasticsearch http://www.pilato.fr/schema/elasticsearch/elasticsearch-0.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<context:annotation-config />
<!-- 自動掃描所有注解該路徑 -->
<!-- <context:component-scan base-package="com.sf.heros.mq.*" /> -->
<context:property-placeholder location="classpath:/app.properties" />
<import resource="elasticseach.xml" />
</beans>
<util:map id="esproperties">
<entry key="cluster.name" value="${elasticsearch.cluster.name}" />
</util:map>
<elasticsearch:client id="client" properties="esproperties"
esnodes="${elasticsearch.esnodes}" />
<bean name="elasticsearchtemplate"
class="org.springframework.data.elasticsearch.core.elasticsearchtemplate">
<constructor-arg name="client" ref="client" />
</bean>
<bean name="elasticsearchservice" class="com.sf.heros.mq.consumer.service.elasticsearchservice"
init-method="init" />
<bean name="es" class="com.sf.daidongxi.web.service.elasticsearchservice"></bean>
### \u8bbe\u7f6elogger\u8f93\u51fa\u7ea7\u522b\u548c\u8f93\u51fa\u76ee\u7684\u5730 ###
log4j.rootlogger=info,logfile
log4j.appender.console=org.apache.log4j.consoleappender
log4j.appender.console.threshold=info
log4j.appender.console.layout=org.apache.log4j.patternlayout
log4j.appender.console.layout.conversionpattern=[%-5p] %d{yyyy-mm-dd hh:mm:ss,sss} - %m%n
log4j.appender.logfile=org.apache.log4j.dailyrollingfileappender
log4j.appender.logfile.file=/app/logs/mq_consumer.log
log4j.appender.logfile.datepattern='.'yyyy-mm-dd'.'
log4j.appender.logfile.append=true
log4j.appender.logfile.threshold=debug
log4j.appender.logfile.layout=org.apache.log4j.patternlayout
log4j.appender.logfile.layout.conversionpattern=[%-5p] %d{yyyy-mm-dd hh:mm:ss,sss} - %m%n
<project xmlns="http://maven.apache.org/pom/4.0.0" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"
xsi:schemalocation="http://maven.apache.org/pom/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelversion>4.0.0</modelversion>
<groupid>com.elasticsearch</groupid>
<artifactid>elasticsearch</artifactid>
<packaging>war</packaging>
<version>0.0.1-snapshot</version>
<name>elasticsearch maven webapp</name>
<url>http://maven.apache.org</url>
<properties>
<spring.version>3.1.1.release</spring.version>
<findbugs.annotations>2.0.0</findbugs.annotations>
<checkstyle.maven.plugin>2.11</checkstyle.maven.plugin>
<pmd.maven.plugin>3.0</pmd.maven.plugin>
<findbugs.maven.plugin>2.5.3</findbugs.maven.plugin>
<java.version>1.7</java.version>
</properties>
<dependencies>
<dependency>
<groupid>junit</groupid>
<artifactid>junit</artifactid>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- spring begin -->
<groupid>org.springframework</groupid>
<artifactid>spring-context</artifactid>
<version>${spring.version}</version>
<artifactid>spring-context-support</artifactid>
<artifactid>spring-aop</artifactid>
<artifactid>spring-core</artifactid>
<artifactid>spring-jdbc</artifactid>
<!-- spring end -->
<!-- elasticsearch package -->
<groupid>fr.pilato.spring</groupid>
<artifactid>spring-elasticsearch</artifactid>
<version>1.0.0</version>
<groupid>org.elasticsearch</groupid>
<artifactid>elasticsearch</artifactid>
<groupid>org.springframework.data</groupid>
<artifactid>spring-data-elasticsearch</artifactid>
<version>1.0.0.release</version>
<groupid>com.alibaba</groupid>
<artifactid>druid</artifactid>
<version>1.0.5</version>
<!--json-lib -->
<groupid>net.sf.json-lib</groupid>
<artifactid>json-lib</artifactid>
<version>2.4</version>
<classifier>jdk15</classifier>
<!-- quartz job -->
<groupid>org.quartz-scheduler</groupid>
<artifactid>quartz</artifactid>
<version>2.2.1</version>
<!-- log4j -->
<groupid>org.slf4j</groupid>
<artifactid>slf4j-log4j12</artifactid>
<version>1.7.5</version>
</dependencies>
<build>
<finalname>elasticsearch</finalname>
</build>
</project>
package com.sf.heros.mq.consumer.vo;
import org.springframework.data.annotation.id;
import org.springframework.data.elasticsearch.annotations.document;
import org.springframework.data.elasticsearch.annotations.field;
import org.springframework.data.elasticsearch.annotations.fieldindex;
import org.springframework.data.elasticsearch.annotations.fieldtype;
import com.sf.heros.mq.consumer.utils.app;
//@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)
@document(indexname = app.esprop.index_name, type = app.esprop.type_task_info)
public class taskinfo {
@id
@field(index = fieldindex.not_analyzed, store = true)
private string taskid;
@field(type = fieldtype.integer, index = fieldindex.not_analyzed, store = true)
private integer userid;
@field(type = fieldtype.string, indexanalyzer="ik", searchanalyzer="ik", store = true)
private string taskcontent;
private string taskarea;
private string tasktags;
private integer taskstate;
@field(type = fieldtype.string, index = fieldindex.not_analyzed, store = true)
private string updatetime;
private string usernickname;
public string gettaskid() {
return taskid;
}
public void settaskid(string taskid) {
this.taskid = taskid;
public integer getuserid() {
return userid;
public void setuserid(integer userid) {
this.userid = userid;
public string gettaskcontent() {
return taskcontent;
public void settaskcontent(string taskcontent) {
this.taskcontent = taskcontent;
public string gettaskarea() {
return taskarea;
public void settaskarea(string taskarea) {
this.taskarea = taskarea;
public string gettasktags() {
return tasktags;
public void settasktags(string tasktags) {
this.tasktags = tasktags;
public integer gettaskstate() {
return taskstate;
public void settaskstate(integer taskstate) {
this.taskstate = taskstate;
public string getupdatetime() {
return updatetime;
public void setupdatetime(string updatetime) {
this.updatetime = updatetime;
public string getusernickname() {
return usernickname;
public void setusernickname(string usernickname) {
this.usernickname = usernickname;
@override
public string tostring() {
return "taskinfo [taskid=" + taskid + ", userid=" + userid
+ ", taskcontent=" + taskcontent + ", taskarea=" + taskarea
+ ", taskstate=" + taskstate
+ ", updatetime=" + updatetime + ", usernickname="
+ usernickname + "]";
public taskinfo(string taskid, integer userid, string taskcontent,
string taskarea, string tasktags, integer taskstate,
string updatetime, string usernickname) {
public taskinfo() {
// todo auto-generated constructor stub
}
其餘的類在源碼中下載下傳,此處不列出了...
package com.sf.heros.mq.consumer.utils;
import java.util.hashmap;
import java.util.map;
public interface app {
public static final map<string, string> map = new hashmap<string, string>();
public static final string closed_msg = "#################closed####################";
public static final long deliveried_tag = -1;
class esprop {
public static final string index_name = "heros";
public static final string daidongxi_index_name = "daidongxi";
public static final string type_news_info = "news_info";
public static final string type_product_info = "product_info";
public static final string type_story_info = "story_info";
public static final string type_task_info = "task_info";
public static final string type_user_info = "user_info";
public static final string type_brandcase_info = "brandcase_info";
public static final string index_store_type = "memory";
public static final int shards = 2;
public static final int replicas = 1;
public static final string refresh_interval = "-1";
/**
*@pr锛歨eros
*@date: 2014-5-4 涓婂崍9:21:27
*@author: seaphy
*@copyright: 漏 2012 sf-express.com inc. all rights reserved
*娉ㄦ剰锛氭湰鍐呭浠呴檺浜庨『涓伴�熻繍鍏徃鍐呴儴浼犻槄锛岀姝㈠娉勪互鍙婄敤浜庡叾浠栫殑鍟嗕笟鐩殑
*/
package com.sf.heros.mq.consumer.service;
import java.util.arraylist;
import java.util.list;
import org.apache.log4j.logger;
import org.elasticsearch.action.actionfuture;
import org.elasticsearch.action.admin.cluster.health.clusterhealthrequest;
import org.elasticsearch.action.admin.cluster.health.clusterhealthresponse;
import org.elasticsearch.action.admin.cluster.health.clusterhealthstatus;
import org.elasticsearch.client.client;
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.data.elasticsearch.core.elasticsearchtemplate;
import org.springframework.data.elasticsearch.core.query.indexquery;
import org.springframework.data.elasticsearch.core.query.indexquerybuilder;
import com.sf.heros.mq.consumer.vo.brandcaseinfo;
import com.sf.heros.mq.consumer.vo.newsinfo;
import com.sf.heros.mq.consumer.vo.taskinfo;
import com.sf.heros.mq.consumer.vo.userinfo;
* @author seaphy
* @date 2014-5-4
public class elasticsearchservice {
private static final logger logger = logger.getlogger(elasticsearchservice.class);
@autowired
private elasticsearchtemplate elasticsearchtemplate;
private client esclient;
public void init() {
if (!elasticsearchtemplate.indexexists(app.esprop.index_name)) {
elasticsearchtemplate.createindex(app.esprop.index_name);
}
elasticsearchtemplate.putmapping(taskinfo.class);
elasticsearchtemplate.putmapping(newsinfo.class);
public boolean update(list<taskinfo> taskinfolist) {
list<indexquery> queries = new arraylist<indexquery>();
for (taskinfo taskinfo : taskinfolist) {
indexquery indexquery = new indexquerybuilder().withid(taskinfo.gettaskid()).withobject(taskinfo).build();
queries.add(indexquery);
elasticsearchtemplate.bulkindex(queries);
return true;
public boolean insertorupdatetaskinfo(list<taskinfo> taskinfolist) {
public boolean insertorupdatenewsinfo(list<newsinfo> newsinfos) {
for (newsinfo newsinfo : newsinfos) {
indexquery indexquery = new indexquerybuilder().withid(newsinfo.getnewsid()).withobject(newsinfo).build();
public boolean insertorupdatenewsinfo(newsinfo newsinfo) {
try {
elasticsearchtemplate.index(indexquery);
return true;
} catch (exception e) {
logger.error("insert or update news info error.", e);
return false;
public boolean insertorupdatetaskinfo(taskinfo taskinfo) {
logger.error("insert or update task info error.", e);
public boolean insertorupdateuserinfo(userinfo userinfo) {
indexquery indexquery = new indexquerybuilder().withid(userinfo.getuserid()).withobject(userinfo).build();
logger.error("insert or update user info error.", e);
public <t> boolean deletebyid(string id, class<t> clzz) {
elasticsearchtemplate.delete(clzz, id);
logger.error("delete " + clzz + " by id " + id + " error.", e);
/**
* 檢查健康狀态
* @author 高國藩
* @date 2015年6月15日 下午6:59:47
* @return
*/
public boolean ping() {
actionfuture<clusterhealthresponse> health = esclient.admin().cluster().health(new clusterhealthrequest());
clusterhealthstatus status = health.actionget().getstatus();
if (status.value() == clusterhealthstatus.red.value()) {
throw new runtimeexception("elasticsearch cluster health status is red.");
}
logger.error("ping elasticsearch error.", e);
public boolean insertorupdatebrandcaseinfo(brandcaseinfo brandcaseinfo) {
indexquery indexquery = new indexquerybuilder()
.withid(brandcaseinfo.getid()).withobject(brandcaseinfo).build();
logger.error("insert or update brandcase info error.", e);
package com.sf.daidongxi.web.service;
import java.util.collection;
import org.apache.commons.lang.stringutils;
import org.apache.lucene.queries.termfilter;
import org.apache.lucene.queryparser.xml.builders.filteredquerybuilder;
import org.elasticsearch.action.search.searchrequestbuilder;
import org.elasticsearch.action.search.searchresponse;
import org.elasticsearch.action.search.searchtype;
import org.elasticsearch.index.query.boolfilterbuilder;
import org.elasticsearch.index.query.filterbuilder;
import org.elasticsearch.index.query.filterbuilders;
import org.elasticsearch.index.query.matchquerybuilder;
import org.elasticsearch.index.query.querybuilder;
import org.elasticsearch.index.query.querybuilders;
import org.elasticsearch.index.query.querystringquerybuilder;
import org.elasticsearch.index.query.rangefilterbuilder;
import org.elasticsearch.index.query.termsquerybuilder;
import org.elasticsearch.search.searchhit;
import org.elasticsearch.search.sort.sortorder;
import org.springframework.beans.factory.initializingbean;
import sun.misc.contended;
public class elasticsearchservice implements initializingbean {
private static final logger logger = logger
.getlogger(elasticsearchservice.class);
private client client;
private string esindexname = "heros";
/** 查詢 id */
public list<string> queryid(string type, string[] fields, string content,
string sortfield, sortorder order, int from, int size) {
searchrequestbuilder reqbuilder = client.preparesearch(esindexname)
.settypes(type).setsearchtype(searchtype.default)
.setexplain(true);
querystringquerybuilder querystring = querybuilders.querystring("\""
+ content + "\"");
for (string k : fields) {
querystring.field(k);
querystring.minimumshouldmatch("10");
reqbuilder.setquery(querybuilders.boolquery().should(querystring))
if (stringutils.isnotempty(sortfield) && order != null) {
reqbuilder.addsort(sortfield, order);
if (from >= 0 && size > 0) {
reqbuilder.setfrom(from).setsize(size);
searchresponse resp = reqbuilder.execute().actionget();
searchhit[] hits = resp.gethits().gethits();
arraylist<string> results = new arraylist<string>();
for (searchhit hit : hits) {
results.add(hit.getid());
return results;
* 查詢得到結果為map集合
*
* @author 高國藩
* @date 2015年6月15日 下午8:46:13
* @param type
* 表
* @param fields
* 字段索引
* @param content
* 查詢的值
* @param sortfield
* 排序的字段
* @param order
* 排序的規則
* @param from
* 分頁
* @param size
* @return
public list<map<string, object>> queryforobject(string type,
string[] fields, string content, string sortfield, sortorder order,
int from, int size) {
list<map<string, object>> results = new arraylist<map<string, object>>();
results.add(hit.getsource());
* querybuilders 所有查詢入口
public list<map<string, object>> queryforobjecteq(string type,
reqbuilder.setquery(querybuilders.boolquery().must(querystring))
* 多個文字記不清是那些字,然後放進去查詢
* @date 2015年6月16日 上午9:56:08
* @param field
* @param countents
public list<map<string, object>> queryforobjectnoteq(string type,
string field, collection<string> countents, string sortfield,
sortorder order, int from, int size) {
list<string> contents = new arraylist<string>();
for (string content : countents) {
contents.add("\"" + content + "\"");
termsquerybuilder inquery = querybuilders.inquery(field, contents);
inquery.minimumshouldmatch("10");
reqbuilder.setquery(querybuilders.boolquery().mustnot(inquery))
* filters 查詢方式
* 1. 1)querybuilders.querystring 獲得基本查詢
* 2)filteredquerybuilder query = querybuilders.filteredquery(querystring,filterbuilder)
* 3)通過上面封裝成為查詢,将這個query插入到reqbuilder中;完成操作
*
* 2.在 reqbuilder.setquery(query);
* 3.介紹在2)中的filterbuilder各種構造方式-參數都可以傳string類型即可
* filterbuilders.rangefilter("taskstate").lt(20) 小于 、 lte(20) 小于等于
* filterbuilders.rangefilter("taskstate").gt(20)) 大于 、 gte(20) 大于等于
* filterbuilders.rangefilter("taskstate").from(start).to(end)) 範圍,也可以指定日期,用字元串就ok了
* @date 2015年6月15日 下午10:06:05
public list<map<string, object>> queryforobjectforelasticserch(string type,
string field, string content,int start,int end) {
querystring.field(field);
reqbuilder.setquery(querybuilders.filteredquery(querystring, filterbuilders.rangefilter("taskstate").from(start).to(end)))
public void afterpropertiesset() throws exception {
system.out.println("init...");
package com.sf.heros.mq.consumer;
import java.util.hashset;
import org.junit.test;
import org.springframework.context.support.classpathxmlapplicationcontext;
import com.sf.heros.mq.consumer.service.elasticsearchservice;
public class appmain {
private static final logger logger = logger.getlogger(appmain.class);
public void start() {
classpathxmlapplicationcontext context = null;
context = new classpathxmlapplicationcontext("classpath:app.xml");
logger.error("an error occurred, applicationcontext will close.", e);
if (context != null) {
context.close();
context = null;
logger.error(app.closed_msg);
* 插入
* @date 2015年6月16日 上午10:14:21
@test
public void insertno() {
classpathxmlapplicationcontext context = new classpathxmlapplicationcontext(
"classpath:app.xml");
elasticsearchservice service = context
.getbean(elasticsearchservice.class);
list<taskinfo> taskinfolist = new arraylist<taskinfo>();
for (int i = 0; i < 20; i++) {
taskinfolist.add(new taskinfo(string.valueof((i + 5)), i + 5, "高國藩"
+ i, "taskarea", "tasktags", i + 5, "1996-02-03", "霍華德"));
service.insertorupdatetaskinfo(taskinfolist);
* 查詢
public void serchno() {
com.sf.daidongxi.web.service.elasticsearchservice service = (com.sf.daidongxi.web.service.elasticsearchservice) context
.getbean("es");
list<map<string, object>> al = service.queryforobject("task_info",
new string[] { "taskcontent", "taskarea" }, "高國藩", "taskarea", sortorder.desc,
0, 2);
for (int i = 0; i < al.size(); i++) {
system.out.println(al.get(i));
* filter查詢
public void serchfilter() {
list<map<string, object>> al = service.queryforobjectforelasticserch("task_info", "taskcontent", "高",19,20);
}