elasticsearch简易使用(自用)

一、介绍

非关系型数据库 + 搜索分析

分布式(在ES里面默认有一个配置,clustername 默认值就是ElasticSearch,如果这个值是一样的就属于同一个集群,不一样的值就是不一样的集群。)

        primary shard:能正常提供查询和插入的分片

        replica shard:备份的分片

倒排索引

1.1名词

index索引(索引库,相当于database)

type类型(相当于table,最新版elasticsearch已删除)

document文档(一条数据)

field字段(相当于列)

1.2数据类型

数据类型,参照JSON。

Keyword 类型是不会分词的,直接根据字符串内容建立反向索引。

Text 类型在存入 Elasticsearch 的时候,会先分词,然后根据分词后的内容建立反向索引。

二、kibana操作

2.1总体

GET _cat/health 查看集群的健康状况

        green:每个索引的primary shard和replica shard 都处于active的状态。

        yellow:每个索引的primary shard是active的状态,但是部分replica shard不是active的状态,处于不可用的状态。

        red:不是所有的primary shard 都是active的状态,这时候是危险的,至少我们不能保证写数据是安全的。

GET _cat/indices 命令查询ES中所有的index    GET _cat/indices?v

但是可能查询的不全,我们使用下面的命令

GET _all

PUT 类似于SQL中的增

DELETE 类似于SQL中的删

POST 类似于SQL中的改(elasticsearch 8.12.0版本中 POST功能和PUT相同)

GET 类似于SQL中的查

2.2 index操作

PUT aura_index 增加一个aura_index的index库

GET _cat/indices 命令查询ES中所有的index索引库 

DELETE aura_index 删除一个aura_index的index库

2.3 document操作

PUT 索引/_doc/{id} 增加id为{id}的数据(必须要有_doc)

{……}

GET 索引/_doc/{id} 查询id为{id}的数据

GET 索引/_search 查询索引内所有数据

GET 索引/_search

{
    "query":{
        "match": {
            "name": "chen"
        }
    },

    "sort": [
    {
      "ind": {
        "order": "desc"    //按ind降序排列
      }
    },
    {……}
   ],
    "from":0,    //从第几个开始
    "size":2    //每页的数据

    "_source": ["name"]    //    返回指定字段的数据内容
}

如果需要多个查询条件拼接在一起就需要使用bool

 bool 过滤可以用来合并多个过滤条件查询结果的布尔逻辑,它包含以下操作符:

    must :: 多个查询条件的完全匹配,相当于 and。

    must_not :: 多个查询条件的相反匹配,相当于 not。

    should :: 至少有一个查询条件匹配, 相当于 or。

    这些参数可以分别继承一个过滤条件或者一个过滤条件的数组

查询条件也会进行分词

高光

GET index3/_search
{
  "query": {    //要有query
    "match": {
      "name": "1"
    }
  }, 
  "highlight": {
    "fields": {
      "name":{}
    }
  }
}

GET index3/_search
{
  "query": {
    "match_all": {
      
    }
  }, 
  "aggs": {
    "group_by_ind": {    //long类型可以不进行上面的设置
      "terms": {
        "field": "ind"
      }
    }
  }
}
POST index3/_mapping
{
  "properties": {
      "name": {
        "type": "text",
        "fielddata": true
      }
    }
}

GET index3/_search
{
  "query": {
    "match_all": {
      
    }
  }, 
  "aggs": {
    "group_by_name": {
      "terms": {
        "field": "name"
      }
    }
  }
}

案例:查询出producer里面包含producer的数据,按照指定的价格区间进行分组,在每个组内再按tag进行分组,分完组以后再求每个组的平均价格,并且按照降序进行排序

range过滤允许我们按照指定范围查找一批数据

POST 索引/_doc/{id} 修改id为{id}的数据,PUT也有修改作用

{……}

三、restful api 操作

大体上上kibana相同,

eg.    GET http://localhost:9200/index3/_doc/3

如果有参数,连带{}一起放入请求体中

四、在java中使用elasticsearch

4.1配置

在pom.xml的<dependencies>中添加下面的依赖

        <dependency>
            <groupId>co.elastic.clients</groupId>
            <artifactId>elasticsearch-java</artifactId>
            <version>8.12.0</version>
        </dependency>

4.2 创建客户端

    private static ElasticsearchClient getElasticsearchClient() {
        // 创建低级客户端
        restClient = RestClient.builder(
                new HttpHost("localhost", 9200)).build();

// 使用Jackson映射器创建传输层
        transport = new RestClientTransport(
                restClient, new JacksonJsonpMapper());

// 创建API客户端
        return new ElasticsearchClient(transport);
//        System.out.println(client);
    }

4.3关闭客户端

private static void closeElasticsearchClient() throws IOException{
        restClient.close();
        transport.close();
    }

4.4索引相关

    private static boolean createIndex(String index) throws IOException{
        // 创建索引
        CreateIndexResponse createIndexResponse = client.indices().create(c -> c.index(index));
        // 打印结果
        return createIndexResponse.acknowledged();
    }

    private static String getIndex(String index) throws IOException{
        GetIndexResponse getIndexResponse = client.indices().get(e->e.index(index));
        System.out.println(getIndexResponse);
        return String.join(",", getIndexResponse.result().keySet());
    }

    private static boolean deleteIndex(String index) throws IOException{
        DeleteIndexResponse deleteIndexResponse = client.indices().delete(e->e.index(index));
        return deleteIndexResponse.acknowledged();
    }

4.4 Doc相关

    private static Result createDoc(String index, user user1, String id)throws IOException{
        // 构建一个创建Doc的请求
        CreateResponse createResponse = client.create(e->e.index(index).id(id).document(user1));
        // 打印请求结果
        return createResponse.result();
    }

    private static Result updateDoc() throws IOException{   //  test
        // 构建需要修改的内容,这里使用了Map
        Map<String, Object> map = new HashMap<>();
        map.put("ind", 35);

// 构建修改文档的请求
        UpdateResponse<user> response = client.update(e -> e.index("index3").id("4").doc(map), user.class);

// 打印请求结果
        System.out.println(response.result());
        return response.result();
    }

查找索引,感觉由于参数多,会有很多种语句的写法,这里只写出一种

        SearchRequest searchRequest = SearchRequest.of(s -> s
                .index("index3")
                .query(q -> q
                        .bool(b -> b
                                .must(m -> m.term(t -> t.field("name").value(FieldValue.of("1"))))
                        )
                ));
        SearchResponse<user> search = client.search(searchRequest, user.class);
//        System.out.println(search.hits().hits().stream().map(Hit::source).toList());
        List<user> list =  search.hits().hits().stream().map(Hit::source).toList();
        System.out.println(list.get(0).getName());

其中“index3”是索引名,“name”是要找匹配的内容,“1”是要找的值

文章有参考

  • 24
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值