本文描述的是内嵌ES项目的开发,虽嵌入了Paoding分词器,但代码过程中暂未使用,故遇到针对paoding的步骤,可直接跳过。
技术描述:ElasticSearch0.20.6+Paoding。
技术环境:Eclipse、Maven
步骤一:下载并安排ElasticSearch,编写本文章时使用的是0.20.6版本,下载地址为:http://www.elasticsearch.org/downloads/0-20-6/,下载完成后,将之解压到任一目录,前期的JDK环境准备不予赘述。
步骤二:在Eclipse下建立一个Maven项目(Eclipse安装Maven插件过程不予赘述,你可直接使用Eclipse的Marketplace安装),模板使用maven-archetype-quickstart即可,创建过程不予赘述,详情询问度娘,此入假设建立的项目名为esample。
步骤三:至https://github.com/medcl/elasticsearch-analysis-paoding下载ES的paoding插件源代码,下载完成后,将之导入Eclipse,elasticsearch-analysis-paoding是个maven项目,导入时请注意,此入假设导入后项目名为elasticsearch-analysis-paoding-master。
步骤四:在esample项目中,分别引入junit、es、es-paoding和jackon,引入的配置如下所示:
junit
junit
4.8.2
test
org.elasticsearch
elasticsearch
0.20.6
org.elasticsearch
elasticsearch-analysis-paoding
1.0.0
../elasticsearch-analysis-paoding-master
com.fasterxml.jackson.core
jackson-databind
2.1.3
步骤五:在esample的src/main/resources(不存在则建立之,是个source folder)下分别添加config.properties和log4j.properties,此时esamine项目的代码结构如下所示:
config.properties和log4j.properties内容如下所示:
***********************config.properties***********************
cluster.name = elasticsearch
node.client = true
***********************log4j.properties***********************
log4j.rootLogger=info, stdout, root, error
log4j.logger.org.apache.struts2.util.TextProviderHelper=ERROR
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
#log4j.appender.stdout.layout.ConversionPattern=[%d{ISO8601}]%5p%6.6r - %m%n
log4j.appender.stdout.layout.ConversionPattern=[%d{ISO8601}]%5p%6.6r(%C:%M) - %m%n
log4j.appender.root=org.apache.log4j.RollingFileAppender
log4j.appender.root.File=${webapp.root}/log/gcr_log.log
log4j.appender.root.MaxFileSize=512KB
log4j.appender.root.MaxBackupIndex=5
log4j.appender.root.encoding=UTF-8
log4j.appender.root.layout=org.apache.log4j.PatternLayout
log4j.appender.root.layout.ConversionPattern=[%d{ISO8601}]%5p%6.6r(%C:%M) - %m%n
log4j.appender.error=org.apache.log4j.RollingFileAppender
log4j.appender.error.File=${webapp.root}/log/gcr_error_log.log
log4j.appender.error.MaxFileSize=512KB
log4j.appender.error.MaxBackupIndex=5
log4j.appender.error.encoding=UTF-8
log4j.appender.error.Threshold = ERROR
log4j.appender.error.append=true
log4j.appender.error.layout=org.apache.log4j.PatternLayout
log4j.appender.error.layout.ConversionPattern=[%d{ISO8601}]%5p%6.6r(%C:%M) - %m%n
步骤六:在esmple中新建四个类,分别为Person、ElasticSearchUtil、LoggerFactory和PropertyManager,建立后结构如下图所示:
这四个类的内容如下所示:
******************************Person.java******************************
/**
* @author Geloin
*/
package com.geloin.esample.entity;
/**
* 测试使用的实体
*
* @author Geloin
*
*/
public class Person {
/**
* 惟一编码
*/
private String id;
/**
* 名称
*/
private String name;
/**
* 性别
*/
private String sex;
/**
* 年龄
*/
private Integer age;
/**
* 是否学生
*/
private Boolean isStudent;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Boolean getIsStudent() {
return isStudent;
}
public void setIsStudent(Boolean isStudent) {
this.isStudent = isStudent;
}
}
******************************ElasticSearchUtil.java******************************
/**
* @author Geloin
*/
package com.geloin.esample.util;
import org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsRequest;
import org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsRequestBuilder;
import org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.node.Node;
import org.elasticsearch.node.NodeBuilder;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
/**
* ES工具类
*
* @author Geloin
*
*/
public class ElasticSearchUtil {
/**
* 索引库是否存在
*
* @author Geloin
* @param client
* 客户端
* @param index
* 索引库名
* @return 存在则返回true,不存在则返回false
*/
public static Boolean indexExist(Client client, String index) {
IndicesExistsRequest request = new IndicesExistsRequestBuilder(client
.admin().indices(), index).request();
IndicesExistsResponse response = client.admin().indices()
.exists(request).actionGet();
return response.exists();
}
/**
* 生成客户端
*
* @author Geloin
* @return 客户端 {@link Client}
*/
public static Client createClient() {
NodeBuilder builder = NodeBuilder.nodeBuilder();
String clusterName = PropertyManager.getContextProperty("cluster.name");
builder.clusterName(clusterName);
Boolean isClient = Boolean.parseBoolean(PropertyManager
.getContextProperty("node.client"));
builder.client(isClient);
Node node = builder.node();
return node.client();
}
/**
* 将Bean转化为JSon
*
* @author Geloin
* @param obj
* 要转化的Bean
* @return 转化后的结果
*/
public static String BeanToJson(Object obj) {
ObjectMapper mapper = new ObjectMapper();
try {
return mapper.writeValueAsString(obj);
} catch (JsonProcessingException e) {
e.printStackTrace();
return new String();
}
}
}
******************************LoggerFactory.java******************************
/**
* @author Geloin
*/
package com.geloin.esample.util;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
/**
* 日志工具类
*
* @author Geloin
*
*/
public class LoggerFactory extends Logger {
protected LoggerFactory(String name) {
super(name);
}
/**
* 新建实例
*
* @author Geloin
* @param name
* 名称
* @return 建立的实例
*/
public static Logger getInstance(String name) {
Logger log = Logger.getLogger(name);
log.setLevel(Level.ERROR);
return log;
}
/**
* 新建实例,并指定level
*
* @author Geloin
* @param name
* 名称
* @param level
* level
* @return 建立的实例
*/
public static Logger getInstance(String name, Level level) {
Logger log = Logger.getLogger(name);
log.setLevel(level);
return log;
}
/**
* 根据类名新建实例
*
* @author Geloin
* @param clazz
* 类名
* @return 实例
*/
public static Logger getInstance(Class clazz) {
Logger log = Logger.getLogger(clazz.getName());
log.setLevel(Level.ERROR);
return log;
}
/**
* 根据类名新建实例,并指定level
*
* @author Geloin
* @param clazz
* 类
* @param level
* level
* @return 实例
*/
public static Logger getInstance(Class clazz, Level level) {
Logger log = Logger.getLogger(clazz.getName());
log.setLevel(level);
return log;
}
}
******************************PropertyManager.java******************************
/**
* @author Geloin
*/
package com.geloin.esample.util;
import java.io.File;
import java.io.FileInputStream;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
/**
* 资源文件管理器
*
* @author Geloin
*
*/
public class PropertyManager {
private static Map ctxPropertiesMap = null;
static {
try {
String path = PropertyManager.class.getResource("/config")
.getPath();
File dic = new File(path);
ctxPropertiesMap = new HashMap();
for (File file : dic.listFiles()) {
if (file.getName().endsWith(".properties")) {
Properties prop = new Properties();
prop.load(new FileInputStream(file));
Enumeration