use of com.yahoo.search.pagetemplates.model.PageElement in project vespa by vespa-engine.
the class ChoicesTestCase method testExecution.
@Test
public void testExecution() {
// Create the page template (second alternative will be chosen)
List<PageElement> pages = new ArrayList<>();
pages.add(importPage("AnySource.xml"));
pages.add(importPage("Choices.xml"));
Choice page = Choice.createSingletons(pages);
// Create a federated result
Query query = new Query();
Result result = new Result(query);
result.hits().add(createHits("news", 3));
result.hits().add(createHits("web", 3));
result.hits().add(createHits("blog", 3));
result.hits().add(createHits("images", 3));
// Resolve
Resolver resolver = new DeterministicResolver();
Resolution resolution = resolver.resolve(page, query, result);
// Execute
Organizer organizer = new Organizer();
organizer.organize(page, resolution, result);
// Check rendering
assertRendered(result, "ChoicesResult.xml");
}
use of com.yahoo.search.pagetemplates.model.PageElement in project vespa by vespa-engine.
the class PageTemplateSearcherTestCase method createPageTemplateRegistry.
private PageTemplateRegistry createPageTemplateRegistry() {
PageTemplateRegistry registry = new PageTemplateRegistry();
PageTemplate twoSources = new PageTemplate(new ComponentId("default"));
twoSources.getSection().elements().add(new com.yahoo.search.pagetemplates.model.Source("source1"));
twoSources.getSection().elements().add(new com.yahoo.search.pagetemplates.model.Source("source2"));
registry.register(twoSources);
PageTemplate oneSource = new PageTemplate(new ComponentId("oneSource"));
oneSource.getSection().elements().add(new com.yahoo.search.pagetemplates.model.Source("source1"));
registry.register(oneSource);
PageTemplate threeSources = new PageTemplate(new ComponentId("threeSources"));
threeSources.getSection().elements().add(new com.yahoo.search.pagetemplates.model.Source("source1"));
threeSources.getSection().elements().add(new com.yahoo.search.pagetemplates.model.Source("source2"));
threeSources.getSection().elements().add(new com.yahoo.search.pagetemplates.model.Source("source3"));
registry.register(threeSources);
PageTemplate twoSourcesAndAny = new PageTemplate(new ComponentId("twoSourcesAndAny"));
twoSourcesAndAny.getSection().elements().add(new com.yahoo.search.pagetemplates.model.Source("source1"));
twoSourcesAndAny.getSection().elements().add(new com.yahoo.search.pagetemplates.model.Source("source2"));
twoSourcesAndAny.getSection().elements().add(com.yahoo.search.pagetemplates.model.Source.any);
registry.register(twoSourcesAndAny);
PageTemplate anySource = new PageTemplate(new ComponentId("anySource"));
anySource.getSection().elements().add(com.yahoo.search.pagetemplates.model.Source.any);
registry.register(anySource);
PageTemplate choiceOfSources = new PageTemplate(new ComponentId("choiceOfSources"));
List<PageElement> alternatives = new ArrayList<>();
alternatives.add(new com.yahoo.search.pagetemplates.model.Source("source1"));
alternatives.add(new com.yahoo.search.pagetemplates.model.Source("source2"));
choiceOfSources.getSection().elements().add(Choice.createSingletons(alternatives));
registry.register(choiceOfSources);
registry.freeze();
return registry;
}
use of com.yahoo.search.pagetemplates.model.PageElement in project vespa by vespa-engine.
the class PageTemplateSearcher method selectPageTemplates.
/**
* Returns the list of page templates specified in the query, or the default if none, or the
* empty list if no default, never null.
*/
private List<PageElement> selectPageTemplates(Query query) {
// Determine the list of page template ids
@SuppressWarnings("unchecked") List<String> pageIds = (List<String>) query.properties().get(pageIdListName);
if (pageIds == null) {
String pageIdString = query.properties().getString(pageIdName, "").trim();
if (pageIdString.length() > 0)
pageIds = Arrays.asList(pageIdString.split(" "));
}
// If none set, just return the default or null if none
if (pageIds == null) {
PageElement defaultPage = templateRegistry.getComponent("default");
return (defaultPage == null ? Collections.<PageElement>emptyList() : Collections.singletonList(defaultPage));
}
// Resolve the id list to page templates
List<PageElement> pages = new ArrayList<>(pageIds.size());
for (String pageId : pageIds) {
PageTemplate page = templateRegistry.getComponent(pageId);
if (page == null)
query.errors().add(ErrorMessage.createInvalidQueryParameter("Could not resolve requested page template '" + pageId + "'"));
else
pages.add(page);
}
return pages;
}
use of com.yahoo.search.pagetemplates.model.PageElement in project vespa by vespa-engine.
the class PageTemplateSearcher method search.
@Override
public Result search(Query query, Execution execution) {
// Pre execution: Choose template and sources
List<PageElement> pages = selectPageTemplates(query);
// Bypass if no page template chosen
if (pages.isEmpty())
return execution.search(query);
addSources(pages, query);
// Set the page template list for inspection by other searchers
query.properties().set(pagePageTemplateListName, pages);
// Execute
Result result = execution.search(query);
// Post execution: Resolve choices and organize the result as dictated by the resolved template
Choice pageTemplateChoice = Choice.createSingletons(pages);
Resolution resolution = selectResolver(query).resolve(pageTemplateChoice, query, result);
organizer.organize(pageTemplateChoice, resolution, result);
return result;
}
use of com.yahoo.search.pagetemplates.model.PageElement in project vespa by vespa-engine.
the class PageTemplateSearcher method addSources.
/**
* Sets query.getModel().getSources() to the right value and add source parameters specified in templates
*/
private void addSources(List<PageElement> pages, Query query) {
// Determine all wanted sources
Set<Source> pageSources = new HashSet<>();
for (PageElement page : pages) pageSources.addAll(((PageTemplate) page).getSources());
addErrorIfSameSourceMultipleTimes(pages, pageSources, query);
if (query.getModel().getSources().size() > 0) {
// Add properties if the source list is set explicitly, but do not modify otherwise
addParametersForIncludedSources(pageSources, query);
return;
}
if (pageSources.contains(Source.any)) {
IntentModel intentModel = IntentModel.getFrom(query);
if (intentModel != null) {
query.getModel().getSources().addAll(intentModel.getSourceNames());
addPageTemplateSources(pageSources, query);
}
// otherwise leave empty to search all
} else {
// Let the page templates decide
addPageTemplateSources(pageSources, query);
}
}
Aggregations