summaryrefslogtreecommitdiffstats
path: root/sample-apps/http-api-using-searcher/src/main/java/com/yahoo/demo
diff options
context:
space:
mode:
authorKristian Aune <kraune@yahoo-inc.com>2017-08-18 09:14:46 +0200
committerKristian Aune <kraune@yahoo-inc.com>2017-08-18 09:14:46 +0200
commite36ce286e3cc2026fe0ccbbf0e116477b23c70e1 (patch)
tree3d3dadc8869629d7ebc7d019a400fc641a072c4c /sample-apps/http-api-using-searcher/src/main/java/com/yahoo/demo
parenta618787ba847eed7c30f6bd81256db8d39e5e2c4 (diff)
Sample apps moved to vespa-engine/sample-apps
Diffstat (limited to 'sample-apps/http-api-using-searcher/src/main/java/com/yahoo/demo')
-rw-r--r--sample-apps/http-api-using-searcher/src/main/java/com/yahoo/demo/DemoComponent.java45
-rw-r--r--sample-apps/http-api-using-searcher/src/main/java/com/yahoo/demo/DemoHandler.java50
-rw-r--r--sample-apps/http-api-using-searcher/src/main/java/com/yahoo/demo/DemoRenderer.java74
-rw-r--r--sample-apps/http-api-using-searcher/src/main/java/com/yahoo/demo/DemoSearcher.java76
4 files changed, 0 insertions, 245 deletions
diff --git a/sample-apps/http-api-using-searcher/src/main/java/com/yahoo/demo/DemoComponent.java b/sample-apps/http-api-using-searcher/src/main/java/com/yahoo/demo/DemoComponent.java
deleted file mode 100644
index d9761d95b48..00000000000
--- a/sample-apps/http-api-using-searcher/src/main/java/com/yahoo/demo/DemoComponent.java
+++ /dev/null
@@ -1,45 +0,0 @@
-// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
-package com.yahoo.demo;
-
-import com.yahoo.component.AbstractComponent;
-
-import java.text.Normalizer;
-import java.util.HashSet;
-import java.util.Random;
-import java.util.Set;
-
-/**
- * A shared component with an "expensive" constructor exposing a shared,
- * thread-safe service.
- */
-public class DemoComponent extends AbstractComponent {
- private final Set<Integer> illegalHashes;
-
- public DemoComponent() {
- illegalHashes = new HashSet<Integer>();
- Random r = new Random();
- // generate up to 1e6 unique hashes
- for (int i = 0; i < 1000 * 1000; ++i) {
- illegalHashes.add(r.nextInt());
- }
- }
-
- /**
- * NFKC-normalize term, or replace it with "smurf" with a low probability.
- * Will change choice for each run, but will be constant in a single run of
- * the container.
- *
- * @param term
- * term to normalize or replace with "smurf"
- * @return NFKC-normalized term or "smurf"
- */
- public String normalize(String term) {
- String normalized = Normalizer.normalize(term, Normalizer.Form.NFKC);
- if (illegalHashes.contains(normalized.hashCode())) {
- return "smurf";
- } else {
- return normalized;
- }
- }
-
-}
diff --git a/sample-apps/http-api-using-searcher/src/main/java/com/yahoo/demo/DemoHandler.java b/sample-apps/http-api-using-searcher/src/main/java/com/yahoo/demo/DemoHandler.java
deleted file mode 100644
index e5a293846cc..00000000000
--- a/sample-apps/http-api-using-searcher/src/main/java/com/yahoo/demo/DemoHandler.java
+++ /dev/null
@@ -1,50 +0,0 @@
-// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
-package com.yahoo.demo;
-
-import com.google.inject.Inject;
-import com.yahoo.container.jdisc.HttpRequest;
-import com.yahoo.container.jdisc.HttpResponse;
-import com.yahoo.container.jdisc.LoggingRequestHandler;
-import com.yahoo.container.logging.AccessLog;
-import com.yahoo.search.handler.SearchHandler;
-import com.yahoo.search.query.Model;
-import com.yahoo.search.query.Presentation;
-
-import java.util.concurrent.Executor;
-
-/**
- * Forward requests to search handler after adding "Red Hat" as query and "demo"
- * as renderer ID.
- */
-public class DemoHandler extends LoggingRequestHandler {
-
- private final SearchHandler searchHandler;
-
- /**
- * Constructor for use in injection. The requested objects are subclasses of
- * component or have dedicated providers, so the container will know how to
- * create this handler.
- *
- * @param executor
- * threadpool, provided by Vespa
- * @param accessLog
- * access log for incoming queries, provided by Vespa
- * @param searchHandler
- * the Vespa search handler, also automatically injected
- */
- @Inject
- public DemoHandler(Executor executor, AccessLog accessLog,
- SearchHandler searchHandler) {
- super(executor, accessLog, null, true);
- this.searchHandler = searchHandler;
- }
-
- @Override
- public HttpResponse handle(HttpRequest request) {
- HttpRequest searchRequest = new HttpRequest.Builder(request)
- .put(Model.QUERY_STRING, "Red Hat")
- .put(Presentation.FORMAT, "demo").createDirectRequest();
- HttpResponse r = searchHandler.handle(searchRequest);
- return r;
- }
-}
diff --git a/sample-apps/http-api-using-searcher/src/main/java/com/yahoo/demo/DemoRenderer.java b/sample-apps/http-api-using-searcher/src/main/java/com/yahoo/demo/DemoRenderer.java
deleted file mode 100644
index dd5ea469620..00000000000
--- a/sample-apps/http-api-using-searcher/src/main/java/com/yahoo/demo/DemoRenderer.java
+++ /dev/null
@@ -1,74 +0,0 @@
-// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
-package com.yahoo.demo;
-
-import com.yahoo.search.Result;
-import com.yahoo.search.rendering.Renderer;
-import com.yahoo.search.result.Hit;
-import com.yahoo.search.result.HitGroup;
-
-import java.io.IOException;
-import java.io.Writer;
-import java.util.Iterator;
-
-/**
- * Render result sets as plain text. First line is whether an error occurred,
- * second rendering initialization time stamp, then each line is the ID of each
- * document returned, and the last line is time stamp for when the renderer was
- * finished.
- */
-public class DemoRenderer extends Renderer {
- private String heading;
-
- /**
- * No global, shared state to set.
- */
- public DemoRenderer() {
- }
-
- @Override
- protected void render(Writer writer, Result result) throws IOException {
- if (result.hits().getErrorHit() == null) {
- writer.write("OK\n");
- } else {
- writer.write("Oops!\n");
- }
- writer.write(heading);
- writer.write("\n");
- renderHits(writer, result.hits());
- writer.write("Rendering finished work: " + System.currentTimeMillis());
- writer.write("\n");
- }
-
- private void renderHits(Writer writer, HitGroup hits) throws IOException {
- for (Iterator<Hit> i = hits.deepIterator(); i.hasNext();) {
- Hit h = i.next();
- if (h.types().contains("summary")) {
- String id = h.getDisplayId();
- if (id != null) {
- writer.write(id);
- writer.write("\n");
- }
- }
- }
- }
-
- @Override
- public String getEncoding() {
- return "utf-8";
- }
-
- @Override
- public String getMimeType() {
- return "text/plain";
- }
-
- /**
- * Initialize mutable, per-result set state here.
- */
- @Override
- public void init() {
- long time = System.currentTimeMillis();
- heading = "Renderer initialized: " + time;
- }
-
-}
diff --git a/sample-apps/http-api-using-searcher/src/main/java/com/yahoo/demo/DemoSearcher.java b/sample-apps/http-api-using-searcher/src/main/java/com/yahoo/demo/DemoSearcher.java
deleted file mode 100644
index 8f57cba6c8b..00000000000
--- a/sample-apps/http-api-using-searcher/src/main/java/com/yahoo/demo/DemoSearcher.java
+++ /dev/null
@@ -1,76 +0,0 @@
-// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
-package com.yahoo.demo;
-
-import com.yahoo.component.chain.dependencies.After;
-import com.yahoo.component.chain.dependencies.Before;
-import com.yahoo.component.chain.dependencies.Provides;
-import com.yahoo.demo.DemoConfig.Demo;
-import com.yahoo.prelude.query.AndItem;
-import com.yahoo.prelude.query.CompositeItem;
-import com.yahoo.prelude.query.Item;
-import com.yahoo.prelude.query.WordItem;
-import com.yahoo.processing.request.CompoundName;
-import com.yahoo.search.Query;
-import com.yahoo.search.Result;
-import com.yahoo.search.Searcher;
-import com.yahoo.search.query.QueryTree;
-import com.yahoo.search.searchchain.Execution;
-import com.yahoo.search.searchchain.PhaseNames;
-
-import java.util.List;
-
-/**
- * A searcher for adding a set of configured terms as AND terms, and add a
- * single term from the request to the query tree (after running the term
- * through a shared component).
- */
-@After(PhaseNames.RAW_QUERY)
-@Before(PhaseNames.TRANSFORMED_QUERY)
-@Provides(DemoSearcher.DEMO_TRANSFORM)
-public class DemoSearcher extends Searcher {
- public static final String DEMO_TRANSFORM = "com.yahoo.demo.DemoSearcher.NothingUseful";
-
- /**
- * The request property with this name will be filtered and added to the
- * query as an AND term.
- */
- public static final CompoundName EXTRA_TERM = new CompoundName("extraTerm");
-
- private final List<Demo> extraTerms;
-
- private final DemoComponent infrastructure;
-
- public DemoSearcher(DemoComponent infrastructure, DemoConfig extraTerms) {
- this.extraTerms = extraTerms.demo();
- this.infrastructure = infrastructure;
- }
-
- /**
- * Programmatic query transform, add terms from config and the EXTRA_TERM
- * request property.
- */
- @Override
- public Result search(Query query, Execution execution) {
- QueryTree q = query.getModel().getQueryTree();
- addAndItem(q, infrastructure.normalize(
- query.properties().getString(EXTRA_TERM)));
- for (Demo d : extraTerms) {
- addAndItem(q, d.term());
- }
- return execution.search(query);
- }
-
- private void addAndItem(QueryTree q, String term) {
- Item root = q.getRoot();
- CompositeItem compositeRoot;
- if (root instanceof AndItem) {
- compositeRoot = (CompositeItem) root;
- } else {
- compositeRoot = new AndItem();
- compositeRoot.addItem(root);
- q.setRoot(compositeRoot);
- }
- compositeRoot.addItem(new WordItem(term));
- }
-
-}