No worries, I've been writing a few examples how to use the QueryBuilder api but I can surely write another small example here.
First of we want to state which page we are wanting to search on like following:
String searchPath = "/conent/mysite/en/category/mypage";
this could also be sent to the code through some other way… but just say that we have somehow defined the path to the page we are investigating.
The next step would be to set-up the query like this:
//some code …. HashMap<String, String> map = new HashMap<String, String>(); map.put("path", searchPath); map.put("type", "nt:unstructured"); //Here we specify which property we are looking for on the node = "sling:resourceType" map.put("property", "sling:resourceType"); //Here we specify which property value for resourceType you are looking for = "foundation/components/text" map.put("property.value", "foundation/components/text"); //Here we can have a limit on the number of text components we are looking for -1 = unlimited map.put("p.limit", Integer.toString(this.paginateAfter)); // same as query.setHitsPerPage(20) below //Initate the querybuilder QueryBuilder builder = request.getResourceResolver().adaptTo(QueryBuilder.class); Session session = request.getResourceResolver().adaptTo(Session.class); //Create the query with the new search predicate group we made above that define our search criteria Query query = builder.createQuery(PredicateGroup.create(map), session); //Get the query result SearchResult result = query.getResult(); //Get the hits List<Hit> hits = result.getHits(); //Iterate over all the hits (where a hit represent a text component hit) for (Hit hit: hits) { Resource textResource = hit.getResource(); //Here we extract the text component resource from the hit ValueMap textProperties = textResource.adaptTo(ValueMap.class); //Here we fetch all the properties from that resource String textText = textProperties.get("text", String.class); //Here we fetch the text that has been entered into the text property //Do something with that text, in this case some transformation that you already have implementet } //More code...Hopefully that will get you started, good luck :)
/Johan