Skip to content

Commit

Permalink
#82 - Add a "indexFilterBeginsWith" string param to POST /search by meta
Browse files Browse the repository at this point in the history
  • Loading branch information
formkiqMike committed Mar 3, 2023
1 parent 001c2c4 commit 445b486
Show file tree
Hide file tree
Showing 7 changed files with 129 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,17 @@ public class SearchMetaCriteria {
/** Search Tag Value Equals. */
@Reflectable
private String eq;
/** Folder Key. */
/**
* Folder Key.
*
* @deprecated replaced with indexType = 'folder'
*/
@Deprecated
@Reflectable
private String folder;
/** Begins With Filter. */
@Reflectable
private String indexFilterBeginsWith;
/** Index Key. */
@Reflectable
private String indexType;
Expand Down Expand Up @@ -85,6 +93,26 @@ public SearchMetaCriteria folder(final String s) {
return this;
}

/**
* Get Index 'Begins With' filter.
*
* @return {@link String}
*/
public String indexFilterBeginsWith() {
return this.indexFilterBeginsWith;
}

/**
* Set Index 'Begins With' filter.
*
* @param beginsWith {@link String}
* @return {@link SearchMetaCriteria}
*/
public SearchMetaCriteria indexFilterBeginsWith(final String beginsWith) {
this.indexFilterBeginsWith = beginsWith;
return this;
}

/**
* Get Index Type.
*
Expand Down
5 changes: 5 additions & 0 deletions docs/openapi/openapi-iam.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2467,6 +2467,11 @@
indexType:
type: string
description: Searches in an index
enum:
- folder
indexFilterBeginsWith:
type: string
description: Returns index records that begins with a particular substring
DocumentSearchItemTag:
required:
- key
Expand Down
5 changes: 5 additions & 0 deletions docs/openapi/openapi-jwt.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2467,6 +2467,11 @@
indexType:
type: string
description: Searches in an index
enum:
- folder
indexFilterBeginsWith:
type: string
description: Returns index records that begins with a particular substring
DocumentSearchItemTag:
required:
- key
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -454,10 +454,15 @@ private PaginationResults<DynamicDocumentItem> searchByMeta(final String siteId,

if (value != null) {

String expression = PK + " = :pk";
Map<String, AttributeValue> values = new HashMap<String, AttributeValue>();
values.put(":pk", AttributeValue.builder().s(createDatabaseKey(siteId, value)).build());

String expression = PK + " = :pk";
if (meta.indexFilterBeginsWith() != null) {
expression = PK + " = :pk and begins_with(" + SK + ", :sk)";
values.put(":sk", AttributeValue.builder().s(meta.indexFilterBeginsWith()).build());
}

QueryRequest q =
createQueryRequest(null, expression, values, token, maxresults, Boolean.TRUE);

Expand Down
5 changes: 5 additions & 0 deletions lambda-api/src/main/resources/cloudformation/api-iam.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2469,6 +2469,11 @@ Resources:
indexType:
type: "string"
description: "Searches in an index"
enum:
- folder
indexFilterBeginsWith:
type: "string"
description: "Returns index records that begins with a particular substring"
DocumentSearchItemTag:
required:
- "key"
Expand Down
5 changes: 5 additions & 0 deletions lambda-api/src/main/resources/cloudformation/api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2469,6 +2469,11 @@ Resources:
indexType:
type: "string"
description: "Searches in an index"
enum:
- folder
indexFilterBeginsWith:
type: "string"
description: "Returns index records that begins with a particular substring"
DocumentSearchItemTag:
required:
- "key"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -600,7 +600,7 @@ public void testHandleSearchRequest12() throws Exception {
}

/**
* /search meta data.
* /search meta 'folder' data.
*
* @throws Exception an error has occurred
*/
Expand Down Expand Up @@ -654,13 +654,83 @@ public void testHandleSearchRequest13() throws Exception {
}

/**
* Text Fulltext search.
* /search meta 'folder' data & folders only.
*
* @throws Exception an error has occurred
*/
@SuppressWarnings("unchecked")
@Test
public void testHandleSearchRequest14() throws Exception {
for (String siteId : Arrays.asList(null, UUID.randomUUID().toString())) {
// given
Date now = new Date();
String username = "joe";

for (String path : Arrays.asList("a/b/test.txt", "a/c/test.txt", "a/test.txt")) {
String documentId = UUID.randomUUID().toString();
DocumentItemDynamoDb document = new DocumentItemDynamoDb(documentId, now, username);
document.setPath(path);
getDocumentService().saveDocument(siteId, document, null);
}

ApiGatewayRequestEvent event = toRequestEvent("/request-post-search01.json");
addParameter(event, "siteId", siteId);
event.setIsBase64Encoded(Boolean.FALSE);
QueryRequest q = new QueryRequest().query(new SearchQuery()
.meta(new SearchMetaCriteria().indexType("folder").eq("a").indexFilterBeginsWith("ff#")));
event.setBody(GsonUtil.getInstance().toJson(q));

// when
String response = handleRequest(event);

// then
Map<String, String> m = fromJson(response, Map.class);
assertEquals("200.0", String.valueOf(m.get("statusCode")));
DynamicObject resp = new DynamicObject(fromJson(m.get("body"), Map.class));

List<DynamicObject> documents = resp.getList("documents");
assertEquals(2, documents.size());
assertNotNull(documents.get(0).get("insertedDate"));
assertNotNull(documents.get(0).get("lastModifiedDate"));

assertEquals("b", documents.get(0).get("path"));
assertEquals("true", documents.get(0).get("folder").toString());
assertNotNull(documents.get(0).get("documentId"));

assertEquals("c", documents.get(1).get("path"));
assertEquals("true", documents.get(1).get("folder").toString());
assertNotNull(documents.get(1).get("documentId"));

// given
q = new QueryRequest().query(new SearchQuery().meta(
new SearchMetaCriteria().indexType("folder").eq("a/").indexFilterBeginsWith("fi#")));
event.setBody(GsonUtil.getInstance().toJson(q));

// when
response = handleRequest(event);

// then
m = fromJson(response, Map.class);
assertEquals("200.0", String.valueOf(m.get("statusCode")));
resp = new DynamicObject(fromJson(m.get("body"), Map.class));

documents = resp.getList("documents");
assertEquals(1, documents.size());

assertEquals("a/test.txt", documents.get(0).get("path"));
assertNull(documents.get(0).get("folder"));
assertNotNull(documents.get(0).get("documentId"));
}
}

/**
* Text Fulltext search.
*
* @throws Exception an error has occurred
*/
@SuppressWarnings("unchecked")
@Test
public void testHandleSearchRequest15() throws Exception {
for (String siteId : Arrays.asList(null, UUID.randomUUID().toString())) {
// given
final String documentId = UUID.randomUUID().toString();
Expand Down Expand Up @@ -705,7 +775,7 @@ public void testHandleSearchRequest14() throws Exception {
*/
@SuppressWarnings("unchecked")
@Test
public void testHandleSearchRequest15() throws Exception {
public void testHandleSearchRequest16() throws Exception {
for (String siteId : Arrays.asList(null, UUID.randomUUID().toString())) {
// given
final String text = UUID.randomUUID().toString();
Expand Down Expand Up @@ -741,7 +811,7 @@ public void testHandleSearchRequest15() throws Exception {
*/
@SuppressWarnings("unchecked")
@Test
public void testHandleSearchRequest16() throws Exception {
public void testHandleSearchRequest17() throws Exception {
for (String siteId : Arrays.asList(null, UUID.randomUUID().toString())) {
// given
Date now = new Date();
Expand Down

0 comments on commit 445b486

Please sign in to comment.