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; }
Hiç yorum yok:
Yorum Gönder