在 Java 应用程序中使用 Elasticsearch

Elasticsearch 是一个分布式、免费且开源的搜索和分析引擎,适用于包括数值、文本、地理、结构化和非结构化数据在内的所有数据类型。Elasticsearch 非常轻量级。它的总依赖大小仅约为 300 KB。它只关注查询性能。这表明所有与数据库相关的进程都经过了充分的优化并具有可扩展性。该系统具有极高的容错性。当集群中的一个 Elasticsearch 节点发生故障时,主服务器会快速确定问题,并尽快将传入的请求发送给替换节点。

以下是在我们的 Java 应用程序中使用 Elasticsearch 的步骤。

前置准备:

首先启动一个 Elasticsearch 实例:

docker run -d --name elastic-test -p 9200:9200 -e "discovery.type=single-node" 
-e "xpack.security.enabled=false" docker.elastic.co/elasticsearch/elasticsearch:8.9.3

Elasticsearch 默认监听 9200 端口以接收传入的 HTTP 请求。通过打开我们喜欢的浏览器并访问 http://localhost:9200/,我们可以确认它已成功启动:

{
  "name": "238170191b08",
  "cluster_name": "elasticsearch",
  "cluster_uuid": "pvHXz7xsS5W4zlZiiATelw",
  "version": {
    "number": "6.1.4",
    "build_hash": "5b1fea5",
    "build_date": "2024-01-12T01:27:58.208Z",
    "build_snapshot": false,
    "lucene_version": "7.1.0",
    "minimum_wire_compatibility_version": "5.6.0",
    "minimum_index_compatibility_version": "5.1.0"
  },
  "tagline": "You Know, for Search"
}

步骤 1:添加 Maven 依赖

既然我们的主 Elasticsearch 集群已经运行起来,让我们直接进入 Java 客户端的部分。首先,我们需要在 pom.xml 文件中包含 Elasticsearch 和 Jackson 库:


    org.elasticsearch
    elasticsearch
    8.9.4


    com.fasterxml.jackson.core
    jackson-databind
    2.16.4

步骤 2:Elasticsearch Java 客户端

现在我们可以与 Elasticsearch 进行通信了。接下来,我们将研究如何执行最常见的任务,例如向索引添加文档、从索引中删除文档以及在索引中查找文档。

RestClient restClient = RestClient
  .builder(HttpHost.create("http://localhost:9200"))
  .build();
ElasticsearchTransport transport = new RestClientTransport(restClient, new JacksonJsonpMapper());
ElasticsearchClient client = new ElasticsearchClient(transport);

步骤 3:文档索引

将数据添加到 Elastic 是使它们可被搜索的第一步。为此,我们将使用 ElasticsearchClient 的 .index() 函数:

Person person = new Person(30, "dido datta", new Date(1771468076764L));
IndexResponse response = client.index(i -> i
  .index("person")
  .id(person.getFullName())
  .document(person));

步骤 4:查询已索引的文档

.search() 函数返回的 SearchResponse 中包含 Hits。我们可以从 SearchResponse 对象中获取 HitsMetadata,然后再次使用 .hits() 方法来获取匹配搜索请求的所有 Person 对象的列表。

String searchText = "Rik";
SearchResponse searchResponse = client.search(s -> s
  .index("person")
  .query(q -> q
    .match(t -> t
      .field("fullName")
      .query(searchText))), Person.class);

List<Hit> hits = searchResponse.hits().hits();
assertEquals(1, hits.size());
assertEquals("Rik Datta", hits.get(0).source().getFullName());

步骤 5:通过 ID 获取特定文档

给定一个文档的 id,我们的目标是先检索它,然后将其删除。例如,为了获取文档,我们使用 get() 方法:

String documentId = "Rik Datta";
GetResponse getResponse = client.get(s -> s
  .index("person")
  .id(documentId), Person.class);
Person source = getResponse.source();
assertEquals("Rik Datta", source.getFullName());

步骤 6:通过 ID 删除特定文档

使用 delete() 方法从索引中删除文档:

String documentId = "dido";
DeleteResponse response = client.delete(i -> i
  .index("person")
  .id(documentId));
assertEquals(Result.Deleted, response.result());
assertEquals("dido datta", response.id());

步骤 7:运行应用程序

现在,让我们通过执行上述所有操作来运行应用程序。

Java


public static void main(String[] args) throws IOException

{

makeConnection();

// Creating a New Person Instance

System.out.println("Inserting a new Person with name Shubham…");

Person person = new Person();

person.setName("Shubham");

person = insertPerson(person);

System.out.println("Person inserted –> " + person);

// Updating the Name o

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。如需转载,请注明文章出处豆丁博客和来源网址。https://shluqu.cn/39540.html
点赞
0.00 平均评分 (0% 分数) - 0