Elasticsearch 学习笔记

安装Elasticsearch

1:解压下载的安装包elasticsearch-1.7.2.zip

修改cluster.name:es(集群状态名字一致)

2:在https://github.com/elasticsearch/elasticsearch-servicewrapper下载该插件后,解压缩。将service目录拷贝到elasticsearch目录的bin目录下。

3:安装es-head插件

进入elasticsearch/bin目录,输入命令./plugin--installmobz/elasticsearch-head安装head插件

4:将Elasticsearchscp到其他机器

5:启动Elasticsearch

在linux环境,进入ES目录的bin/service目录,如下图,运行命令shelasticsearchstart访问地址http://hadoop1:9200/_plugin/head/

ElasticsearchJavaAPI

添加Elasticsearchmaven依赖

<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());
		}
	}

}

相关推荐