Elasticsearch 学习笔记
- - 研发管理 - ITeye博客安装 Elasticsearch. 1:解压下载的安装包 elasticsearch-1.7.2.zip. 修改 node.name: es(集群状态名字一致). 2:在https://github.com/elasticsearch/elasticsearch-servicewrapper下载该插件后,解压缩.
<dependency> <groupId>org.elasticsearch</groupId> <artifactId>elasticsearch</artifactId> <version>1.7.2</version> </dependency>
package test; import java.io.IOException; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentFactory; import entity.User; /* * 返回json 数据 */ public class JsonUtil { public static String obj2JsonData(User user){ String jsonData = null; try { //使用XContentBuilder创建json数据 XContentBuilder jsonBuild = XContentFactory.jsonBuilder(); jsonBuild.startObject() .field("id",user.getId()) .field("name", user.getName()) .field("pwd",user.getPwd()) .endObject(); jsonData = jsonBuild.string(); System.out.println(jsonData); } catch (IOException e) { e.printStackTrace(); } return jsonData; } } package test; import java.net.InetSocketAddress; import java.util.ArrayList; import java.util.List; import org.elasticsearch.action.index.IndexResponse; import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.client.Client; import org.elasticsearch.client.transport.TransportClient; import org.elasticsearch.common.transport.InetSocketTransportAddress; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.index.query.QueryBuilder; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.search.SearchHit; import org.elasticsearch.search.SearchHits; import entity.User; public class ElasticsearchTest { Client client; /* * 初始化client */ public ElasticsearchTest() { client = new TransportClient() .addTransportAddress(new InetSocketTransportAddress("hadoop1",9300)); } /* * 添加 */ public IndexResponse createIndexResponse(String indexname, String type, String jsondata) { IndexResponse response = client.prepareIndex(indexname, type) .setSource(jsondata).execute().actionGet(); return response; } /** * 执行搜索 * * @param queryBuilder * @param indexname * @param type * @return */ public List<User> searcher(QueryBuilder queryBuilder, String indexname, String type) { List<User> list = new ArrayList<User>(); SearchResponse searchResponse = client.prepareSearch(indexname) .setTypes(type).setQuery(queryBuilder).execute().actionGet(); SearchHits hits = searchResponse.getHits(); System.out.println("查询到记录数=" + hits.getTotalHits()); SearchHit[] searchHists = hits.getHits(); if (searchHists.length > 0) { for (SearchHit hit : searchHists) { Integer id = (Integer) hit.getSource().get("id"); String name = (String) hit.getSource().get("name"); String function = (String) hit.getSource().get("pwd"); list.add(new User(id, name, function)); } } return list; } public static void main(String[] args) { ElasticsearchTest es = new ElasticsearchTest(); // User user = new User(); // user.setId(1); // user.setName("张三"); // user.setPwd("123"); // String jsondata = JsonUtil.obj2JsonData(user); String indexname = "indexdemo"; String type = "typedemo"; // es.createIndexResponse(indexname, type, jsondata); // 查询条件 QueryBuilder queryBuilder = QueryBuilders.termQuery("name", "张"); /* * QueryBuilder queryBuilder = QueryBuilders.boolQuery() * .must(QueryBuilders.termQuery("id", 1)); */ List<User> result = es.searcher(queryBuilder, indexname, type); for (User us : result) { System.out.println(us.getName()); } } }