From 79084dbb9ed4eff902e7a2ca3110e0a50f72e9d1 Mon Sep 17 00:00:00 2001 From: andrewlalis Date: Tue, 10 Oct 2023 08:08:19 -0400 Subject: [PATCH] Added basic front-end for the search API for testing. --- .../d_package_search/DPackageSearch.java | 8 +-- .../d_package_search/WebApiRunner.java | 8 +++ .../impl/LucenePackageSearcher.java | 2 + src/main/resources/index.html | 53 +++++++++++++++++++ 4 files changed, 67 insertions(+), 4 deletions(-) create mode 100644 src/main/resources/index.html diff --git a/src/main/java/com/andrewlalis/d_package_search/DPackageSearch.java b/src/main/java/com/andrewlalis/d_package_search/DPackageSearch.java index 39e106a..273f418 100644 --- a/src/main/java/com/andrewlalis/d_package_search/DPackageSearch.java +++ b/src/main/java/com/andrewlalis/d_package_search/DPackageSearch.java @@ -11,10 +11,10 @@ import java.util.ArrayList; public class DPackageSearch { public static void main(String[] args) { Path indexPath = Path.of("package-index"); - startIndexerThread(new IndexGenerator( - new DubRegistryPackageFetcher(), - () -> new LucenePackageIndexer(indexPath) - )); +// startIndexerThread(new IndexGenerator( +// new DubRegistryPackageFetcher(), +// () -> new LucenePackageIndexer(indexPath) +// )); new WebApiRunner(new LucenePackageSearcher(indexPath)).run(); } diff --git a/src/main/java/com/andrewlalis/d_package_search/WebApiRunner.java b/src/main/java/com/andrewlalis/d_package_search/WebApiRunner.java index a613f08..2406f56 100644 --- a/src/main/java/com/andrewlalis/d_package_search/WebApiRunner.java +++ b/src/main/java/com/andrewlalis/d_package_search/WebApiRunner.java @@ -7,6 +7,7 @@ import org.eclipse.jetty.server.*; import org.eclipse.jetty.util.Callback; import org.eclipse.jetty.util.thread.QueuedThreadPool; +import java.io.IOException; import java.net.URLDecoder; import java.nio.ByteBuffer; import java.nio.charset.StandardCharsets; @@ -63,6 +64,13 @@ public final class WebApiRunner extends Handler.Abstract implements Runnable { byte[] responseBody = objectMapper.writeValueAsBytes(results); response.write(true, ByteBuffer.wrap(responseBody), callback); } + } else if (uri.getPath().equalsIgnoreCase("/index.html") || uri.getPath().equalsIgnoreCase("/")) { + try (var in = WebApiRunner.class.getClassLoader().getResourceAsStream("index.html")) { + if (in == null) throw new IOException("Resource doesn't exist."); + response.setStatus(HttpStatus.OK_200); + response.getHeaders().add("Content-Type", "text/html; charset=utf-8"); + response.write(true, ByteBuffer.wrap(in.readAllBytes()), callback); + } } else { response.setStatus(HttpStatus.NOT_FOUND_404); } diff --git a/src/main/java/com/andrewlalis/d_package_search/impl/LucenePackageSearcher.java b/src/main/java/com/andrewlalis/d_package_search/impl/LucenePackageSearcher.java index 8bc61fa..43e576d 100644 --- a/src/main/java/com/andrewlalis/d_package_search/impl/LucenePackageSearcher.java +++ b/src/main/java/com/andrewlalis/d_package_search/impl/LucenePackageSearcher.java @@ -55,12 +55,14 @@ public class LucenePackageSearcher implements PackageSearcher { BooleanQuery.Builder queryBuilder = new BooleanQuery.Builder(); String[] searchTerms = queryText.toLowerCase().split("\\s+"); + // We define a set of weighted fields that we will add prefix queries for. Map weightedFields = Map.of( "name", 1f, "description", 0.5f, "readme", 0.25f ); + // Only consider the first 5 search terms, and add a prefix query for each term for them. for (int i = 0; i < Math.min(5, searchTerms.length); i++) { for (var entry : weightedFields.entrySet()) { String fieldName = entry.getKey(); diff --git a/src/main/resources/index.html b/src/main/resources/index.html new file mode 100644 index 0000000..f3f7c46 --- /dev/null +++ b/src/main/resources/index.html @@ -0,0 +1,53 @@ + + + + + D Package Search + + + +

D Package Search

+ +

+ Use this site to search for D packages! +

+ + + +
+ + + + \ No newline at end of file