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
相关推荐
消费kafka数据,然后批量导入到Elasticsearch,本例子使用的kafka版本0.10,es版本是6.4,使用bulk方式批量导入到es中,也可以一条一条的导入,不过比较慢。 <dependency> <groupId>org.elasticsearch</groupId> ...
<elasticsearch.version>5.6.0</elasticsearch.version> <spring-version>5.0.0.RC3</spring-version> <fastjson-version>1.2.7</fastjson-version> <junit-version>4.12</junit-version> <slf4j-version>1.7.5...
<groupId>org.elasticsearch</groupId> <artifactId>elasticsearch</artifactId> <version>5.4.2</version> <!--<version>6.5.4</version>--> </dependency> <!-- 添加字段需要的JAR --> <!...
<elasticsearch.version>1.7.1</elasticsearch.version> </properties> <dependencies> <dependency> <groupId>org.apache.lucene</groupId> <artifactId>lucene-core</artifactId> <version>4.10.4</version...
<spring.data.elasticsearch.version>3.1.0.RELEASE</spring.data.elasticsearch.version> </properties> <dependencies> <dependency> <groupId>org.elasticsearch</groupId> <artifactId>elasticsearch</...
Elasticsearch是一个分布式、RESTful风格的搜索和数据分析引擎,被广泛用于实时的索引和搜索,尤其在大数据分析和实时数据检索方面表现卓越。它使用Java开发,并且提供了丰富的Java API供开发者进行操作。本实例主要...
<groupId>org.elasticsearch.client</groupId> <artifactId>transport</artifactId> <version>${elasticsearch.version}</version> </dependency> ``` 接着,我们需要在项目中创建一个配置类,如`ESConfig`,...
<groupId>org.elasticsearch</groupId> <artifactId>elasticsearch</artifactId> <version>版本号</version> </dependency> <!-- Spring Data Elasticsearch dependencies --> <dependency> <groupId>org....
<artifactId>spring-boot-starter-data-elasticsearch</artifactId> <version>${spring-boot.version}</version> </dependency> 2、配置文件 spring: application: name: ware-elastic-search data: elastic...
<groupId>org.elasticsearch</groupId> <artifactId>elasticsearch</artifactId> <version>5.5.0</version> </dependency> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data...
<artifactId>elasticsearch-rest-high-level-client</artifactId> <version>7.8.1</version> </dependency> <dependency> <groupId>org.elasticsearch</groupId> <artifactId>elasticsearch</artifactId> ...
<artifactId>spring-boot-starter-data-elasticsearch</artifactId> <version>YOUR_SPRINGBOOT_VERSION</version> </dependency> ``` 接着,配置Elasticsearch连接信息。在`application.properties`文件中添加...
Elasticsearch是一个流行的开源搜索引擎,它也依赖于Maven进行构建。在`elasticsearch-master`这个压缩包中,可能包含了Elasticsearch项目的源码和构建文件。如果你打算参与Elasticsearch的开发或自定义配置,理解...
<artifactId>elasticsearch-spring-boot-starter</artifactId> <version>1.2.0.RELEASE</version> </dependency> 2)application.yml spring: elasticsearch: cluster-nodes: 192.168.19.131:9300
<artifactId>spring-boot-starter-data-elasticsearch</artifactId> </dependency> <dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>elasticsearch-rest-high-level-client</artifactId...
<groupId>org.elasticsearch</groupId> <artifactId>elasticsearch</artifactId> <version>6.1.2</version> </dependency> ``` 接着,引入Spring Data Elasticsearch依赖: ```xml <dependency> <groupId>org....
在本项目"Demo_springboot_ES_mybatis操作es_DEMO_mybatis"中,我们将探讨如何集成SpringBoot、MyBatis以及Elasticsearch(ES)框架,以实现对ES数据库的基本操作。SpringBoot以其便捷的配置和强大的依赖管理,极大...
<groupId>org.elasticsearch</groupId> <artifactId>elasticsearch</artifactId> <version>6.3.2</version> </dependency> ``` 对于Gradle,对应的`build.gradle`配置为: ```groovy implementation 'org....
在描述中提到的场景,用户在研究Elasticsearch(ES)的过程中遇到了Maven插件下载的问题。虽然Elasticsearch本身并不直接与Maven DB Plugin关联,但可能是在配置Elasticsearch的环境或集成测试中,需要用到这个插件...