• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

《EalsticSearch从入门到》-CRUD+JAVA常用操作

武飞扬头像
赵侠客
帮助3

目录

《EalsticSearch从入门到实战》

  1. windows环境安装elasticsearch kibana并完成JAVA客户端查询
  2. 《EalsticSearch从入门到实战》-常用基本操作

前言

索引管理

创建索引

我们使用PUT /indexName来创建索引,如果没有索引EalsticSearch会给我们默认创建索引,不过创建的索引可能不满足我们的需求,所以最好禁用自动创建索引。 在elasticsearch.yml中增加配置项可禁用自动创建索引 action.auto_create_index:false

PUT /article
{
  "settings": {
    "number_of_shards": 6,
    "number_of_replicas": 1,
    "refresh_interval": "1s",
    "max_result_window":"20000"
  },
  "mappings": {
    "properties": {
      "id": {
        "type": "long"
      },
      "title": {
        "type": "text",
        "analyzer": "standard"
      },
      "tags": {
        "type": "keyword"
      },
      "read_count": {
        "type": "long"
      },
     "like_count": {
        "type": "long"
      },
     "comment_count": {
        "type": "long"
      },
      "rank": {
        "type": "double"
      },
      "location": {
          "type": "geo_point"
        },
      "pub_time": {
        "type": "date",
        "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd HH:mm||yyyy-MM-dd||epoch_millis"
      }
    }
  }
}

我们将article配置参数保存成article.json放入项目的resources目录下使用JAVA操作创建索引:

    @Test
    public void testCreateIndex() throws IOException {
        URL url = this.getClass().getClassLoader().getResource("article.json");
        var indexSetting= Files.readString(Path.of(url.getPath().substring(1)), Charset.defaultCharset());
        JSONObject json=JSON.parseObject(indexSetting);
        JSONObject settings=json.getJSONObject("settings");
        JSONObject indexMapping=json.getJSONObject("mappings");
        CreateIndexRequest request = new CreateIndexRequest("article").mapping(indexMapping.toString(), XContentType.JSON).settings(settings);
        CreateIndexResponse response = esClient.indices().create(request, RequestOptions.DEFAULT);
    }

创建完成索引后我们可以如下命令查看索引的配置情况:

GET /article/_mapping
GET /article/_settings

学新通

在这里我们需要关注四个配置项number_of_shardsnumber_of_replicasrefresh_intervalmax_result_window,更多的配置可以参考官方文档(settings配置)

Seetings分为静态动态,静态配置项设置后就不可修改了,像number_of_shardsnumber_of_replicas为静态配置项,动态配置项创建索引后可以随时动态修改的如refresh_intervalmax_result_window

  • number_of_shards 分片数量 分片数量在集群环境时要等于数据节点的倍数,这样索引分片可以均匀分配到不同的节点中,机器负载比较平衡,比如你的集群中数据节点有3个,那么number_of_shards你可以设置3、6、9、...3N。

  • number_of_replicas 副本数 number_of_replicas就是分片的备份数量,如果对数据安全性要求不高、对写入速度要求较高、也不想花太高的存储成本,可以设置成0,一般建议设置成1。

  • refresh_interval 刷新间隔 默认为10S,数据写入后刷新间隔,对数据写入后查询实时性要求不高可以使用默认,也不能设置太小了,这样刷新太频繁会影响数据写入速度,这里设置成1S。

  • max_result_window 最大返回条数,默认1万条 这个参数是elasticSearch在查询数据时最大能查到到的数据条数,默认为1万条,如果自定义大于1万条后,在查询时需要增加"track_total_hits": truejava中通过searchSourceBuilder.trackTotalHits(true)设置,否则最多还是只能查到1万条数据。

学新通 动态参考我们也可以通过KibanaIndex Management选择对应的索引来设置。

删除索引

删除索引比较简单

DELETE /article

