ElasticSearch JAVA API基础学习------Elasticsearch学习(二)

在上一篇博客中(Elasticsearch安装)已经完成了es的安装,那么接下来,将介绍下如在java代码中完成对某个索引的类型的文档的增删改查。这个java api的介绍在官网上也有很好的例子,大家可以参考下。

完整演示demo下载:

github:https://github.com/wesley5201314/Elasticsearch-demo

gitosc:https://git.oschina.net/zhengweishan/Elasticsearch-demo

es中的索引就对应数据库,类型就对应着数据库中的表,文档就对应着数据库表中的记录,因此,我们首先得创建一个索引,然后,再创建一个类型,这个类型会包含字段类型信息,然后就可以在这个索引上对此类型的文档进行增删改查了。

下面,将分步介绍:

创建一个EsClient.java类,直接贴出代码如下:

package com.es.demo;

import java.io.IOException;
import java.net.InetAddress;
import java.util.Properties;

import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;

public class EsClient {
	
	/** 配置文件参数 */
	private static Properties properties = null;
	
	private static String es_ip = null; //ip
	private static int es_port = 0; //端口
	private static String es_cluster_name = null; //集群名字
	
	private static TransportClient client = null;
	
	public TransportClient getClient() throws IOException{
		properties = new Properties();
		properties.load(TransportClient.class.getClassLoader().getResourceAsStream("env.properties"));
		
		es_ip = properties.getProperty("target.es.ip");
		es_port = Integer.valueOf(properties.getProperty("target.es.port"));
		es_cluster_name = properties.getProperty("target.es.cluster.name");
		System.out.println("原始地址,es_ip = "+es_ip);
		String [] hosts = es_ip.split(",");
		System.out.println("切割之后的esIp数组  = "+hosts);//针对于以后的集群部署我这里只有 一台服务器
		
		Settings settings = Settings.settingsBuilder()
				.put("cluster.name",es_cluster_name)
		.put("client.transport.sniff", true).build();
		client = TransportClient.builder().settings(settings)
				.build();
		for(int i=0;i<hosts.length;i++){
			client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(hosts[i]), es_port));
		}
		return client;
	} 

}

然后创建一个实体类Test.java对应es中的数据

package com.es.demo;

public class Test {
	
	private String author;
	private String content;
	private String title;
	private String category;
	
	public String getAuthor() {
		return author;
	}
	
	public void setAuthor(String author) {
		this.author = author;
	}
	
	public String getContent() {
		return content;
	}
	
	public void setContent(String content) {
		this.content = content;
	}
	
	public String getTitle() {
		return title;
	}
	
	public void setTitle(String title) {
		this.title = title;
	}
	
	public String getCategory() {
		return category;
	}
	
	public void setCategory(String category) {
		this.category = category;
	}

	@Override
	public String toString() {
		return "Test [author=" + author + ", content=" + content + ", title=" + title + ", category=" + category + "]";
	}
	
}

然后再提供一个转换工具类(此类不是多好,有好的大家可以提供下):

package com.es.demo;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Date;
import java.util.Map;

import org.joda.time.DateTime;
import org.joda.time.format.ISODateTimeFormat;
/**
 * 
 * <br>
 * 标题: map转成bean<br>
 * 描述: <br>
 * 公司: www.tydic.com<br>
 * @autho wesley
 * [@time](https://my.oschina.net/u/126678) 2016年10月31日 下午5:03:08
 */
public class ConverterUtil {

	/** 转换为驼峰(大写)
	 * 
	 * [@param](https://my.oschina.net/u/2303379) underscoreName
	 * [@return](https://my.oschina.net/u/556800) */
	public static String camelCaseName(String underscoreName) {
		StringBuilder result = new StringBuilder();
		if (underscoreName != null && underscoreName.length() > 0) {
			boolean flag = false;
			for (int i = 0; i < underscoreName.length(); i++) {
				char ch = underscoreName.charAt(i);
				if ("_".charAt(0) == ch) {
					flag = true;
				}
				else {
					if (flag) {
						result.append(Character.toUpperCase(ch));
						flag = false;
					}
					else {
						result.append(Character.toLowerCase(ch));
					}
				}
			}
		}
		return result.toString();
	}

