Elasticsearch极速入门
下载 ES
https://www.elastic.co/cn/downloads/
安装
解压进入bin,执行elasticsearch.bat,启动
端口 9300 程序交互
端口 9200 客户端交互
localhst:9200 看到信息:
{
"name" : "WB3S2bt",
"cluster_name" : "elasticsearch",
"cluster_uuid" : "1c26M_I9SB6VkvxBjJMNhw",
"version" : {
"number" : "6.8.0",
"build_flavor" : "default",
"build_type" : "zip",
"build_hash" : "65b6179",
"build_date" : "2019-05-15T20:06:13.172855Z",
"build_snapshot" : false,
"lucene_version" : "7.7.0",
"minimum_wire_compatibility_version" : "5.6.0",
"minimum_index_compatibility_version" : "5.0.0"
},
"tagline" : "You Know, for Search"
}
安装可视化界面插件 head
https://github.com/mobz/elasticsearch-head
下载nodejs:https://nodejs.org/en/download
使用node安装grunt:
npm install -g grunt-cli
进入 elasticsearch-head 根目录使用 nodejs 命令:
npm install
grunt server
可以看到输出:
>grunt server
>> Local Npm module "grunt-contrib-jasmine" not found. Is it installed?
Running "connect:server" (connect) task
Waiting forever...
Started connect web server on http://localhost:9100
打开 http://localhost:9100 可以看到可视化web界面,9100如果访问出错或者看不到数据信息,浏览器中按F12会发现控制台报错,则设置下允许es跨域:修改elasticsearch/config下的配置文件:elasticsearch.yml,增加以下三句命令:
http.cors.enabled: true
http.cors.allow-origin: "*"
network.host: 127.0.0.1
重启 ES。
程序Demo
maven项目,添加依赖:
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<!--ES包-->
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>5.6.8</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>transport</artifactId>
<version>5.6.8</version>
</dependency>
<!--日志包-->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-to-slf4j</artifactId>
<version>2.9.1</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.24</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.21</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.12</version>
</dependency>
<!--测试包-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
编写测试小程序:
@Test
public void test01() throws Exception {
TransportClient transportClient = new PreBuiltTransportClient(Settings.EMPTY);
transportClient.addTransportAddress(
new InetSocketTransportAddress(InetAddress.getByName("localhost"), 9300));
XContentBuilder xContentBuilder = XContentFactory.jsonBuilder().startObject();
xContentBuilder.field("id", 1)
.field("title", "ES简介")
.field("content", "它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口。Elasticsearch是用Java开发的,并作为Apache许可条款下的开放源码发布,是当前流行的企业级搜索引擎。设计用于云计算中,能够达到实时搜索,稳定,可靠,快速,安装使用方便。");
xContentBuilder.endObject();
transportClient.prepareIndex("blog1", "article", "1")
.setSource(xContentBuilder).get();
transportClient.close();
浏览器可以看到数据信息:
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix