首页 > 其他分享 >ElasticSearch

ElasticSearch

时间:2023-11-23 21:01:40浏览次数:27  
标签:request throws client IOException ElasticSearch book es

环境准备

1.安装ElasticSearch

# 创建网络
docker network create es-net
docker network ls
# 拉取镜像
docker pull elasticsearch:7.12.1
# 创建容器
docker run -d \
--name es \
-e "ES_JAVA_OPTS=-Xms512m -Xmx512m" \
-e "discovery.type=single-node" \
-v es-data:/usr/share/elasticsearch/data \
-v es-plugins:/usr/share/elasticsearch/plugins \
--privileged \
--network es-net \
-p 9200:9200 \
-p 9300:9300 \
elasticsearch:7.12.1

2.安装kibana

# 拉取镜像
docker pull kibana:7.12.1
# 创建容器
docker run -d \
--name kibana \
-e ELASTICSEARCH_HOSTS=http://es:9200 \
--network es-net \
-p 5601:5601 \
kibana:7.12.1

3.安装IK分词器插件

# 1.从github下载IK分词插件
# https://github.com/medcl/elasticsearch-analysis-ik/releases/tag/v7.12.1
# 2.查看卷(找到es容器插件的位置)
docker volume inspect es-plugins
# /var/lib/docker/volumes/es-plugins/_data
# 3.将解压后的目录上传到插件目录
# 4.重启es
docker restart es
# 5.测试
# http://10.10.0.100:5601/app/dev_tools#/console
GET /_analyze
{
  "text": "我爱中国ok",
  "analyzer": "ik_smart"
}
# analyzer取ik_smart表示智能切分,粗粒度;取ik_max_word表示最细切分,细粒度。
# 6.自定义词典
# 修改插件目录下配置文件
config/IKAnalyzer.cfg.xml
<!--用户可以在这里配置自己的扩展字典 -->
<entry key="ext_dict">ext.dic</entry>
<!--用户可以在这里配置自己的扩展停止词字典-->
<entry key="ext_stopwords">stopword.dic</entry>
# .dic是词典文件,内容可自定义
# 修改后需重启es容器

索引库的操作

1.使用kibana的dev tools操作索引库

(1)创建索引库

格式:PUT /索引库名

举例:

PUT /book
{
  "mappings": {
    "properties": {
      "name":{
        "type": "text",
        "analyzer": "ik_smart"
      },
      "price":{
        "type": "float"
      },
      "author":{
        "properties": {
          "firstname":{
            "type":"keyword"
          },
          "lastname":{
            "type":"keyword"
          }
        }
      }
    }
  }
}

(2)查看索引库

格式:GET /索引库名
举例:GET /book

(3)删除索引库

格式:DELETE /索引库名
举例:DELETE /book

2.使用RestClient操作索引库

(1)导入依赖

	<dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>elasticsearch-rest-high-level-client</artifactId>
            <version>7.12.1</version>
        </dependency>

注意:RestClient组件的版本必须和ES版本一致

(2)Java编程

点击查看代码
public class RestClientCRUD {
    RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(new HttpHost("10.10.0.100", 9200, "http")));

    /**
     * 查
     * @throws IOException
     */
    @Test
    public void test1() throws IOException {

        GetRequest request = new GetRequest("book", "1");
        GetResponse response = client.get(request, RequestOptions.DEFAULT);
        String jsonStr = response.getSourceAsString();
        System.out.println(jsonStr);
        Book book = JSON.parseObject(jsonStr, Book.class);  //将json字符串转换为java对象
        System.out.println(book.toString());
        client.close();
    }

    /**
     * 增
     * @throws IOException
     */
    @Test
    public void test2() throws IOException {
        String jsonStr="{" +
                "  \"name\":\"mysql数据库教程\"," +
                "  \"author\":{" +
                "    \"firstname\":\"诸葛\"," +
                "    \"lastname\":\"亮\"" +
                "  }," +
                "  \"price\":213," +
                "  \"category\":\"计算机类\"" +
                "}";
        IndexRequest request = new IndexRequest("book");
        request.id("1");
        request.source(jsonStr, XContentType.JSON);
        client.index(request,RequestOptions.DEFAULT);
        client.close();
    }

    /**
     * 改
     * @throws IOException
     */
    @Test
    public void test3() throws IOException {
        UpdateRequest request = new UpdateRequest("book", "1");
        request.doc("price",999,"name","Spring Cloud微服务");
        client.update(request,RequestOptions.DEFAULT);
        client.close();
    }

    /**
     * 删
     * @throws IOException
     */
    @Test
    public void test4() throws IOException {
        DeleteRequest request = new DeleteRequest("book", "2");
        client.delete(request,RequestOptions.DEFAULT);
        client.close();
    }

    /**
     * 批量添加
     * @throws IOException
     */
    @Test
    public void test5() throws IOException {
        String jsonStr1="{" +
                "  \"name\":\"Java web教程\"," +
                "  \"author\":{" +
                "    \"firstname\":\"赵\"," +
                "    \"lastname\":\"云\"" +
                "  }," +
                "  \"price\":219," +
                "  \"category\":\"计算机类\"" +
                "}";
        String jsonStr2="{" +
                "  \"name\":\"hadoop教程\"," +
                "  \"author\":{" +
                "    \"firstname\":\"关\"," +
                "    \"lastname\":\"羽\"" +
                "  }," +
                "  \"price\":413," +
                "  \"category\":\"计算机类\"" +
                "}";

        IndexRequest request1 = new IndexRequest("book").id("101").source(jsonStr1,XContentType.JSON);
        IndexRequest request2 = new IndexRequest("book").id("102").source(jsonStr2,XContentType.JSON);

        BulkRequest bulkRequest = new BulkRequest();
        bulkRequest.add(request1).add(request2);
        client.bulk(bulkRequest,RequestOptions.DEFAULT);
        client.close();
    }

}

