aboutsummaryrefslogtreecommitdiffstats
path: root/sample-apps
diff options
context:
space:
mode:
authorKristian Aune <kraune@yahoo-inc.com>2017-01-03 15:02:42 +0100
committerKristian Aune <kraune@yahoo-inc.com>2017-01-03 15:02:42 +0100
commit7c221b8396009f2447b244a691ca85ad59c44ab0 (patch)
tree0f011ae1a2f71764bbb8fd3e0dcd5f54c4442b2c /sample-apps
parent033dd952465ff295762df0e364c77b16fbe9b4a0 (diff)
Add java to basic search
- this builds and is coherent with documentation to be committed shortly - will probably change the java code shortly to fit the tutorial, but commit now as I got pom.xml right
Diffstat (limited to 'sample-apps')
-rw-r--r--sample-apps/basic-search/pom.xml41
-rw-r--r--sample-apps/basic-search/src/main/java/com/yahoo/example/ExampleDocumentProcessor.java99
-rw-r--r--sample-apps/basic-search/src/main/java/com/yahoo/example/ExampleSearcher.java36
-rw-r--r--sample-apps/basic-search/src/main/resources/configdefinitions/message.def4
4 files changed, 180 insertions, 0 deletions
diff --git a/sample-apps/basic-search/pom.xml b/sample-apps/basic-search/pom.xml
new file mode 100644
index 00000000000..5801d201595
--- /dev/null
+++ b/sample-apps/basic-search/pom.xml
@@ -0,0 +1,41 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+ <groupId>com.yahoo.example</groupId>
+ <artifactId>basic-application</artifactId>
+ <packaging>container-plugin</packaging> <!-- Use Vespa packaging -->
+ <version>1.0.1</version>
+
+ <build>
+ <plugins>
+ <plugin> <!-- Build the bundles -->
+ <groupId>com.yahoo.vespa</groupId>
+ <artifactId>bundle-plugin</artifactId>
+ <version>6-SNAPSHOT</version>
+ <extensions>true</extensions>
+ </plugin>
+ <plugin> <!-- Zip the application package -->
+ <groupId>com.yahoo.vespa</groupId>
+ <artifactId>vespa-application-maven-plugin</artifactId>
+ <version>6-SNAPSHOT</version>
+ <executions>
+ <execution>
+ <goals>
+ <goal>packageApplication</goal>
+ </goals>
+ </execution>
+ </executions>
+ </plugin>
+ </plugins>
+ </build>
+
+ <dependencies>
+ <dependency> <!-- Vespa dependencies -->
+ <groupId>com.yahoo.vespa</groupId>
+ <artifactId>container</artifactId>
+ <version>6-SNAPSHOT</version>
+ <scope>provided</scope>
+ </dependency>
+ </dependencies>
+
+</project>
diff --git a/sample-apps/basic-search/src/main/java/com/yahoo/example/ExampleDocumentProcessor.java b/sample-apps/basic-search/src/main/java/com/yahoo/example/ExampleDocumentProcessor.java
new file mode 100644
index 00000000000..cbcb5ae406c
--- /dev/null
+++ b/sample-apps/basic-search/src/main/java/com/yahoo/example/ExampleDocumentProcessor.java
@@ -0,0 +1,99 @@
+package com.yahoo.example;
+
+import com.yahoo.docproc.DocumentProcessor;
+import com.yahoo.docproc.Processing;
+import com.yahoo.document.Document;
+import com.yahoo.document.DocumentOperation;
+import com.yahoo.document.DocumentPut;
+import com.yahoo.document.DocumentRemove;
+import com.yahoo.document.DocumentUpdate;
+
+import java.net.*;
+import java.io.*;
+
+import com.yahoo.document.datatypes.StringFieldValue;
+import org.json.*;
+
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+/**
+ * A document processor
+ *
+ * @author Joe Developer
+ */
+public class ExampleDocumentProcessor extends DocumentProcessor {
+
+ private static final String artistField = "artist";
+ private static final String artistIdField = "artistId";
+ private static final String spotifyUrl = "https://api.spotify.com/v1/search?type=artist&limit=1&q=";
+ private static final Logger log = Logger.getLogger(ExampleDocumentProcessor.class.getName());
+
+ public Progress process(Processing processing) {
+ for (DocumentOperation op : processing.getDocumentOperations()) {
+ if (op instanceof DocumentPut) {
+ Document document = ((DocumentPut) op).getDocument();
+ addArtistId(document);
+ }
+ else if (op instanceof DocumentUpdate) {
+ DocumentUpdate update = (DocumentUpdate) op;
+ // Updates to existing documents can be modified here
+ }
+ else if (op instanceof DocumentRemove) {
+ DocumentRemove remove = (DocumentRemove) op;
+ // Document removes can be modified here
+ }
+ }
+ return Progress.DONE;
+ }
+
+ /** Queries the Spotify API, parses JSON and sets Artist ID */
+ public void addArtistId(Document document) {
+ StringFieldValue artistString = (StringFieldValue) document.getFieldValue(artistField);
+
+ HttpURLConnection connection = null;
+ try {
+ connection = getConnection(spotifyUrl + java.net.URLEncoder.encode(artistString.getString(), "UTF-8"));
+ String artistId = parseSpotifyResponse(new InputStreamReader(connection.getInputStream(), "UTF-8"));
+ if (artistId == null) return;
+
+ document.setFieldValue(artistIdField, new StringFieldValue(artistId));
+ }
+ catch (Exception e) {
+ log.log(Level.WARNING, "Could not find artist id for '" + document.getId(), e);
+ }
+ finally {
+ if (connection != null)
+ connection.disconnect();
+ }
+ }
+
+ /** Returns an artist id from spotify or null if not found */
+ private String parseSpotifyResponse(InputStreamReader streamReader) throws IOException, JSONException {
+ // Read JSON data from API
+ BufferedReader reader = new BufferedReader(streamReader);
+ StringBuilder builder = new StringBuilder();
+ for (String line; (line = reader.readLine()) != null; ) {
+ builder.append(line).append("\n");
+ }
+
+ // Parse the JSON to find the first artist item returned or null if not found
+ JSONObject json = new JSONObject(builder.toString());
+ JSONObject artists = json.getJSONObject("artists");
+ JSONArray items = artists.getJSONArray("items");
+ JSONObject artist = items.getJSONObject(0);
+ return artist.getString("id");
+ }
+
+ /** Establishes an HTTP Connection */
+ private HttpURLConnection getConnection(String urlString) throws IOException {
+ HttpURLConnection connection = (HttpURLConnection) new URL(urlString).openConnection();
+ connection.setRequestProperty("User-Agent", "Vespa Tutorial DocProc");
+ connection.setReadTimeout(10000);
+ connection.setConnectTimeout(5000);
+ connection.connect();
+ return connection;
+ }
+
+}
+
diff --git a/sample-apps/basic-search/src/main/java/com/yahoo/example/ExampleSearcher.java b/sample-apps/basic-search/src/main/java/com/yahoo/example/ExampleSearcher.java
new file mode 100644
index 00000000000..76b07a46b93
--- /dev/null
+++ b/sample-apps/basic-search/src/main/java/com/yahoo/example/ExampleSearcher.java
@@ -0,0 +1,36 @@
+package com.yahoo.example;
+
+import com.yahoo.search.Query;
+import com.yahoo.search.Result;
+import com.yahoo.search.Searcher;
+import com.yahoo.search.result.Hit;
+import com.yahoo.search.searchchain.Execution;
+
+/**
+ * A searcher adding a new hit.
+ *
+ * @author Joe Developer
+ */
+public class ExampleSearcher extends Searcher {
+
+ public static final String hitId = "ExampleHit";
+ private final String message;
+
+ public ExampleSearcher(MessageConfig config) {
+ message = config.message();
+ }
+
+ public Result search(Query query, Execution execution) {
+ // Pass on to the next searcher to execute the query
+ Result result = execution.search(query);
+
+ // Add an extra hit to the result
+ Hit hit = new Hit(hitId);
+ hit.setField("message", message);
+ result.hits().add(hit);
+
+ // Return it to the upstream searcher, or to rendering
+ return result;
+ }
+
+}
diff --git a/sample-apps/basic-search/src/main/resources/configdefinitions/message.def b/sample-apps/basic-search/src/main/resources/configdefinitions/message.def
new file mode 100644
index 00000000000..95039ec77c7
--- /dev/null
+++ b/sample-apps/basic-search/src/main/resources/configdefinitions/message.def
@@ -0,0 +1,4 @@
+namespace=example
+
+# Explanation of the 'message' config variable
+message string default="Hello, World!" \ No newline at end of file