in src/main/java/com/twitter/nodes_examples/search/SearchGraph.java [43:74]
public SearchGraph(Node<SearchRequest> requestNode) {
// look up user score using UserScoreServiceNode, this step is independent from search
Node<Double> userScoreNode =
Node.ifThenElse(
requestNode.map("hasUserId", r -> r.getUserId() > 0),
new UserScoreServiceNode(
requestNode.map("getUserId", SearchRequest::getUserId)),
Node.value(0.0, "defaultUserScore"));
// Search the index and find result ids for the given query
Node<List<Long>> resultIdsNode = Node.build(
SearchIndexNode.class,
SearchIndexNode.D.QUERY, requestNode.map("getQuery", SearchRequest::getQuery),
SearchIndexNode.D.NUM_RESULTS, requestNode.map("getNumResults",
r -> r.getNumResults() > 0 ? r.getNumResults() : 10));
// Hydrate the ids returned from the index
Node<Map<Long, String>> hydrationMapNode = Node.build(
HydrationNode.class,
HydrationNode.D.ID_LIST, resultIdsNode,
HydrationNode.D.PREFIX, Node.value("cool_prefix", "newPrefix"));
// Combined the hydration information and the user score to create the final response
this.responseNode = Node.build(
BuildResponseNode.class,
BuildResponseNode.D.USER_SCORE, userScoreNode,
BuildResponseNode.D.RESULT_ID_LIST, resultIdsNode,
BuildResponseNode.D.HYDRATION_MAP, hydrationMapNode)
.when(requestNode.map("hasQuery", r -> !r.getQuery().isEmpty()));
markExposedNodes();
}