[Java]-Elastic中index与document基本操作


Elastic Stack核心产品包括Elasticsearch、Kibana、Beats 和 Logstash,可用于日志、APM、安全等各种用例。

Elasticsearch

Elasticsearch是一个分布式、RESTful 风格的搜索和数据分析引擎;作为 Elastic Stack 的核心,它集中存储数据。

Node与Cluster

Elasticsearch本质上是一个分布式数据库,允许多台服务器协同工作,每台服务器可以运行多个 Elastic 实例。单个实例称为一个节点(node);一组节点构成一个集群(cluster)。

Index

Elasticsearch会索引所有字段,经过处理后写入一个反向索引(Inverted Index);查找数据的时候,直接查找该索引

Elasticsearch数据管理的顶层容器叫做 Index(索引)(类似数据库)。Index 的名字必须小写

Document

Index 里面单条的记录称为 Document(文档)。Document使用 JSON 格式表示:

  • _index:是索引名;
  • _type:文档类型(以后会弃用);
  • _id:文档唯一标识,操作文档时就需要使用此唯一标识;
  • _version:文档版本,每更新一次会加一;
{
  "_index": "test-07.02",
  "_type": "_doc",
  "_id": "Hoaaa3oBfSQ12-KDj4pW",
  "_version": 1,
  "_score": 0,
  "fields": {
    "number": [
      1175
    ],
    "name": [
      "Name 2usfD"
    ],
    "name.keyword": [
      "Name 2usfD"
    ],
    "age": [
      7
    ]
  }
}

RestHighLevelClient

ES提供了两个JAVA REST client版本:

  • Java Low Level REST Client: 低级别的REST客户端,通过http与集群交互,用户需自己编组请求JSON串,及解析响应JSON串。兼容所有ES版本。

  • Java High Level REST Client: 高级别的REST客户端,基于低级别的REST客户端,增加了编组请求JSON串、解析响应JSON串等相关api。使用的版本需要保持和ES服务端的版本一致,否则会有版本问题。

一般推荐使用High Level版本;不同版本下接口会有些不同,本文以7.13.2为例。

依赖包

为了使用ES客户端,需要在pom.xml中增加依赖(ES的Client版本要与ES版本统一,因此要在Properties中设定所使用的版本):

<properties>
    <java.version>1.8</java.version>
    <es.version>7.13.2</es.version>
</properties>

<dependencies>
    <!-- https://mvnrepository.com/artifact/org.elasticsearch/elasticsearch -->
    <dependency>
        <groupId>org.elasticsearch</groupId>
        <artifactId>elasticsearch</artifactId>
        <version>7.13.2</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.elasticsearch.client/elasticsearch-rest-high-level-client -->
    <dependency>
        <groupId>org.elasticsearch.client</groupId>
        <artifactId>elasticsearch-rest-high-level-client</artifactId>
        <version>7.13.2</version>
        <exclusions>
            <exclusion>
                <groupId>org.elasticsearch.client</groupId>
                <artifactId>elasticsearch-rest-client</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.elasticsearch.client/elasticsearch-rest-client -->
    <dependency>
        <groupId>org.elasticsearch.client</groupId>
        <artifactId>elasticsearch-rest-client</artifactId>
        <version>7.13.2</version>
    </dependency>
</dependencies>

client对象

在对ES进行操作前,需要先创建与elasticsearch服务进行连接的RestHighLevelClient对象:

