Elasticsearch极速入门

编程知识 更新时间:2023-05-03 04:00:44

文章目录

  • 下载 ES
  • 安装
    • 程序Demo

下载 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/mobz/elasticsearch-head
下载nodejs:https://nodejs/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>
        <mavenpiler.source>8</mavenpiler.source>
        <mavenpiler.target>8</mavenpiler.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();

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

更多推荐

Elasticsearch极速入门

本文发布于:2023-04-30 12:18:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/1a9d8be9eb101ffae75e94e4107bf8f3.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:极速   入门   Elasticsearch

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!

  • 114442文章数
  • 28947阅读数
  • 0评论数