1. 安装
Elasticsearch 安装非常简单, 下载, 解压, 启动, 搞定
wget https://download.elastic.co/elasticsearch/elasticsearch/elasticsearch-1.7.1.zip
unzip elasticsearch-1.7.1.zip
cd elasticsearch-1.7.1/bin
./elasticsearch
启动日志如下, 表示成功
[2015-09-02 09:55:28,779][INFO ][http ] [Eleggua] bound_address {inet[/0.0.0.0:9202]}, publish_address {inet[/192.168.0.101:9202]}
[2015-09-02 09:55:28,779][INFO ][node ] [Eleggua] started
打开浏览器访问 http://192.168.0.101:9202, 响应如下:
{ "status" : 200, "name" : "Eleggua", "cluster_name" : "elasticsearch", "version" : { "number" : "1.7.1", "build_hash" : "b88f43fc40b0bcd7f173a1f9ee2e97816de80b19", "build_timestamp" : "2015-07-29T09:54:16Z", "build_snapshot" : false, "lucene_version" : "4.10.4" }, "tagline" : "You Know, for Search" }
2. 使用JAVA API 构建索引,查询
public class Student { private int id; private String name; private String address; private int age; public Student(int id, int age, String name, String address){ this.id = id; this.age = age; this.name = name; this.address = address; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
import net.sf.json.JSONObject; import org.elasticsearch.action.get.GetResponse; import org.elasticsearch.action.index.IndexResponse; import org.elasticsearch.action.search.SearchRequestBuilder; import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.action.search.SearchType; import org.elasticsearch.client.transport.TransportClient; import org.elasticsearch.common.settings.ImmutableSettings; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.transport.InetSocketTransportAddress; import org.elasticsearch.index.query.QueryBuilders; import com.league.elasticsearch.pojo.Student; public class App { private static final String SERVER = "192.168.0.101"; public static void main(String[] args) { // 创建连接的客户端 Settings settings = ImmutableSettings.settingsBuilder().put("client.transport.sniff", true).put("cluster.name", "elasticsearch") .build(); TransportClient client = new TransportClient(settings); client.addTransportAddress(new InetSocketTransportAddress(SERVER, 9300)); //createIndex(client); //getData(client); query(client); client.close(); } // 创建索引 public static void createIndex(TransportClient client) { Student student = new Student(103161066, 20, "zhangsan", "beijing of china"); String jsonValue = JSONObject.fromObject(student).toString(); IndexResponse response = client.prepareIndex("student_index", "student_info", "stu_103161066").setSource(jsonValue).execute().actionGet(); System.out.println(response.getId()); } //获取数据 public static void getData(TransportClient client){ GetResponse responseGet = client.prepareGet("student_index", "student_info", "stu_103161066").execute().actionGet(); System.out.println(responseGet.getSourceAsString()); } public static void query(TransportClient client){ SearchRequestBuilder builder = client.prepareSearch("student_index").setTypes("student_info").setSearchType(SearchType.DEFAULT).setFrom(0).setSize(100); builder.setQuery(QueryBuilders.termQuery("address", "beijing")); SearchResponse response = builder.execute().actionGet(); System.out.println("response: " + response); System.out.println("======================="); System.out.println(response.getHits().getTotalHits()); System.out.println("======================="); System.out.println(response.getHits().getHits()[0].getSourceAsString()); } }
MAVEN POM.XML
spring-milestone Spring Maven MILESTONE Repository http://repo.spring.io/libs-milestone com.springsource.repository.maven.release http://maven.springframework.org/release/ false oracleReleases Oracle Released Java Packages http://download.oracle.com/maven JBossRepo1 Jboss1 https://repository.jboss.org/nexus/content/groups/public-jboss/ JBossRepo Jboss https://repository.jboss.org/nexus/content/repositories/releases/ org.elasticsearch elasticsearch 1.7.1 net.sf.json-lib json-lib 2.4 jdk15
更多JAVA API, 请参见 https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/index.html