	/** 转换为下划线(大写)
	 * 
	 * @param camelCaseName
	 * @return */
	public static String underscoreName(String camelCaseName) {
		StringBuilder result = new StringBuilder();
		int len = camelCaseName.length();
		if (camelCaseName != null && len > 0) {
			result.append(camelCaseName.substring(0, 1).toUpperCase());
			for (int i = 1; i < len; i++) {
				char ch = camelCaseName.charAt(i);
				if (Character.isUpperCase(ch)) {
					result.append("_");
					result.append(ch);
				}
				else {
					result.append(Character.toUpperCase(ch));
				}
			}
		}
		return result.toString();
	}

	/** 把Map<String,Object>处理成实体类
	 * 
	 * @param clazz
	 *想要的实体类
	 * @param map
	 *包含信息的Map对象
	 * @return */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public static Object mapToObject(Class clazz, Map<String, Object> map) {
		if (null == map) {
			return null;
		}
		Field[] fields = clazz.getDeclaredFields(); // 取到类下所有的属性,也就是变量名
		Field field;
		Object o = null;
		try {
			o = clazz.newInstance();
		}
		catch (InstantiationException e1) {
			e1.printStackTrace();
		}
		catch (IllegalAccessException e1) {
			e1.printStackTrace();
		}
		for (int i = 0; i < fields.length; i++) {
			field = fields[i];
			String fieldName = field.getName();
			// 把属性的第一个字母处理成大写
			String stringLetter = fieldName.substring(0, 1).toUpperCase();
			// 取得set方法名,比如setBbzt
			String setterName = "set" + stringLetter + fieldName.substring(1);
			// 真正取得set方法。
			Method setMethod = null;
			Class fieldClass = field.getType();
			try {
				if (isHaveSuchMethod(clazz, setterName)) {
					if (fieldClass == String.class) {
						setMethod = clazz.getMethod(setterName, fieldClass);
						if (null != map.get(fieldName) && !("").equals(map.get(fieldName))) {
							setMethod.invoke(o, String.valueOf(map.get(fieldName)));// 为其赋值
						}
					}
					else if (fieldClass == Integer.class || fieldClass == int.class) {
						setMethod = clazz.getMethod(setterName, fieldClass);
						if (null != map.get(fieldName) && !("").equals(map.get(fieldName))) {
							setMethod.invoke(o, Integer.parseInt(String.valueOf(map.get(fieldName))));// 为其赋值
						}
						
					}
					else if (fieldClass == Boolean.class || fieldClass == boolean.class) {
						setMethod = clazz.getMethod(setterName, fieldClass);
						if (null != map.get(fieldName) && !("").equals(map.get(fieldName))) {
							setMethod.invoke(o, Boolean.getBoolean(String.valueOf(map.get(fieldName))));// 为其赋值
						}
						
					}
					else if (fieldClass == Short.class || fieldClass == short.class) {
						setMethod = clazz.getMethod(setterName, fieldClass);
						if (null != map.get(fieldName) && !("").equals(map.get(fieldName))) {
							setMethod.invoke(o, Short.parseShort(String.valueOf(map.get(fieldName))));// 为其赋值
						}
						
					}
					else if (fieldClass == Long.class || fieldClass == long.class) {
						setMethod = clazz.getMethod(setterName, fieldClass);
						if (null != map.get(fieldName) && !("").equals(map.get(fieldName))) {
							setMethod.invoke(o, Long.parseLong(String.valueOf(map.get(fieldName))));// 为其赋值
						}
						
					}
					else if (fieldClass == Double.class || fieldClass == double.class) {
						setMethod = clazz.getMethod(setterName, fieldClass);
						if (null != map.get(fieldName) && !("").equals(map.get(fieldName))) {
							setMethod.invoke(o, Double.parseDouble(String.valueOf(map.get(fieldName))));// 为其赋值
						}
						
					}
					else if (fieldClass == Float.class || fieldClass == float.class) {
						setMethod = clazz.getMethod(setterName, fieldClass);
						if (null != map.get(fieldName) && !("").equals(map.get(fieldName))) {
							setMethod.invoke(o, Float.parseFloat(String.valueOf(map.get(fieldName))));// 为其赋值
						}
						
					}
					else if (fieldClass == BigInteger.class) {
						setMethod = clazz.getMethod(setterName, fieldClass);
						if (null != map.get(fieldName) && !("").equals(map.get(fieldName))) {
							setMethod.invoke(o, BigInteger.valueOf(Long.parseLong(String.valueOf(map.get(fieldName)))));// 为其赋值
						}
						
					}
					else if (fieldClass == BigDecimal.class) {
						setMethod = clazz.getMethod(setterName, fieldClass);
						if (null != map.get(fieldName) && !("").equals(map.get(fieldName))) {
							setMethod.invoke(o, BigDecimal.valueOf(Long.parseLong(String.valueOf(map.get(fieldName)))));// 为其赋值
						}
						
					}
					else if (fieldClass == Date.class) {
						setMethod = clazz.getMethod(setterName, fieldClass);
						if (null != map.get(fieldName) && !("").equals(map.get(fieldName))) {
							DateTime date = ISODateTimeFormat.dateTimeParser().parseDateTime((String) map.get(fieldName));
							setMethod.invoke(o, new Date(date.getMillis()));// 为期赋值
						}
						/*
						 * if (map.get(fieldName).getClass() == java.sql.Date.class) { setMethod.invoke(o, new Date(((java.sql.Date)
						 * map.get(fieldName)).getTime()));// 为其赋值 } else if (map.get(fieldName).getClass() ==
						 * java.sql.Time.class) { setMethod.invoke(o, new Date(((java.sql.Time) map.get(fieldName)).getTime()));// 为其赋值 } else
						 * if (map.get(fieldName).getClass() == java.sql.Timestamp.class) { setMethod.invoke(o, new Date(((java.sql.Timestamp)
						 * map.get(fieldName)).getTime()));// 为其赋值 }else if(map.get(fieldName).getClass() ==
						 * org.joda.time.format.ISODateTimeFormat.class){ DateTime date =
						 * ISODateTimeFormat.dateTimeParser().parseDateTime((String)map.get(fieldName)); setMethod.invoke(o, new
						 * Date(date.getMillis())); }
						 */
					}
				}
			}
			catch (SecurityException e) {
				e.printStackTrace();
			}
			catch (NoSuchMethodException e) {
				e.printStackTrace();
			}
			catch (IllegalArgumentException e) {
				e.printStackTrace();
			}
			catch (IllegalAccessException e) {
				e.printStackTrace();
			}
			catch (InvocationTargetException e) {
				e.printStackTrace();
			}

		}
		return o;
	}