RestHighLevelClient getClient() {
    RestHighLevelClient client = new RestHighLevelClient(
            RestClient.builder(
                    new HttpHost("127.0.0.1", 9200, "http")
            )
    );

在client对象使用完后,需通过close()及时关闭

Index

Index(索引)是文档(Document)的容器,是一类文档的集合。其相关操作,主要封装client..indices()中。

创建索引

通过create来创建索引;通过返回值中的isAcknowledged判断是否成功:

public String createIndex(String index) {
    try (RestHighLevelClient rhlClient = ESClient.getClient()) {
        CreateIndexRequest createReq = new CreateIndexRequest(index);
        CreateIndexResponse resp = rhlClient.indices().create(createReq, RequestOptions.DEFAULT);
        String result = resp.isAcknowledged() ? "OK" : "Fail";
        System.out.println(index + " create: " + result);
    } catch (Exception ex) {
        System.out.println(index + " create fail: " + ex);
    }
    return "fail";
}

判断索引是否存在

通过exists来判断索引是否存在:

public boolean isIndexExists(String index) {
    try (RestHighLevelClient rhlClient = ESClient.getClient()) {
        GetIndexRequest getReq = new GetIndexRequest(index);
        boolean bExists = rhlClient.indices().exists(getReq, RequestOptions.DEFAULT);
        System.out.println(index + " exists: " + bExists);
    } catch (Exception ex) {
        System.out.println(index + " check fail: " + ex);
    }
    return false;
}

删除索引

通过delete来删除索引;通过返回值中的isAcknowledged判断是否成功:

public String deleteIndex(String index) {
    try (RestHighLevelClient rhlClient = ESClient.getClient()) {
        DeleteIndexRequest delReq = new DeleteIndexRequest(index);
        AcknowledgedResponse resp = rhlClient.indices().delete(delReq, RequestOptions.DEFAULT);
        String result = resp.isAcknowledged() ? "OK" : "Fail";
        System.out.println(index + " delete: " + result);
    } catch (Exception ex) {
        System.out.println(index + " delete fail: " + ex);
    }
    return "fail";
}

获取索引

通过别名Alias来获取索引(通过*来匹配);要匹配索引需要通过GetAliasesRequest的**indices(pattern)**来设定。返回是一个map映射:

public Set<String> listIndex(String prefix) {
    try (RestHighLevelClient rhlClient = ESClient.getClient()) {
        GetAliasesRequest getReq = new GetAliasesRequest();
        getReq.indices(prefix + "*");
        GetAliasesResponse resp = rhlClient.indices().getAlias(getReq, RequestOptions.DEFAULT);
        Map<String, Set<AliasMetadata>> mapAlias = resp.getAliases();
        Set<String> result = mapAlias.keySet();
        System.out.println("list index of " + prefix + ": " + result);
    } catch (Exception ex) {
        System.out.println(prefix + " list fail: " + ex);
    }
    return new HashSet<>();
}

Document

Index 里面单条的记录称为Document(文档)。

添加记录

通过index来添加索引;内容可通过Map<String, Object>设定,返回值为添加记录的基本信息(id等):

public String addDocument(String index, Map<String, Object> data) {
    try (RestHighLevelClient rhlClient = ESClient.getClient()) {
        IndexRequest request = new IndexRequest(index);
        request.source(data);
        IndexResponse resp = rhlClient.index(request, RequestOptions.DEFAULT);
        String result = resp.toString();
        System.out.println(result);
        return result;
    } catch (Exception ex) {
        System.out.println(index + " add doc fail: " + ex);
        return ex.getMessage();
    }
}

判断记录是否存在

通过exists判断记录是否存在:

public boolean isDocExists(String index, String id) {
    try (RestHighLevelClient rhlClient = ESClient.getClient()) {
        GetRequest request = new GetRequest(index);
        request.id(id);
        boolean bExists = rhlClient.exists(request, RequestOptions.DEFAULT);
        System.out.println(index + " exists: " + bExists);
        return bExists;
    } catch (Exception ex) {
        System.out.println(index + " add doc fail: " + ex);
    }
    return false;
}

删除记录

通过delete来删除记录(根据记录id):

public String delDocument(String index, String id) {
    try (RestHighLevelClient rhlClient = ESClient.getClient()) {
        DeleteRequest request = new DeleteRequest(index);
        request.id(id);
        DeleteResponse resp = rhlClient.delete(request, RequestOptions.DEFAULT);
        System.out.println(index + " exists: " + resp.toString());
        return resp.toString();
    } catch (Exception ex) {
        System.out.println(index + " add doc fail: " + ex);
        return ex.toString();
    }
}

更新记录

通过update来更新记录;记录通过id标识,新的内容通过Map<String, Object>设定:

public String updateDocument(String index, String id, Map<String, Object> data) {
    try (RestHighLevelClient rhlClient = ESClient.getClient()) {
        UpdateRequest request = new UpdateRequest(index, id);
        request.doc(data);
        UpdateResponse resp = rhlClient.update(request, RequestOptions.DEFAULT);
        String result = resp.toString();
        System.out.println(result);
        return result;
    } catch (Exception ex) {
        System.out.println(ex);
        return ex.getMessage();
    }
}

获取记录

通过get可获取记录的详细信息:

public String getDocument(String index, String id){
    try (RestHighLevelClient rhlClient = ESClient.getClient()) {
        GetRequest request = new GetRequest(index);
        request.id(id);
        GetResponse resp = rhlClient.get(request, RequestOptions.DEFAULT);
        System.out.println(resp.getIndex() + " # " + resp.getVersion() + " # " + resp.getId());
        System.out.println(resp.getSourceAsString());
        return resp.getSourceAsString();
    } catch (Exception ex) {
        System.out.println(index + " add doc fail: " + ex);
    }
    return "Fail";
}

获取记录条数

通过count可获取记录的条数:

public long getDocCount(String index) {
    try (RestHighLevelClient rhlClient = ESClient.getClient()) {
        CountRequest request = new CountRequest(index);
        CountResponse resp = rhlClient.count(request, RequestOptions.DEFAULT);
        System.out.println(index + " document count: " + resp.getCount());
        return resp.getCount();
    } catch (Exception ex) {
        System.out.println(index + " document count fail: " + ex);
        return 0L;
    }
}

获取记录Id

通过search来获取所需记录(通过fetchSource(new String[]{""}, null)来设定只获取信息,不获取内容):

public List<String> listDocId(String index, int count) {
    try (RestHighLevelClient rhlClient = ESClient.getClient()) {
        SearchRequest request = new SearchRequest(index);
        SearchSourceBuilder sourceBuilder = SearchSourceBuilder.searchSource();
        sourceBuilder.from(0);
        sourceBuilder.size(count);
        sourceBuilder.fetchSource(new String[]{""}, null);
        request.source(sourceBuilder);

        SearchResponse response = rhlClient.search(request, RequestOptions.DEFAULT);
        SearchHits gotHits = response.getHits();

        List<String> lstId = new ArrayList<>(gotHits.getHits().length);
        for (SearchHit doc : gotHits) {
            lstId.add(doc.getId());
        }
        System.out.println(index + " documents: " + lstId);
        return lstId;
    } catch (Exception ex) {
        System.out.println(index + " update doc fail: " + ex);
        return new ArrayList<>(0);
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Elasticsearch 的 Java 客户端,有一个名为 `org.elasticsearch.client.document.DocumentClient` 的类,用于执行与文档相关的操作。这个类提供了一系列方法,可以用来索引、获取、更新和删除文档。下面是一个简单的示例: ```java import org.elasticsearch.action.get.GetRequest; import org.elasticsearch.action.get.GetResponse; import org.elasticsearch.action.index.IndexRequest; import org.elasticsearch.action.index.IndexResponse; import org.elasticsearch.action.update.UpdateRequest; import org.elasticsearch.action.update.UpdateResponse; import org.elasticsearch.action.delete.DeleteRequest; import org.elasticsearch.action.delete.DeleteResponse; import org.elasticsearch.client.RequestOptions; import org.elasticsearch.client.RestHighLevelClient; import org.elasticsearch.common.xcontent.XContentType; public class DocumentExample { private RestHighLevelClient client; public DocumentExample(RestHighLevelClient client) { this.client = client; } public void indexDocument(String index, String id, String json) throws IOException { IndexRequest request = new IndexRequest(index) .id(id) .source(json, XContentType.JSON); IndexResponse response = client.index(request, RequestOptions.DEFAULT); System.out.println(response); } public void getDocument(String index, String id) throws IOException { GetRequest request = new GetRequest(index, id); GetResponse response = client.get(request, RequestOptions.DEFAULT); System.out.println(response); } public void updateDocument(String index, String id, String json) throws IOException { UpdateRequest request = new UpdateRequest(index, id) .doc(json, XContentType.JSON); UpdateResponse response = client.update(request, RequestOptions.DEFAULT); System.out.println(response); } public void deleteDocument(String index, String id) throws IOException { DeleteRequest request = new DeleteRequest(index, id); DeleteResponse response = client.delete(request, RequestOptions.DEFAULT); System.out.println(response); } } ``` 以上示例展示了如何使用 `DocumentClient` 类来执行索引、获取、更新和删除文档的操作。你可以根据自己的需求调用这些方法,并根据需要处理相应的响应。请注意,需要先创建一个 `RestHighLevelClient` 实例,并将其传递给 `DocumentExample` 的构造函数,以便与 Elasticsearch 集群进行交互。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值