下载elasticsearch的zip包,elasticsearch的版本是2.2.1
ps:elasticsearch的api随版本更新的速度快,这里边需要查看对应版本的api文档
解压后安装,elasticsearch的访问地址:
http://localhost:9200/

{
  "name" : "Venus",
  "cluster_name" : "elasticsearch",
  "version" : {
    "number" : "2.2.1",
    "build_hash" : "d045fc29d1932bce18b2e65ab8b297fbf6cd41a1",
    "build_timestamp" : "2016-03-09T09:38:54Z",
    "build_snapshot" : false,
    "lucene_version" : "5.4.1"
  },
  "tagline" : "You Know, for Search"
}

安装插件:
elasticsearch插件elasticsearch-head安装:
elasticsearch-head是一个elasticsearch的集群管理工具,它是完全由html5编写的独立网页程序,你可以通过插件把它集成到es。
在cmd进入elasticsearch安装的bin目录,执行下边的命令:

plugin install mobz/elasticsearch-head

网上查的命令是:

plugin -install mobz/elasticsearch-head

注意比较不同
安装完命令可以打开url:http://localhost:9200/_plugin/head/ 查看效果

elasticsearch-head

elasticsearch-head


bigdesk这个插件就不要安装了,github上的代码都是几年前的了
pojo类:

package com.rong360.elasticsearch;
public class User {
	private long id;
	private String name;
	private int age;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public long getId() {
		return id;
	}
	public void setId(long id) {
		this.id = id;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
}

elasticsearch建立索引:

package com.rong360.elasticsearch;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.search.SearchType;
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.common.xcontent.XContentFactory;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHits;
public class ElasticSearchClient {
	private static Client client;
	public void init() {
		try {
			client = TransportClient.builder().build()
					.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("localhost"), 9300));
		} catch (UnknownHostException e) {
			e.printStackTrace();
		}
	}
	public void close() {
		client.close();
	}
	public void createIndex() {
		for (int i = 0; i < 1000; i++) {
			User user = new User();
			user.setId(new Long(i));
			user.setName("huang fox " + i);
			user.setAge(i % 100);
			client.prepareIndex("users", "user").setSource(generateJson(user)).execute().actionGet();
		}
	}
	private String generateJson(User user) {
		String json = "";
		try {
			XContentBuilder contentBuilder = XContentFactory.jsonBuilder().startObject();
			contentBuilder.field("id", user.getId() + "");
			contentBuilder.field("name", user.getName());
			contentBuilder.field("age", user.getAge() + "");
			json = contentBuilder.endObject().string();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return json;
	}
	public static void search() {
		SearchResponse response = client.prepareSearch("users").setTypes("user")
				.setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
				.setQuery(QueryBuilders.termQuery("name", "fox")) // Query
				.setPostFilter(QueryBuilders.rangeQuery("age").from(22).to(26))// Filter
				.setFrom(0).setSize(60).setExplain(true).execute().actionGet();
		SearchHits hits = response.getHits();
		System.out.println(hits.getTotalHits());
		for (int i = 0; i < hits.getHits().length; i++) {
			System.out.println(hits.getHits()[i].getSourceAsString());
		}
	}
	public static void main(String[] args) {
		long a = System.currentTimeMillis();
		ElasticSearchClient elasticSearchClient = new ElasticSearchClient();
		elasticSearchClient.init();
		System.out.println("监测的时间1:"+(System.currentTimeMillis()-a)/1000f+" 秒 ");
		elasticSearchClient.createIndex();	//创建索引
		System.out.println("监测的时间2:"+(System.currentTimeMillis()-a)/1000f+" 秒 ");
		search();	//查询
		System.out.println("监测的时间3:"+(System.currentTimeMillis()-a)/1000f+" 秒 ");
		elasticSearchClient.close();
	}
}
elasticsearch_demo

elasticsearch_demo