首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >Elasticsearch极速入门

Elasticsearch极速入门

作者头像
青山师
发布2023-05-05 19:54:27
发布2023-05-05 19:54:27
9050
举报

下载 ES

https://www.elastic.co/cn/downloads/

安装

解压进入bin,执行elasticsearch.bat,启动 端口 9300 程序交互 端口 9200 客户端交互 localhst:9200 看到信息:

代码语言:javascript
复制
{
  "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:

代码语言:javascript
复制
npm install -g grunt-cli

进入 elasticsearch-head 根目录使用 nodejs 命令:

代码语言:javascript
复制
npm install
grunt server

可以看到输出:

代码语言:javascript
复制
>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,增加以下三句命令:

代码语言:javascript
复制
http.cors.enabled: true
http.cors.allow-origin: "*"
network.host: 127.0.0.1

重启 ES。

程序Demo

maven项目,添加依赖:

代码语言:javascript
复制
<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>

编写测试小程序:

代码语言:javascript
复制
@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();

浏览器可以看到数据信息:

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2021-04-11,如有侵权请联系 cloudcommunity@tencent.com 删除
目录
  • 下载 ES
  • 安装
    • 程序Demo
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档