Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add exists query for fields #372

Merged
merged 1 commit into from
Jan 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ public interface ContentQuery<T> {
ContentQuery<T> whereNotIn(final String field, final Object... value);

ContentQuery<T> whereNotIn(final String field, final List<Object> value);

ContentQuery<T> whereExists(final String field);

public static interface Sort<T> {
public ContentQuery<T> asc();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,11 @@ public MemoryQuery<T> whereNotIn(final String field, final List<Object> value) {
return where(field, Queries.Operator.NOT_IN, value);
}

@Override
public MemoryQuery<T> whereExists(final String field) {
return new MemoryQuery<>(QueryUtil.exists(context, field));
}

private MemoryQuery<T> where(final String field, final Queries.Operator operator, final Object value) {
return new MemoryQuery<>(filtered(context, field, value, operator));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
* <http://www.gnu.org/licenses/gpl-3.0.html>.
* #L%
*/

import com.condation.cms.api.db.ContentNode;
import com.condation.cms.api.utils.MapUtil;
import com.condation.cms.filesystem.metadata.query.Queries;
Expand All @@ -40,8 +39,6 @@
@Slf4j
public final class QueryUtil {



public static Map<Object, List<ContentNode>> groupby(final Stream<ContentNode> nodes, final String field) {
return nodes.collect(Collectors.groupingBy((node) -> MapUtil.getValue(node.data(), field)));
}
Expand All @@ -66,6 +63,14 @@ protected static <T extends ContentNode> QueryContext<T> sorted(final QueryConte
return context;
}

public static QueryContext exists(final QueryContext context, final String field) {
context.setNodes(context.getNodes().filter((node) -> {
var node_value = MapUtil.getValue(((ContentNode)node).data(), field);
return node_value != null;
}));
return context;
}

public static QueryContext filtered(final QueryContext context, final String field, final Object value, final Queries.Operator operator) {
context.setNodes(context.getNodes().filter(createPredicate(field, value, operator)));
return context;
Expand All @@ -92,7 +97,4 @@ protected static QueryContext filter_extension(final QueryContext context, final
context.setNodes(context.getNodes().filter(Queries.createExtensionPredicate(field, value, predicate)));
return context;
}



}
Original file line number Diff line number Diff line change
Expand Up @@ -269,10 +269,16 @@ public ContentQuery<T> whereNotIn(String field, Object... value) {
public ContentQuery<T> whereNotIn(String field, List<Object> value) {
return where(field, Queries.Operator.NOT_IN, value);
}

@Override
public ContentQuery<T> whereExists(String field) {
QueryHelper.exists(queryBuilder, field);
return this;
}

private ContentQuery<T> where(final String field, final Queries.Operator operator, final Object value) {

QueryHelper.exists(queryBuilder, field, value);
QueryHelper.exists(queryBuilder, field);

switch (operator) {
case EQ ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
@Slf4j
public class QueryHelper {

public static void exists(BooleanQuery.Builder queryBuilder, String field, Object value) {
public static void exists(BooleanQuery.Builder queryBuilder, String field) {

if (true) {
queryBuilder.add(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,19 @@ public void test_query_with_start_uri() throws IOException {
Assertions.assertThat(nodes.getFirst().uri()).isEqualTo("test/test1.md");
}

@Test
public void test_field_exists() throws IOException {

var nodes = fileSystem.query("/test", (node, i) -> node).whereExists("keywords").get();

Assertions.assertThat(nodes).hasSize(1);
Assertions.assertThat(nodes.getFirst().uri()).isEqualTo("test/test1.md");

nodes = fileSystem.query((node, i) -> node).whereExists("keywords").get();

Assertions.assertThat(nodes).hasSize(2);
}

@Test
public void test_custom_operation() throws IOException {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -290,4 +290,11 @@ public void test_where_not_null() {
query.where("index", "=", null).get();
Assertions.assertThat(nodes).hasSize(2);
}

@Test
public void test_where_exists() {
MemoryQuery<ContentNode> query = createQuery();
var nodes = query.whereExists("index").get();
Assertions.assertThat(nodes).hasSize(2);
}
}
3 changes: 2 additions & 1 deletion cms-filesystem/src/test/resources/content/index.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
featured: true
publish_date: 2024-09-23
name: start
name: start
keywords: ["eins", "zwei"]
3 changes: 2 additions & 1 deletion cms-filesystem/src/test/resources/content/test/test1.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ taxonomy:
tags: [eins, zwei]
number1: 1
number2: 5
publish_date: 2024-10-01
publish_date: 2024-10-01
keywords: ["eins", "zwei"]
Loading