JAVA代码

    @Test
    public void deleteIndex() throws IOException {
        DeleteIndexRequest request = new DeleteIndexRequest("article");
        esClient.indices().delete(request, RequestOptions.DEFAULT);
    }

这里需要非常注意,如果在生产环境,这样一个小小的命令瞬间就删除了索引中的所有数据,是非常危险的操作。

学新通 为了防止这种删库的危险操作,我们可以在Kibana中创建角色配置操作权限,然后分配给用户对应的角色,禁用这种删库权限。

更新索引

EalsticSearch索引Mapping是不支持修改的,如果增加字段,就需要重建索引,具体的重建索引方法可以参考我的另一篇文章《EalsticSearch添加字段后重建索引方法

数据管理

添加数据

单条添加:

POST /article/_doc/1
{
    "comment_count": 600,
    "id": 1,
    "like_count": 2000,
    "location": [
        118.55199,
        24.78144
    ],
    "pub_time": "2023-07-29 09:47",
    "rank": 0,
    "read_count": 10000,
    "tags": [
        "台风",
        "杜苏芮",
        "福建"
    ],
    "title": "台风“杜苏芮”登陆福建晋江 多部门多地全力应对"
}

批量添加:

POST _bulk
{"create": {"_index": "news", "_id": 1}}
{"comment_count":600,"id":1,"like_count":2000,"location":[118.55199,24.78144],"pub_time":"2023-07-29 09:47","rank":0.0,"read_count":10000,"tags":["台风","杜苏芮","福建"],"title":"台风“杜苏芮”登陆福建晋江 多部门多地全力应对"}
{"create": {"_index": "news", "_id": 2}}
{"comment_count":60,"id":2,"like_count":200,"location":[116.23128,40.22077],"pub_time":"2023-06-29 14:49:38","rank":0.0,"read_count":1000,"tags":["台风","杜苏芮","北京"],"title":"受台风“杜苏芮”影响 北京7月29日至8月1日将有强降雨"}
{"create": {"_index": "news", "_id": 3}}
{"comment_count":6,"id":3,"like_count":20,"location":[120.21201,30.208],"pub_time":"2020-07-29 14:49:38","rank":0.99,"read_count":100,"tags":["台风","杭州"],"title":"杭州解除台风蓝色预警信号"}

JAVA单条数据插入:

    public void testAdd() throws IOException {
        News news=new News();
        news.setId(1L);
        news.setTitle("台风“杜苏芮”登陆福建晋江 多部门多地全力应对");
        news.setTags(Arrays.asList("台风;杜苏芮;福建".split(";")));
        news.setRead_count(10000L);
        news.setLike_count(2000L);
        news.setComment_count(600L);
        news.setRank(0.0);
        news.setLocation(List.of(118.55199,24.78144));
        news.setPub_time("2023-07-29 09:47");

        IndexRequest indexRequest=new IndexRequest("articel");
        indexRequest.id(news.getId().toString());
        indexRequest.source(JSON.toJSONString(news), XContentType.JSON);
        var index = esClient.index(indexRequest, RequestOptions.DEFAULT);
        System.out.println(index.getResult());
}

JAVA批量数据插入:

    @Test
    public  void testBuckAdd() throws IOException {
        List<News> news=new ArrayList<>();
        BulkRequest bulkRequest = new BulkRequest();
        news.forEach(x-> bulkRequest.add(new IndexRequest("article").id(x.getId().toString()).source(JSON.toJSONString(x), XContentType.JSON)));
        BulkResponse bulk = esClient.bulk(bulkRequest, RequestOptions.DEFAULT);
        System.out.println(bulk.getItems());
    }

这里需要注意几个事项:

  1. EalsticSearch数据插入后就不支持修改了
  2. 不支持像Mysql那样修改某个字段
  3. 当数据插入量比较大的,使用批量插入会大大提升写入速度
  4. 如果要更新某些字段,可以使用《EalsticSearch添加字段后重建索引方法》介绍的_update_by_query,指定更新某些字段