	/** 判断某个类里是否有某个方法
	 * 
	 * @param clazz
	 * @param methodName
	 * @return */
	public static boolean isHaveSuchMethod(Class<?> clazz, String methodName) {
		Method[] methodArray = clazz.getMethods();
		boolean result = false;
		if (null != methodArray) {
			for (int i = 0; i < methodArray.length; i++) {
				if (methodArray[i].getName().equals(methodName)) {
					result = true;
					break;
				}
			}
		}
		return result;
	}

	public static void main(String[] args) {
		System.err.println(underscoreName("name_full"));
		System.err.println(camelCaseName("NAME_FILL"));
		System.err.println(camelCaseName("nameFill"));
		System.err.println(underscoreName("nameFill"));
	}
}

暂时没有封装,直接用main学习演示基本操作的的,代码如下:

package com.es.demo;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.elasticsearch.action.ActionWriteResponse.ShardInfo;
import org.elasticsearch.action.admin.indices.create.CreateIndexRequestBuilder;
import org.elasticsearch.action.admin.indices.create.CreateIndexResponse;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequestBuilder;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexResponse;
import org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest;
import org.elasticsearch.action.admin.indices.mapping.put.PutMappingResponse;
import org.elasticsearch.action.delete.DeleteRequestBuilder;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.index.IndexRequestBuilder;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.indexedscripts.delete.DeleteIndexedScriptRequestBuilder;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.search.SearchType;
import org.elasticsearch.action.update.UpdateRequestBuilder;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.Requests;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;

import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * Hello world!
 *
 */