标签:request,throws,client,IOException,ElasticSearch,book,es
From: https://www.cnblogs.com/beast-king/p/17852447.html

相关文章

  • Docker中使用elasticsearch
    Docker中使用elasticsearch1、docker拉取elasticsearch:7.17镜像这里我们拉取7.17.10版本:dockerpullelasticsearch:7.17.102、创建自己的配置文件并写入基础数据供后续挂载后直接启动使用【非必选,在不指定挂载配置文件启动的情况下可不设置】创建文件夹后,新建一个自己的e......
  • ElasticSearch的安装和使用
    ElasticSearch的安装和使用elasticsearch安装步骤1、下载elasticsearch-7.2.0并解压缩将elasticsearch解压缩到/usr/localtar-zxvfelasticsearch.tar.gz-C/usr/local/2、创建es的用户和用户组由于es不能通过root用户来启动,所以需要创建一个非root的es用户和用户组grou......
  • 【主流技术】详解 Spring Boot 2.7.x 集成 ElasticSearch7.x 全过程(二)
    目录前言一、添加依赖二、yml配置三、注入依赖四、CRUD常用APIES实体类documents操作常见条件查询(重点)分页查询排序构造查询测试调用五、文章小结前言ElasticSearch简称es,是一个开源的高扩展的分布式全文检索引擎,目前最新版本已经到了8.11.x了。它可以近乎实时的存储、......
  • ElasticSearch之安装
    参照InstallingElasticsearch,完成验证集群的部署。操作步骤下载软件包和摘要文件。wgethttps://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-8.11.1-linux-x86_64.tar.gzwgethttps://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-8.11.......
  • 如何确定Elasticsearch的副本和分片设置
    Elasticsearch是一个开源的分布式搜索和分析引擎,它使用分片和副本来实现数据的分布式存储和高可用性。在配置Elasticsearch的副本和分片时,需要考虑数据的大小、查询负载、硬件资源等多个因素。本文将详细介绍如何确定Elasticsearch的副本和分片设置。分片和副本的概念在Elasticsear......
  • Elasticsearch 系列(二)- ES的基本概念
    本章将和大家分享Elasticsearch的一些基本概念。话不多说,下面我们直接进入主题。一、什么是LuceneLucene是Apache的开源搜索引擎类库,提供了搜索引擎的核心API。1、Lucene的优势:易扩展、高性能(基于倒排索引)2、Lucene的缺点:只限于Java语言开发、学习曲线陡峭、不支持水平扩展......
  • Node.js精进(12)——ElasticSearch
    ElasticSearch(简称ES)是一款基于Lucene的分布式、可扩展、RESTful风格的全文检索和数据分析引擎,擅长实时处理PB级别的数据。一、基本概念1)LuceneLucene是一款开源免费、成熟权威、高性能的全文检索库,是ES实现全文检索的核心基础,而检索的关键正是倒排索引。2)倒......
  • Elasticsearch入门
    1、什么是Elasticsearch?Elasticsearch是基于Lucene的Restful的分布式实时全文搜索引擎,每个字段都被索引并可被搜索,可以快速存储、搜索、分析海量的数据。全文检索是指对每一个词建立一个索引,指明该词在文章中出现的次数和位置。当查询时,根据事先建立的索引进行查找,并将查找......
  • org.elasticsearch.client.transport.NoNodeAvailableException: None of the configu
    org.elasticsearch.client.transport.NoNodeAvailableException:Noneoftheconfigurednodesareavailableelasticsearch有两个端口:http_port和transport.tcp.port①http_port是ES节点与外部通讯使用的端口。它是http协议的RESTful接口(各种CRUD操作都是走的该端口)默认9200......
  • 统一日志管理方案:Spring项目logback日志与logstash和Elasticsearch整合
    原创/朱季谦 最近在做一个将分布式系统的日志数据通过logstash传到kafka的功能,做完之后决定业余搭一个ELK日志分析系统,将logstash采集到的日志传给Elasticsearch。经过一番捣鼓,也把这个过程给走通了,于是写了这篇总结,可按照以下步骤搭建logstash采集spring日志数据并传输给Elastics......