POST /bucket_size_alias/_update_by_query
{
    "query": {
        "bool": {
            "must_not": {
                "exists": {
                    "field": "bucket_name"
                }
            }
        }
    },
    "script":{
    "inline" : "ctx._source.bucket_name= 'default_bucket_name'",
    "lang" : "painless"
  }
}

删除数据

删除接口

DELETE /article/_doc/1

注意事项: 大批量删除数据可以通过 POST /article/_delete_by_query来完成

java删除单条数据:

    @Test
    public   void deleteById() throws IOException {
        DeleteRequest deleteRequest=new DeleteRequest("article","1");
        deleteRequest.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE);
        esClient.delete(deleteRequest, RequestOptions.DEFAULT);
    }

java批量删除数据:

   @Test
    public   void deleteByIds() throws IOException {
        List<String> ids=new ArrayList<>();
        BulkRequest bulkRequest = new BulkRequest();
        ids.forEach(x-> bulkRequest.add(new DeleteRequest("article",x)));
        BulkResponse bulk = esClient.bulk(bulkRequest, RequestOptions.DEFAULT);
    }

更新数据

更新某一字段:

POST /article/_update_by_query
{
  "script":{
    "source": "ctx._source['title'] = \"测试只更新标题\""
  }, 
  "query": {
    "term": {
      "_id": {
        "value": "1"
      }
    }
  }
}

Java更新某一字段:


    @Test
    public   void updateDate() throws IOException {
        UpdateRequest updateRequest=new UpdateRequest("article","1");
        XContentBuilder builder = XContentFactory.jsonBuilder();
        builder.startObject();
        builder.field("title", "测试只更新标题");
        builder.endObject();
        updateRequest.doc(builder);
        updateRequest.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE);
        esClient.update(updateRequest, RequestOptions.DEFAULT);
    }

学新通

注意事项,如果我们通过PUT只传一个title,这样整个文档除了title其它字段都会被删除,EalsticSearch本身是不支持单个字段更新了,update_by_queryJAVA Client UpdateRequest只是帮你做了从EalsticSearch中查出来原来数据再和你的输入合并后,然后插入而已。

PUT /article/_doc/1
{
    "title": "测试只更新标题"
}

学新通

查询数据

通过ID查询

GET /article/_doc/1

JAVA查询操作

    @Test
    public void getById() throws IOException {
        GetRequest request = new GetRequest("article", "1");
        GetResponse getResponse = esClient.get(request, RequestOptions.DEFAULT);
        var sourceAsMap = getResponse.getSourceAsMap();
        sourceAsMap.forEach((k, v) -> System.out.println(k   ":"   v));
    }

JAVA批量查询:

    @Test
    public void getByIds() throws IOException {
        List<String> ids = List.of("1", "2", "3");
        MultiGetRequest request = new MultiGetRequest();
        for (String id : ids) {
            request.add("article", id);
        }
        List<Map<String, Object>> sourceAsMap = new ArrayList<>();
        MultiGetResponse getResponse = esClient.mget(request,RequestOptions.DEFAULT);
        MultiGetItemResponse[] responses = getResponse.getResponses();
        if (responses != null) {
            for (MultiGetItemResponse response : responses) {
                sourceAsMap.add(response.getResponse().getSourceAsMap());
            }
        }
        System.out.println(JSON.toJSONString(sourceAsMap));
    }

总结

本文主要介绍EalsticSearch RestFull ApiJAVA Client对数据增加修改删除批量增加批量删除以及对索引的增加删除重建等操作。同时还介绍的索引创建过程中和更新数据过程中的注意事项。EalsticSearch最重要的就是搜索,接下来将分篇介绍逐一介绍搜索功能的使用。

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhfgafcg
系列文章
更多 icon
同类精品
更多 icon
继续加载