public class App 
{
    public static void main( String[] args ) throws IOException
    {
    	
    	EsClient esClient = new EsClient();
        TransportClient client = esClient.getClient();
        //---------------演示查询
       /* SearchRequestBuilder request = client.prepareSearch("wesley")
		        .setTypes("test").setSearchType(SearchType.QUERY_THEN_FETCH);
		//初始化查询参数
		BoolQueryBuilder query = QueryBuilders.boolQuery();
		query.must(QueryBuilders.termQuery("author","author"));
        request.setQuery(query);
        //查询操作
        SearchResponse response = request.execute().actionGet();
        List<Test> returnList = new ArrayList<Test>();
		SearchHits hints = response.getHits();
		for(SearchHit theHit : hints){// 每条纪录
			Map<String,Object> testInfo = new HashMap<String,Object>();
			for(Map.Entry<String, Object> entity : theHit.getSource().entrySet()){
				testInfo.put(entity.getKey(), entity.getValue()==null ? null : entity.getValue().toString());// 根据数值大小,value 可能为Integer/Long 不依赖ES映射类型
			}
			returnList.add((Test)ConverterUtil.mapToObject(Test.class, testInfo));
		}
		
		for(Test test:returnList){
			System.out.println("-----------------"+test);
		}*/
		//---------------演示查询------------end
		
		//-----------演示创建索引,类型,文档-----------
        /*ObjectMapper objectMapper = new ObjectMapper();
        Test test = new Test();
        test.setAuthor("author6");
        test.setCategory("category6");
        test.setTitle("title6");
        test.setContent("content6");
        String source = objectMapper.writeValueAsString(test);
        System.out.println("--------"+source);
		IndexRequestBuilder indexBuilder = client.prepareIndex().setIndex("wesley6").setType("test6").setSource(source);
		IndexResponse indexResponse = indexBuilder.execute().actionGet();
		System.out.println("--------"+indexResponse.isCreated()+"----------");*/
		//-----------演示创建索引,类型,文档-----------end
        //------------演示删除文档-------
        /*DeleteRequestBuilder deleteRequestBuilder = client.prepareDelete("wesley6", "test6", "AVmV5176AmysSwOEBaDt");
        DeleteResponse deleteResponse = deleteRequestBuilder.execute().actionGet();
        System.out.println("------isDelete-----"+deleteResponse.isFound());*/
        //------------演示删除文档-------end
        //------------演示删除指定索引-------
        /*DeleteIndexRequestBuilder deleteRequestBuilder = client.admin().indices().prepareDelete("wesley6");
        DeleteIndexResponse deleteResponse = deleteRequestBuilder.execute().actionGet();
        System.out.println("------isDelete-----"+deleteResponse.isAcknowledged());*/
        //------------演示删除指定索引-------end
        //----------演示创建索引,类型
        // 使用XContentBuilder创建Mapping
        /*XContentBuilder builder = 
            XContentFactory.jsonBuilder()
                            .startObject()
                                .field("properties")
                                    .startObject()
                                        .field("name")
                                            .startObject()
                                                .field("index", "not_analyzed")
                                                .field("type", "string")
                                            .endObject()
                                        .field("age")
                                            .startObject()
                                                .field("index", "not_analyzed")
                                                .field("type", "integer")
                                            .endObject()
                                    .endObject()
                            .endObject();
        System.out.println(builder.string());  
        //创建索引
        CreateIndexRequestBuilder createIndexRequestBuilder = client.admin().indices().prepareCreate("wesley00");
        CreateIndexResponse createIndexResponse = createIndexRequestBuilder.execute().actionGet();
        System.out.println("------isCreated----"+createIndexResponse.isAcknowledged());
        //创建type
        PutMappingRequest mappingRequest = Requests.putMappingRequest("wesley00").source(builder).type("user");
        PutMappingResponse putMappingResponse = client.admin().indices().putMapping(mappingRequest).actionGet();
        System.out.println("------isCreated----"+putMappingResponse.isAcknowledged());*/
        //----------演示创建索引,类型---end
        //---------演示更新文档----
        XContentBuilder builder = 
                XContentFactory.jsonBuilder()
                                .startObject()
                                    .field("author", "lisi")
                                    .field("title", "update111")
                                    .field("content","111111")
                                    .field("category","category-update")
                                .endObject();
        	UpdateRequestBuilder updateRequestBuilder = client.prepareUpdate().setIndex("wesley").setType("test").setDoc(builder).setId("1111");
        	System.out.println("----------"+updateRequestBuilder);
        	UpdateResponse updateResponse = updateRequestBuilder.execute().actionGet();
        	System.out.println("---------"+updateResponse);
        	//说明isCreated这个方法如果返回的是true说明原来没有数据重新创建了,如果返回false说明更新成功。不知道他们为什么这么设计。
        	//Returns true if document was created due to an UPSERT operation 源码中的注释说明。
            System.out.println("---------"+updateResponse.isCreated()+"---version-"+updateResponse.getVersion()+"-------------"); //
        ---------演示更新文档----end--
    }
}

最后,稍作总结,上面都是只贴出了代码,也没给出比较详细的解释,这个大家可以参考着官网的介绍,然后结合贴出的代码,估计就能够看懂了。另外,还有其它java api比如聚合查询,暂时也没去研究,大家可以去学习下,我也是刚学,有啥问题的地方欢迎指出,谢谢!

ElasticSearch2.x API变化官网链接 https://www.elastic.co/guide/en/elasticsearch/reference/2.3/breaking-changes-2.0.html

转载于:https://my.oschina.net/zhengweishan/blog/824543

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值