We will now follow Gavin Cornwell https://community.alfresco.com/community/ecm/blog/2017/04/11/v1-rest-api-part-9-queries-search examples and see how we can achieve the same experience using the SDK
To make the exercise more concise we will execute each request in a synchronous way.
Alfresco Java Client is currently in Early Access mode. It evolves as you use them, as you give feedback, and as the developers update and add file. We like to think app & lib development as services that grow and evolve with the involvement of the community.
In order to follow along you'll need an environment to do so, firstly download and install the 5.2.c Early Access Community Release. In our case we will consider Alfresco is available at http://localhost:8080/alfresco and the "admin" user is available and has "admin" as password.
//Create a public Site
SitesAPI sitesAPI = client.getSitesAPI();
//Create public site
SiteBodyCreate siteBodyCreate = new SiteBodyCreate("queriesSearchSite", "Queries and Search Site", "Site created for queries and search blog post", SiteVisibilityEnum.PUBLIC);
Response<SiteRepresentation> siteRepresentationResponse = sitesAPI.createSiteCall(siteBodyCreate).execute();
SiteRepresentation siteRepresentation = siteRepresentationResponse.body();
Assert.assertEquals(siteRepresentation.getId(), "queriesSearchSite");
Assert.assertEquals(siteRepresentation.getVisibilityEnum(), SiteVisibilityEnum.PUBLIC);
Response<SiteContainerRepresentation> doclibContainerResponse = sitesAPI.getSiteContainerCall("queriesSearchSite", "documentLibrary").execute();
SiteContainerRepresentation doclibContainer = doclibContainerResponse.body();
Assert.assertEquals(doclibContainer.getFolderId(), "documentLibrary");
File file = new File("W:\\test.txt");
RequestBody requestBody = RequestBody.create(MediaType.parse("text/plain"), file);
MultipartBody.Builder multipartBuilder = new MultipartBody.Builder();
multipartBuilder.addFormDataPart("filedata", "test.txt", requestBody);
RequestBody fileRequestBody = multipartBuilder.build();
client.getNodesAPI().createNodeCall(doclibContainer.getId(), fileRequestBody).execute();
file = new File("W:\\image.png");
requestBody = RequestBody.create(MediaType.parse("image/png"), file);
multipartBuilder = new MultipartBody.Builder();
multipartBuilder.addFormDataPart("filedata", "image.png", requestBody);
fileRequestBody = multipartBuilder.build();
client.getNodesAPI().createNodeCall(doclibContainer.getId(), fileRequestBody).execute();
ResultPaging<NodeRepresentation> loremResult = client.getQueriesAPI().findNodesCall("lorem").execute().body();
Assert.assertTrue(loremResult.getCount() >= 7);
ResultPaging<SiteRepresentation> siteResult = client.getQueriesAPI().findSitesCall("queries").execute().body();
Assert.assertEquals(siteResult.getCount(), 1);
Assert.assertEquals(siteResult.getList().get(0).getId(), "queriesSearchSite");
ResultPaging<PersonRepresentation> personResult = client.getQueriesAPI().findPeopleCall("jackson").execute().body();
Assert.assertEquals(personResult.getCount(), 1);
Assert.assertEquals(personResult.getList().get(0).getId(), "mjackson");
SearchAPI searchAPI = client.getSearchAPI();
QueryBody body = new QueryBody().query(new RequestQuery().query("lorem"));
ResultSetRepresentation<ResultNodeRepresentation> loremSearchResult = searchAPI.searchCall(body).execute().body();
Assert.assertTrue(loremSearchResult.getCount() >= 7);
RequestQuery cmisQuery = new RequestQuery().query("select * from cmis:document WHERE cmis:name LIKE 'test.%'")
.language(RequestQuery.LanguageEnum.CMIS);
QueryBody cmisbody = new QueryBody().query(cmisQuery);
ResultSetRepresentation<ResultNodeRepresentation> cmisResult = searchAPI.searchCall(cmisbody).execute().body();
Assert.assertTrue(cmisResult.getCount() >= 1);
RequestQuery luceneQuery = new RequestQuery().query("+@cm\\:modified:[NOW/DAY-7DAYS TO NOW/DAY+1DAY] +TYPE:\"cm:content\"")
.language(RequestQuery.LanguageEnum.LUCENE);
QueryBody lucenebody = new QueryBody().query(luceneQuery);
ResultSetRepresentation<ResultNodeRepresentation> luceneResult = searchAPI.searchCall(lucenebody).execute().body();
Assert.assertTrue(luceneResult.getCount() == 100);
RequestQuery pagingQuery = new RequestQuery().query("+TYPE:\"cm:content\"").language(RequestQuery.LanguageEnum.AFTS);
List<RequestSortDefinition> sortDefinitions = Arrays.asList(
new RequestSortDefinition().type(RequestSortDefinition.TypeEnum.FIELD).field("cm:name").ascending(false));
RequestPagination pagination = new RequestPagination().skipCount(10).maxItems(25);
QueryBody pagingQueryBody = new QueryBody().query(pagingQuery).sort(sortDefinitions).paging(pagination);
ResultSetRepresentation<ResultNodeRepresentation> pagingResult = searchAPI.searchCall(pagingQueryBody).execute().body();
Assert.assertTrue(pagingResult.getCount() == 25);
Assert.assertEquals(pagingResult.getPagination().getMaxItems(), 25);
Assert.assertEquals(pagingResult.getPagination().getSkipCount(), 10);
RequestQuery facetQuery = new RequestQuery().query("(name:\"test*\" OR title:\"test*\") AND TYPE:\"cm:content\"");
List<RequestFacetQuery> facetsQuery = Arrays.asList(
new RequestFacetQuery().query("content.size:[0 TO 10240]").label("Small Files"),
new RequestFacetQuery().query("content.mimetype:'text/plain'").label("Plain Text"),
new RequestFacetQuery().query("content.mimetype:'image/jpeg' OR content.mimetype:'image/png' OR content.mimetype:'image/gif'").label("Images"),
new RequestFacetQuery().query("content.mimetype:'application/msword' OR content.mimetype:'application/vnd.ms-excel'").label("Office")
);
RequestFacetFields facetFields = new RequestFacetFields().facets(Arrays.asList(new RequestFacetFieldsFacets().field("creator")));
QueryBody facetQueryBody = new QueryBody().query(facetQuery).facetQueries(facetsQuery).facetFields(facetFields);
ResultSetRepresentation<ResultNodeRepresentation> facetResult = searchAPI.searchCall(facetQueryBody).execute().body();
Assert.assertTrue(facetResult.getCount() >= 3);
Assert.assertEquals(facetResult.getContext().getFacetQueries().size(), 4);
Assert.assertEquals(facetResult.getContext().getFacetFields().get(0).getBuckets().size(), 2);
Assert.assertEquals(facetResult.getList().size(), 3);
//Search with term highlighting
RequestQuery highlightQuery = new RequestQuery().query("(name:\"test*\" OR title:\"test*\") AND TYPE:\"cm:content\"");
List<RequestHighlightField> highlightField = Arrays.asList(
new RequestHighlightField().field("cm:name").prefix("(").postfix(")"),
new RequestHighlightField().field("{http://www.alfresco.org/model/content/1.0}title")
);
RequestHighlight highlight = new RequestHighlight().fields(highlightField);
QueryBody highlightQueryBody = new QueryBody().query(highlightQuery).highlight(highlight);
ResultSetRepresentation<ResultNodeRepresentation> highlightResult = searchAPI.searchCall(highlightQueryBody).execute().body();
Assert.assertTrue(highlightResult.getCount() >= 3);
Assert.assertEquals(highlightResult.getList().size(), 3);
Assert.assertEquals(highlightResult.getList().get(0).getSearch().getHighlight().size(), 2);