15 Mayıs 2023 Pazartesi

ElasticSearch Java API Client ElasticsearchClient Sınıfı

Giriş
Şu satırı dahil ederiz
import co.elastic.clients.elasticsearch.ElasticsearchClient;
Sanırım RestHighLevelClient yerine bu Java client kullanılmalı

Maven
Şu satırı dahil ederiz
<dependency>
  <groupId>co.elastic.clients</groupId>
  <artifactId>elasticsearch-java</artifactId>
  <version>8.5.3</version>
</dependency>
<dependency>
  <groupId>com.fasterxml.jackson.core</groupId>
  <artifactId>jackson-databind</artifactId>
  <version>2.12.7</version>
</dependency>
Gradle
Şu satırı dahil ederiz
dependencies {
  implementation 'co.elastic.clients:elasticsearch-java:8.5.3'
  implementation 'com.fasterxml.jackson.core:jackson-databind:2.12.7'
}
constructor
Açıklaması şöyle
The client class is co.elastic.clients.elasticsearch.ElasticsearchClient which is initialized by providing the transport object (co.elastic.clients.transport.ElasticsearchTransport) to its constructor. This transport object in turn needs the restclient and JSON mapper objects.
Örnek
Şöyle yaparız
# Step 1: create the RestClient
RestClient restClient = RestClient.builder(new HttpHost("localhost", 9200)).build();

# Step 2: Construct the ElasticsearchTransport object

# Create a new JacksonJsonpMapper object
JacksonJsonpMapper jsonMapper = new JacksonJsonpMapper();

# Create the transport object
ElasticsearchTransport elasticsearchTransport =
   new RestClientTransport(restClient, jsonMapper);

# Step 3: Construct the ElasticsearchTransport object
ElasticsearchClient elasticsearchClient = new ElasticsearchClient(elasticsearchTransport);
Bulk Requests
Açıklaması şöyle
The Bulk API makes it possible to perform many index/delete operations in a single API call. This can greatly increase the
indexing speed. Each subrequest is executed independently, so the failure of one subrequest won’t affect the success of the others. If
any of the requests fail, the top-level error flag is set to true and the error details will be reported under the relevant request.

indices metodu

index metodu
Yaratılmış bir index'e doküman gönderir

Örnek
Şununla aynıdır
POST flights/_doc
{
  "route":"London to New York",
  "name":"BA123",
  "airline":"British Airways",
  "duration_hours":5
}
Şöyle yaparız
public void indexDocument(String indexName, Flight flight) throws IOException {
  IndexResponse indexResponse = this.elasticsearchClient.index(
    i -> i.index(indexName)
          .document(flight)
   );
   System.out.println("Document indexed" + indexResponse);
} 
Açıklaması şöyle
Executing this query will index a flight into our flights index. The elasticsearchClient exposes an index method that can be chained up with other methods such as id and document. In the above case, we didn’t use the ID as we’ve let the system generate it for us.

The document method expects the flight object; did you notice we didn’t do any transformation of the flight Java object to JSON? That’s because, we’ve delegated the responsibility of marshaling and unmarshaling to the JacsonJasonpMapper class that we associated with the transport object earlier on.
search metodu
Şöyle yaparız
SearchResponse searchResponse = 
elasticsearchClient.search(
 searchRequest -> searchRequest
   .index(indexName)
   .query(
     queryBuilder ->
       queryBuilder.match(
	     matchQBuilder->
		   matchQBuilder.field("route").query(searchText))
	), //query
	Flight.class
);
Daha sonra şöyle yaparız
// Capture flights
List<Flight> flights =  (List<Flight>) searchResponse.hits().hits()
  .stream()
  .collect(Collectors.toList());

// Or print them to the console
searchResponse.hits()
  .hits()
  .stream()
  .forEach(System.out::println);
Açıklaması şöyle
Searching data using queries with Java API client follows the similar path — invoking search method in the ElasticsearchClient class by passing the required query. There’s one subtle difference though — unlike the other features exposing a client per namespace, the search feature does not. That is, there’s no separate “client” per se, unlike ElasticsearchIndicesClient.

Let’s just say we wish to search for a route from London to New York: We create a match query providing the “London New York” as the search criteria against the route field in a nicely formed DSL when working with Kibana.

The search method is expecting a search request, it is provided as a lambda expression to the method as you see above. The query will be written using another lambda function — given a query request (Query.Builder object), invoke the match function with the MatchQuery.Builder object. The JSON will be converted to a Flight Java object, which is why Flight.class is provided as the argument to the query method.

The response from the search method is a SearchResponse — so we can capture the results ...

The searchResponse consists of results as hits — which can be iterated through to get to the list of Flights that were returned. 

As you can infer, the searchResponse object has the results in the hits array — all we need to make sure is to cast the hits to appropriate domain objects (Flight in this case). 






Hiç yorum yok:

Yorum Gönder