1515package org .jnosql .diana .elasticsearch .document ;
1616
1717
18- import org .elasticsearch .action .search .SearchResponse ;
19- import org .elasticsearch .client .Client ;
2018import org .elasticsearch .index .query .QueryBuilder ;
21- import org .jnosql .diana .api .document .Document ;
2219import org .jnosql .diana .api .document .DocumentCollectionManager ;
23- import org .jnosql .diana .api .document .DocumentDeleteQuery ;
2420import org .jnosql .diana .api .document .DocumentEntity ;
25- import org .jnosql .diana .api .document .DocumentQuery ;
2621
27- import javax .json .bind .Jsonb ;
28- import javax .json .bind .JsonbBuilder ;
29- import java .time .Duration ;
3022import java .util .List ;
31- import java .util .Map ;
32- import java .util .Objects ;
33- import java .util .concurrent .ExecutionException ;
34- import java .util .stream .Collectors ;
3523
36- import static java .nio .charset .StandardCharsets .UTF_8 ;
37- import static java .util .Objects .requireNonNull ;
38- import static java .util .stream .StreamSupport .stream ;
39- import static org .elasticsearch .common .unit .TimeValue .timeValueMillis ;
40- import static org .jnosql .diana .elasticsearch .document .EntityConverter .ID_FIELD ;
41- import static org .jnosql .diana .elasticsearch .document .EntityConverter .getMap ;
42-
43- public class ElasticsearchDocumentCollectionManager implements DocumentCollectionManager {
44-
45-
46- protected static final Jsonb JSONB = JsonbBuilder .create ();
47-
48- private final Client client ;
49-
50- private final String index ;
51-
52- ElasticsearchDocumentCollectionManager (Client client , String index ) {
53- this .client = client ;
54- this .index = index ;
55- }
56-
57- @ Override
58- public DocumentEntity insert (DocumentEntity entity ) throws NullPointerException {
59- requireNonNull (entity , "entity is required" );
60- Document id = entity .find (ID_FIELD )
61- .orElseThrow (() -> new ElasticsearchKeyFoundException (entity .toString ()));
62- Map <String , Object > jsonObject = getMap (entity );
63- byte [] bytes = JSONB .toJson (jsonObject ).getBytes (UTF_8 );
64- try {
65- client .prepareIndex (index , entity .getName (), id .get (String .class )).setSource (bytes )
66- .execute ().get ();
67- return entity ;
68- } catch (InterruptedException | ExecutionException e ) {
69- throw new ElasticsearchException ("An error to try to save/update entity on elasticsearch" , e );
70- }
71-
72- }
73-
74-
75- @ Override
76- public DocumentEntity insert (DocumentEntity entity , Duration ttl ) throws NullPointerException , UnsupportedOperationException {
77- requireNonNull (entity , "entity is required" );
78- requireNonNull (ttl , "ttl is required" );
79- Document id = entity .find (ID_FIELD )
80- .orElseThrow (() -> new ElasticsearchKeyFoundException (entity .toString ()));
81- Map <String , Object > jsonObject = getMap (entity );
82- byte [] bytes = JSONB .toJson (jsonObject ).getBytes (UTF_8 );
83- try {
84- client .prepareIndex (index , entity .getName (), id .get (String .class ))
85- .setSource (bytes )
86- .setTTL (timeValueMillis (ttl .toMillis ()))
87- .execute ().get ();
88- } catch (InterruptedException | ExecutionException e ) {
89- throw new ElasticsearchException ("An error to try to save with TTL entity on elasticsearch" , e );
90- }
91- return entity ;
92- }
93-
94- @ Override
95- public DocumentEntity update (DocumentEntity entity ) throws NullPointerException {
96- return insert (entity );
97- }
98-
99- @ Override
100- public void delete (DocumentDeleteQuery query ) throws NullPointerException {
101- requireNonNull (query , "query is required" );
102- List <DocumentEntity > entities = select (DocumentQuery .of (query .getCollection ())
103- .and (query .getCondition ().orElseThrow (() -> new IllegalArgumentException ("condition is required" ))));
104-
105- entities .stream ()
106- .map (entity -> entity .find (ID_FIELD ).get ().get (String .class ))
107- .forEach (id -> {
108- try {
109- client .prepareDelete (index , query .getCollection (), id ).execute ().get ();
110- } catch (InterruptedException | ExecutionException e ) {
111- throw new ElasticsearchException ("An error to delete entities on elasticsearch" , e );
112- }
113- });
114-
115- }
116-
117-
118- @ Override
119- public List <DocumentEntity > select (DocumentQuery query ) throws NullPointerException {
120- requireNonNull (query , "query is required" );
121- return EntityConverter .query (query , client , index );
122- }
24+ /**
25+ * The ES implementation of {@link DocumentCollectionManager}
26+ */
27+ public interface ElasticsearchDocumentCollectionManager extends DocumentCollectionManager {
12328
12429 /**
12530 * Find entities from {@link QueryBuilder}
@@ -129,31 +34,7 @@ public List<DocumentEntity> select(DocumentQuery query) throws NullPointerExcept
12934 * @return the objects from query
13035 * @throws NullPointerException when query is null
13136 */
132- public List <DocumentEntity > select (QueryBuilder query , String ... types ) throws NullPointerException {
133- Objects .requireNonNull (query , "query is required" );
134-
135- SearchResponse searchResponse = null ;
136- try {
137- searchResponse = client .prepareSearch (index )
138- .setTypes (types )
139- .setQuery (query )
140- .execute ().get ();
141-
142- return stream (searchResponse .getHits ().spliterator (), false )
143- .map (h -> new ElasticsearchEntry (h .getId (), h .getIndex (), h .sourceAsMap ()))
144- .filter (ElasticsearchEntry ::isNotEmpty )
145- .map (ElasticsearchEntry ::toEntity )
146- .collect (Collectors .toList ());
147- } catch (InterruptedException | ExecutionException e ) {
148- throw new ElasticsearchException ("An error when do find from QueryBuilder on elasticsearch" , e );
149- }
150-
151-
152- }
153-
154- @ Override
155- public void close () {
37+ List <DocumentEntity > select (QueryBuilder query , String ... types ) throws NullPointerException ;
15638
157- }
15839
15940}
0 commit comments