ElasticSearchHighLevel etiketine sahip kayıtlar gösteriliyor. Tüm kayıtları göster
ElasticSearchHighLevel etiketine sahip kayıtlar gösteriliyor. Tüm kayıtları göster

23 Mayıs 2023 Salı

Eski ElasticSearchHighLevel IndicesClient Sınıfı

exists metodu
Örnek
Şöyle yaparız
GetIndexRequest request = new GetIndexRequest(props.getIndex().getName());
boolean exists = client.indices().exists(request, RequestOptions.DEFAULT);

if (!exists) {
  CreateIndexRequest indexRequest = new CreateIndexRequest(props.getIndex().getName());
  indexRequest.settings(Settings.builder()
       .put("index.number_of_shards", props.getIndex().getShard())
       .put("index.number_of_replicas", props.getIndex().getReplica())
  );

  CreateIndexResponse createIndexResponse = client.indices()
    .create(indexRequest, RequestOptions.DEFAULT);
  if (createIndexResponse.isAcknowledged()&& createIndexResponse.isShardsAcknowledged()) {
      log.info("{} index created successfully", props.getIndex().getName());
  } else {
    log.debug("Failed to create {} index", props.getIndex().getName());
  }
}

17 Mayıs 2023 Çarşamba

Eski ElasticSearchHighLevel RestHighLevelClient Sınıfı

Giriş
Şu satırı dahil ederiz
import org.elasticsearch.client.RestHighLevelClient;
Açıklaması şöyle
The Elasticsearch High Level REST Client, which was a popular choice for interacting with Elasticsearch clusters, has been deprecated since version 7.7.0. 

Maven
Şu satırı dahil ederiz
<dependency>
  <groupId>org.elasticsearch.client</groupId>
  <artifactId>elasticsearch-rest-high-level-client</artifactId>
  <version>7.14.0</version>
</dependency>
constructor -RestClientBuilder
Şöyle yaparız
@Bean(destroyMethod = "close")
public RestHighLevelClient getRestClient() {
  return new RestHighLevelClient(RestClient.builder(new HttpHost(hostName,
    httpPort, scheme)));
}
delete metodu
Örnek
Şöyle yaparız
DeleteRequest deleteRequest = new DeleteRequest(props.getIndex().getName(), id);
client.delete(deleteRequest, RequestOptions.DEFAULT);
index metodu
Örnek
Şöyle yaparız
Map<String, Object> jsonMap = new HashMap<>();
jsonMap.put("user", "kimchy");
jsonMap.put("postDate", new Date());
jsonMap.put("message", "trying out Elasticsearch");
IndexRequest indexRequest = new IndexRequest("myIndex")
                             .id("1").source(jsonMap);

IndexResponse indexResponse = client.index(request, RequestOptions.DEFAULT);
Örnek
Eğer JSON göndermek istersek şöyle yaparız
IndexRequest indexRequest = new IndexRequest("myIndex")
                             .id("1")
String jsonString = "{" +
   "\"user\":\"kimchy\"," +
   "\"postDate\":\"2013-01-30\"," +
   "\"message\":\"trying out Elasticsearch\"" +
   "}";
indexRequest.source(jsonString, XContentType.JSON);

IndexResponse indexResponse = client.index(request, RequestOptions.DEFAULT);
Örnek
Şöyle yaparız
public String indexRequest(Foo foo){
  try {
    IndexRequest indexRequest = new IndexRequest("myIndex")
      .id(foo.getId())
      .source(XContentType.JSON,"title", foo.getTitle(),
          "subject", foo.getSubject(),
          "content", foo.getContent());
      IndexResponse response = client.index(indexRequest, RequestOptions.DEFAULT);
      return response.getId();
  } catch (Exception ex) {
    ...
  }
  return null;
}
indices metodu
IndicesClient nesnesi döner

search metodu
Örnek
Şöyle yaparız
import org.elasticsearch.index.query.AbstractQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.builder.SearchSourceBuilder;

SearchSourceBuilder sourceBuilder = ...;

public List<Document> wildcardQuery(String query) {
  List<Foo> result = new ArrayList<>();
  try {
    AbstractQueryBuilder builder = QueryBuilders
      .queryStringQuery("*" + query.toLowerCase() + "*")
    result = getDocuments(builder);
  } catch (Exception ex) {...}
  return result;
}

List<Document> getDocuments(AbstractQueryBuilder builder) throws IOException {
  List<Foo> result = new ArrayList<>();

  sourceBuilder.query(builder);

  SearchRequest searchRequest = new SearchRequest("myIndex");
  searchRequest.source(sourceBuilder);

  SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
  SearchHits hits = searchResponse.getHits();
  SearchHit[] searchHits = hits.getHits();
  for (SearchHit hit : searchHits) {
    Foo foo = gson.fromJson(hit.getSourceAsString(), Foo.class);
    foo.setId(hit.getId());
    result.add(foo);
  }
  return result;
}

update metodu
Örnek
Şöyle yaparız
public String updateDocument(Foo foo){
  try {
    UpdateRequest request = new UpdateRequest("myIndex", foo.getId())
      .doc(gson.toJson(foo), XContentType.JSON);
    UpdateResponse response = client.update(request, RequestOptions.DEFAULT);
    return response.getId();
  } catch (Exception ex){
    ...
  }
  return null;
}