`
ivan19861025
  • 浏览: 42020 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

Elasticsearch 学习之旅 <一> 初体验

    博客分类:
  • Java
阅读更多

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

	<repositories>
		<repository>
			<id>spring-milestone</id>
			<name>Spring Maven MILESTONE Repository</name>
			<url>http://repo.spring.io/libs-milestone</url>
		</repository>
		<repository>
			<id>com.springsource.repository.maven.release</id>
			<url>http://maven.springframework.org/release/</url>
			<snapshots>
				<enabled>false</enabled>
			</snapshots>
		</repository>
		<repository>
			<id>oracleReleases</id>
			<name>Oracle Released Java Packages</name>
			<url>http://download.oracle.com/maven</url>
		</repository>
		<repository>
			<id>JBossRepo1</id>
			<name>Jboss1</name>
			<url>https://repository.jboss.org/nexus/content/groups/public-jboss/</url>
		</repository>
		<repository>
			<id>JBossRepo</id>
			<name>Jboss</name>
			<url>https://repository.jboss.org/nexus/content/repositories/releases/</url>
		</repository>
	</repositories>

	<dependencies>
		<dependency>
			<groupId>org.elasticsearch</groupId>
			<artifactId>elasticsearch</artifactId>
			<version>1.7.1</version>
		</dependency>
		<dependency>
            <groupId>net.sf.json-lib</groupId>
            <artifactId>json-lib</artifactId>
            <version>2.4</version>
            <classifier>jdk15</classifier>
        </dependency>
	</dependencies>

 

 更多JAVA API, 请参见 https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/index.html

分享到:
评论

相关推荐

    java语言kafka数据批量导入到Elasticsearch实例

    消费kafka数据,然后批量导入到Elasticsearch,本例子使用的kafka版本0.10,es版本是6.4,使用bulk方式批量导入到es中,也可以一条一条的导入,不过比较慢。 &lt;dependency&gt; &lt;groupId&gt;org.elasticsearch&lt;/groupId&gt; ...

    elasticsearch学习demo

    &lt;elasticsearch.version&gt;5.6.0&lt;/elasticsearch.version&gt; &lt;spring-version&gt;5.0.0.RC3&lt;/spring-version&gt; &lt;fastjson-version&gt;1.2.7&lt;/fastjson-version&gt; &lt;junit-version&gt;4.12&lt;/junit-version&gt; &lt;slf4j-version&gt;1.7.5...

    Java操作ElasticSearch工具类【支持5.0+以上所有版本】

    &lt;groupId&gt;org.elasticsearch&lt;/groupId&gt; &lt;artifactId&gt;elasticsearch&lt;/artifactId&gt; &lt;version&gt;5.4.2&lt;/version&gt; &lt;!--&lt;version&gt;6.5.4&lt;/version&gt;--&gt; &lt;/dependency&gt; &lt;!-- 添加字段需要的JAR --&gt; &lt;!...

    开发ElasticSearch的HanLP中文插件含截图

    &lt;elasticsearch.version&gt;1.7.1&lt;/elasticsearch.version&gt; &lt;/properties&gt; &lt;dependencies&gt; &lt;dependency&gt; &lt;groupId&gt;org.apache.lucene&lt;/groupId&gt; &lt;artifactId&gt;lucene-core&lt;/artifactId&gt; &lt;version&gt;4.10.4&lt;/version...

    SpringBoot Elasticsearch.docx

    &lt;spring.data.elasticsearch.version&gt;3.1.0.RELEASE&lt;/spring.data.elasticsearch.version&gt; &lt;/properties&gt; &lt;dependencies&gt; &lt;dependency&gt; &lt;groupId&gt;org.elasticsearch&lt;/groupId&gt; &lt;artifactId&gt;elasticsearch&lt;/...

    elasticsearch java操作的api实例

    Elasticsearch是一个分布式、RESTful风格的搜索和数据分析引擎,被广泛用于实时的索引和搜索,尤其在大数据分析和实时数据检索方面表现卓越。它使用Java开发,并且提供了丰富的Java API供开发者进行操作。本实例主要...

    Spring Boot 集成 ElasticSearch.docx

    &lt;groupId&gt;org.elasticsearch.client&lt;/groupId&gt; &lt;artifactId&gt;transport&lt;/artifactId&gt; &lt;version&gt;${elasticsearch.version}&lt;/version&gt; &lt;/dependency&gt; ``` 接着,我们需要在项目中创建一个配置类,如`ESConfig`,...

    springMVC整合elasticsearch,基于maven

    &lt;groupId&gt;org.elasticsearch&lt;/groupId&gt; &lt;artifactId&gt;elasticsearch&lt;/artifactId&gt; &lt;version&gt;版本号&lt;/version&gt; &lt;/dependency&gt; &lt;!-- Spring Data Elasticsearch dependencies --&gt; &lt;dependency&gt; &lt;groupId&gt;org....

    springboot整合ElasticSearch,实现高性能搜索引擎

    &lt;artifactId&gt;spring-boot-starter-data-elasticsearch&lt;/artifactId&gt; &lt;version&gt;${spring-boot.version}&lt;/version&gt; &lt;/dependency&gt; 2、配置文件 spring: application: name: ware-elastic-search data: elastic...

    ranger-2.0.0-elasticsearch-plugin.tar.gz

    3. **配置Ranger**: 在Ranger管理界面创建一个新的服务定义,选择Elasticsearch类型,并配置连接Elasticsearch实例的参数,如集群名称、节点地址等。 4. **配置Elasticsearch**: 更新Elasticsearch的配置文件(如`...

    spring mvc 集成elasticSearch 5.5.0版本

    &lt;groupId&gt;org.elasticsearch&lt;/groupId&gt; &lt;artifactId&gt;elasticsearch&lt;/artifactId&gt; &lt;version&gt;5.5.0&lt;/version&gt; &lt;/dependency&gt; &lt;dependency&gt; &lt;groupId&gt;org.springframework.data&lt;/groupId&gt; &lt;artifactId&gt;spring-data...

    Spring Boot整合ElasticSearch和Mysql 附案例源码.docx

    &lt;artifactId&gt;elasticsearch-rest-high-level-client&lt;/artifactId&gt; &lt;version&gt;7.8.1&lt;/version&gt; &lt;/dependency&gt; &lt;dependency&gt; &lt;groupId&gt;org.elasticsearch&lt;/groupId&gt; &lt;artifactId&gt;elasticsearch&lt;/artifactId&gt; ...

    elasticsearch.zip

    &lt;artifactId&gt;spring-boot-starter-data-elasticsearch&lt;/artifactId&gt; &lt;version&gt;YOUR_SPRINGBOOT_VERSION&lt;/version&gt; &lt;/dependency&gt; ``` 接着,配置Elasticsearch连接信息。在`application.properties`文件中添加...

    Maven一些相关的配置

    Elasticsearch是一个流行的开源搜索引擎,它也依赖于Maven进行构建。在`elasticsearch-master`这个压缩包中,可能包含了Elasticsearch项目的源码和构建文件。如果你打算参与Elasticsearch的开发或自定义配置,理解...

    elasticsearch-spring-boot-starter-1.2.0.RELEASE.jar

    &lt;artifactId&gt;elasticsearch-spring-boot-starter&lt;/artifactId&gt; &lt;version&gt;1.2.0.RELEASE&lt;/version&gt; &lt;/dependency&gt; 2)application.yml spring: elasticsearch: cluster-nodes: 192.168.19.131:9300

    springboot 集成elasticsearch ,api的使用

    &lt;artifactId&gt;spring-boot-starter-data-elasticsearch&lt;/artifactId&gt; &lt;/dependency&gt; &lt;dependency&gt; &lt;groupId&gt;org.elasticsearch.client&lt;/groupId&gt; &lt;artifactId&gt;elasticsearch-rest-high-level-client&lt;/artifactId...

    elasticsearch+spring小案例

    &lt;groupId&gt;org.elasticsearch&lt;/groupId&gt; &lt;artifactId&gt;elasticsearch&lt;/artifactId&gt; &lt;version&gt;6.1.2&lt;/version&gt; &lt;/dependency&gt; ``` 接着,引入Spring Data Elasticsearch依赖: ```xml &lt;dependency&gt; &lt;groupId&gt;org....

    demo_springboot_ES_mybatis操作es_DEMO_mybatis_

    在本项目"Demo_springboot_ES_mybatis操作es_DEMO_mybatis"中,我们将探讨如何集成SpringBoot、MyBatis以及Elasticsearch(ES)框架,以实现对ES数据库的基本操作。SpringBoot以其便捷的配置和强大的依赖管理,极大...

    elasticsearch6.3.2_java开发依赖

    &lt;groupId&gt;org.elasticsearch&lt;/groupId&gt; &lt;artifactId&gt;elasticsearch&lt;/artifactId&gt; &lt;version&gt;6.3.2&lt;/version&gt; &lt;/dependency&gt; ``` 对于Gradle,对应的`build.gradle`配置为: ```groovy implementation 'org....

Global site tag (gtag.js) - Google Analytics