Skip to content

Commit 6994c9e

Browse files
authored
Merge pull request #156 from eclipse/sort_es
Enable Sort of Elastic Search
2 parents bb52770 + c85d54c commit 6994c9e

File tree

3 files changed

+72
-8
lines changed

3 files changed

+72
-8
lines changed

elasticsearch-driver/src/main/java/org/eclipse/jnosql/diana/elasticsearch/document/DefaultElasticsearchDocumentCollectionManager.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,6 @@ public Iterable<DocumentEntity> update(Iterable<DocumentEntity> entities) {
110110
public void delete(DocumentDeleteQuery query) throws NullPointerException {
111111
requireNonNull(query, "query is required");
112112

113-
query.getCondition().orElseThrow(() -> new IllegalArgumentException("condition is required"));
114113
DocumentQuery select = new ElasticsearchDocumentQuery(query);
115114

116115
List<DocumentEntity> entities = select(select).collect(Collectors.toList());

elasticsearch-driver/src/main/java/org/eclipse/jnosql/diana/elasticsearch/document/EntityConverter.java

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.elasticsearch.client.RequestOptions;
2828
import org.elasticsearch.client.RestHighLevelClient;
2929
import org.elasticsearch.search.builder.SearchSourceBuilder;
30+
import org.elasticsearch.search.sort.SortOrder;
3031

3132
import java.io.IOException;
3233
import java.util.HashMap;
@@ -35,6 +36,7 @@
3536
import java.util.stream.Stream;
3637
import java.util.stream.StreamSupport;
3738

39+
import static jakarta.nosql.SortType.ASC;
3840
import static java.util.Collections.singletonMap;
3941
import static java.util.stream.Collectors.toList;
4042

@@ -79,10 +81,8 @@ static Stream<DocumentEntity> query(DocumentQuery query, RestHighLevelClient cli
7981
private static Stream<DocumentEntity> executeStatement(DocumentQuery query, RestHighLevelClient client, String index,
8082
QueryConverterResult select) throws IOException {
8183
SearchRequest searchRequest = new SearchRequest(index);
82-
if (select.hasQuery()) {
83-
setQueryBuilder(query, select, searchRequest);
84-
}
8584

85+
setQueryBuilder(query, select, searchRequest);
8686
SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
8787
return Stream.of(response.getHits())
8888
.flatMap(h -> Stream.of(h.getHits()))
@@ -172,15 +172,31 @@ private static Stream<DocumentEntity> executeId(RestHighLevelClient client, Stri
172172

173173
private static void setQueryBuilder(DocumentQuery query, QueryConverterResult select, SearchRequest searchRequest) {
174174
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
175-
searchSourceBuilder.query(select.getStatement());
175+
176+
if (select.hasQuery()) {
177+
searchSourceBuilder.query(select.getStatement());
178+
}
179+
feedBuilder(query, searchSourceBuilder);
180+
176181
searchRequest.source(searchSourceBuilder);
182+
}
183+
184+
private static void feedBuilder(DocumentQuery query, SearchSourceBuilder searchSource) {
185+
query.getSorts().forEach(d -> {
186+
if (ASC.equals(d.getType())) {
187+
searchSource.sort(d.getName(), SortOrder.ASC);
188+
} else {
189+
searchSource.sort(d.getName(), SortOrder.DESC);
190+
}
191+
});
192+
177193
int from = (int) query.getSkip();
178194
int size = (int) query.getLimit();
179195
if (from > 0) {
180-
searchSourceBuilder.from(from);
196+
searchSource.from(from);
181197
}
182198
if (size > 0) {
183-
searchSourceBuilder.size(size);
199+
searchSource.size(size);
184200
}
185201
}
186202

elasticsearch-driver/src/test/java/org/eclipse/jnosql/diana/elasticsearch/document/ElasticsearchDocumentCollectionManagerTest.java

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,17 @@
2121
import jakarta.nosql.document.DocumentQuery;
2222
import org.eclipse.jnosql.diana.document.Documents;
2323
import org.elasticsearch.index.query.TermQueryBuilder;
24+
import org.junit.jupiter.api.Assertions;
2425
import org.junit.jupiter.api.BeforeEach;
2526
import org.junit.jupiter.api.Test;
27+
import org.testcontainers.shaded.org.apache.commons.lang.text.StrBuilder;
2628

2729
import java.time.Duration;
2830
import java.util.ArrayList;
2931
import java.util.Arrays;
3032
import java.util.List;
33+
import java.util.Optional;
34+
import java.util.concurrent.TimeUnit;
3135
import java.util.stream.Collectors;
3236

3337
import static jakarta.nosql.document.DocumentDeleteQuery.delete;
@@ -38,6 +42,7 @@
3842
import static org.hamcrest.MatcherAssert.assertThat;
3943
import static org.hamcrest.Matchers.contains;
4044
import static org.hamcrest.Matchers.containsInAnyOrder;
45+
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
4146
import static org.junit.jupiter.api.Assertions.assertEquals;
4247
import static org.junit.jupiter.api.Assertions.assertFalse;
4348
import static org.junit.jupiter.api.Assertions.assertNotNull;
@@ -54,6 +59,10 @@ public void setUp() {
5459
ElasticsearchDocumentCollectionManagerFactory managerFactory = ElasticsearchDocumentCollectionManagerFactorySupplier.INSTANCE.get();
5560
entityManager = managerFactory.get(DocumentEntityGerator.INDEX);
5661

62+
DocumentDeleteQuery deleteQuery = DocumentDeleteQuery.delete().from("person").build();
63+
64+
entityManager.delete(deleteQuery);
65+
5766
}
5867

5968
@Test
@@ -76,10 +85,11 @@ public void shouldInsertTTL() {
7685
}
7786

7887
@Test
79-
public void shouldReturnAll() {
88+
public void shouldReturnAll() throws InterruptedException {
8089
DocumentEntity entity = DocumentEntityGerator.getEntity();
8190
entityManager.insert(entity);
8291
DocumentQuery query = select().from(DocumentEntityGerator.COLLECTION_NAME).build();
92+
SECONDS.sleep(1L);
8393
List<DocumentEntity> result = entityManager.select(query).collect(Collectors.toList());
8494
assertFalse(result.isEmpty());
8595
}
@@ -161,6 +171,45 @@ public void shouldFindDocumentById() {
161171
assertThat(entities, contains(entity));
162172
}
163173

174+
@Test
175+
public void shouldFindOrderByName() throws InterruptedException {
176+
final DocumentEntity poliana = DocumentEntityGerator.getEntity();
177+
final DocumentEntity otavio = DocumentEntityGerator.getEntity();
178+
poliana.add("name", "poliana");
179+
otavio.add("name", "otavio");
180+
otavio.add("_id", "id2");
181+
entityManager.insert(Arrays.asList(poliana, otavio));
182+
SECONDS.sleep(1L);
183+
DocumentQuery query = DocumentQuery.select().from("person").orderBy("name").asc().build();
184+
String[] names = entityManager.select(query).map(d -> d.find("name"))
185+
.filter(Optional::isPresent)
186+
.map(Optional::get)
187+
.map(d -> d.get(String.class))
188+
.toArray(String[]::new);
189+
190+
assertArrayEquals(names, new String[]{"otavio", "poliana"});
191+
}
192+
193+
@Test
194+
public void shouldFindOrderByNameDesc() throws InterruptedException {
195+
final DocumentEntity poliana = DocumentEntityGerator.getEntity();
196+
final DocumentEntity otavio = DocumentEntityGerator.getEntity();
197+
poliana.add("name", "poliana");
198+
otavio.add("name", "otavio");
199+
otavio.add("_id", "id2");
200+
entityManager.insert(Arrays.asList(poliana, otavio));
201+
SECONDS.sleep(1L);
202+
DocumentQuery query = DocumentQuery.select().from("person").orderBy("name").desc().build();
203+
String[] names = entityManager.select(query).map(d -> d.find("name"))
204+
.filter(Optional::isPresent)
205+
.map(Optional::get)
206+
.map(d -> d.get(String.class))
207+
.toArray(String[]::new);
208+
209+
assertArrayEquals(names, new String[]{"poliana", "otavio"});
210+
}
211+
212+
164213
@Test
165214
public void shouldFindAll() throws InterruptedException {
166215
entityManager.insert(DocumentEntityGerator.getEntity());

0 commit comments

Comments
 (0)