Skip to content

Commit

Permalink
Wire up UI config values
Browse files Browse the repository at this point in the history
excerpt length, number of results, number of excerpts
  • Loading branch information
jameslittle230 committed Feb 4, 2023
1 parent c2f7593 commit 5084298
Show file tree
Hide file tree
Showing 13 changed files with 183 additions and 40 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 26 additions & 6 deletions dev/site/all-options.html
Expand Up @@ -23,9 +23,20 @@
<button id="btn-4">4: Download Index - 404</button>
<button id="btn-5">5: Attach</button>
<button id="btn-6">6: Attach (Wrong name)</button>
<textarea id="attach-config" cols="30" rows="10">
{
"excerptLength": 150,
"numberOfResults": 10,
"numberOfExcerpts": 5
}
</textarea>
</div>
<div class="stork-wrapper">
<input data-stork="federalist" placeholder="liberty" class="stork-input" />
<input
data-stork="federalist"
placeholder="liberty"
class="stork-input"
/>
<div data-stork="federalist-output" class="stork-output"></div>
</div>
</div>
Expand All @@ -48,9 +59,11 @@
});

document.getElementById("btn-2").addEventListener("click", () => {
stork.initialize("http://127.0.0.1:8025/does-not-exist.wasm").then((value) => {
console.log("WASM initialized", value);
});
stork
.initialize("http://127.0.0.1:8025/does-not-exist.wasm")
.then((value) => {
console.log("WASM initialized", value);
});
});

document.getElementById("btn-3").addEventListener("click", () => {
Expand All @@ -66,7 +79,10 @@

document.getElementById("btn-4").addEventListener("click", () => {
stork
.downloadIndex("federalist", "http://127.0.0.1:8025/does-not-exist.st")
.downloadIndex(
"federalist",
"http://127.0.0.1:8025/does-not-exist.st"
)
.then(() => {
console.log("Index loaded");
})
Expand All @@ -76,7 +92,11 @@
});

document.getElementById("btn-5").addEventListener("click", () => {
stork.attach("federalist");
console.log(document.getElementById("attach-config").value);
stork.attach(
"federalist",
JSON.parse(document.getElementById("attach-config").value)
);
});

document.getElementById("btn-6").addEventListener("click", () => {
Expand Down
5 changes: 4 additions & 1 deletion js/config.ts
Expand Up @@ -50,7 +50,10 @@ const defaultUIConfig = {
onQueryUpdate: (_query: string) => {},
// eslint-disable-next-line @typescript-eslint/no-empty-function
onResultSelected: (_query: string, _result: SearchResult) => {},
transformResultUrl: (url: string) => url
transformResultUrl: (url: string) => url,
excerptLength: 150,
numberOfResults: 10,
numberOfExcerpts: 5
};

export type UIConfig = Readonly<typeof defaultUIConfig>;
Expand Down
10 changes: 9 additions & 1 deletion js/entity.ts
Expand Up @@ -22,6 +22,7 @@ export default class Entity implements EntityDomDelegate {
readonly config: RegisterConfiguration;
readonly indexLoadPromise: Promise<IndexLoadValue>;

private uiConfig: UIConfig;
private domManager: EntityDomManager;
private loadManager: LoadManager;
private indexLoader: IndexLoader;
Expand Down Expand Up @@ -103,6 +104,7 @@ export default class Entity implements EntityDomDelegate {
}

attach(uiConfig: UIConfig) {
this.uiConfig = uiConfig;
this.domManager.attach(uiConfig);
}

Expand All @@ -115,7 +117,13 @@ export default class Entity implements EntityDomDelegate {
log(`Performing search for index "${this.name}" with query "${query}"`);

try {
const v = perform_search(this.name, query);
const v = perform_search(
this.name,
query,
this.uiConfig?.excerptLength,
this.uiConfig?.numberOfResults,
this.uiConfig?.numberOfExcerpts
);
const value = JSON.parse(v);
return {
success: true,
Expand Down
4 changes: 4 additions & 0 deletions js/entityDomManager.ts
Expand Up @@ -82,6 +82,10 @@ export default class EntityDomManager {
this.attachedToDom = true;

this.resetElements();

if (this.input.value.length > 0) {
this.performSearchFromInputValue();
}
}

setProgress(number: number) {
Expand Down
18 changes: 12 additions & 6 deletions readme.md
Expand Up @@ -171,7 +171,7 @@ $ stork search --index federalist.st --query "liberty"

To build an interactive, online search interface, you can use the Stork Javascript library to load your search index and attach it to HTML on your webpage.

You'll need to take the output file generated in the previous step and make it accessible on a web server. I've already done that and uploaded the search index to `https://files.stork-search.net/federalist.st`.
You'll need to take the output file generated in the previous step and make it accessible on a web server. I've already done that and uploaded the search index to `https://files.stork-search.net/releases/latest/federalist.st`.

Stork looks for the `data-stork` attributes on two tags: an `<input>` tag where your users will type their search query, and a `<div>` tag where Stork will render the search results. Here, we're setting up our input and output elements with the name "federalist"—we'll use that name later to point the Javascript library at the correct HTML tags.

Expand All @@ -195,7 +195,10 @@ By default, Stork's output is completely unstyled, letting you customize the out
<html lang="en">
<head>
<title>Search</title>
<link rel="stylesheet" href="https://files.stork-search.net/basic.css" />
<link
rel="stylesheet"
href="https://files.stork-search.net/releases/latest/basic.css"
/>
</head>
<body>
<div class="stork-wrapper">
Expand All @@ -215,19 +218,22 @@ Finally, we'll load the Stork Javascript library and register our search index:
<html lang="en">
<head>
<title>Search</title>
<link rel="stylesheet" href="https://files.stork-search.net/basic.css" />
<link
rel="stylesheet"
href="https://files.stork-search.net/releases/latest/basic.css"
/>
</head>
<body>
<div class="stork-wrapper">
<input data-stork="federalist" class="stork-input" />
<div data-stork="federalist-output" class="stork-output"></div>
</div>

<script src="https://files.stork-search.net/stork.js"></script>
<script src="https://files.stork-search.net/releases/latest/stork.js"></script>
<script>
stork.register(
"federalist",
"https://files.stork-search.net/federalist.st",
"https://files.stork-search.net/releases/latest/federalist.st",
{}
);
</script>
Expand All @@ -237,7 +243,7 @@ Finally, we'll load the Stork Javascript library and register our search index:

> **Warning**
>
> The files at the root of `files.stork-search.net` point directly to the build artifacts from most recent release. Linking to these files from your webpage can result in unexpected behavior when a new version is released. To pin to a specific release, use URLs in the following the format:
> The files linked in this demo point directly to the build artifacts from most recent release. Linking to these files from your webpage can result in unexpected behavior when a new version is released. To pin to a specific release, use URLs in the following the format:
>
> `https://files.stork-search.net/releases/v2.0.0/stork.js`
>
Expand Down
1 change: 1 addition & 0 deletions stork-cli/Cargo.toml
Expand Up @@ -27,6 +27,7 @@ hyper = { version = "0.14.17", optional = true, features = [
"tcp"
] }
indicatif = "0.17.0"
lazy_static = "1.4.0"
num-format = "0.4.0"
serde = "1.0.130"
serde_json = "1.0.68"
Expand Down
52 changes: 44 additions & 8 deletions stork-cli/src/app/search.rs
@@ -1,6 +1,18 @@
use clap::{Arg, Command};
use lazy_static::lazy_static;
use stork_lib::SearchConfig;

pub(super) fn search_subcommand() -> Command<'static> {
lazy_static! {
static ref DEFAULT_SEARCH_CONFIG: SearchConfig = SearchConfig::default();
static ref DEFAULT_NUMBER_OF_EXCERPTS: String =
DEFAULT_SEARCH_CONFIG.number_of_excerpts.to_string();
static ref DEFAULT_EXCERPT_LENGTH: String =
DEFAULT_SEARCH_CONFIG.excerpt_length.to_string();
static ref DEFAULT_NUMBER_OF_RESULTS: String =
DEFAULT_SEARCH_CONFIG.number_of_results.to_string();
}

Command::new("search")
.about("Search an index for a query.")
.arg(
Expand All @@ -21,21 +33,45 @@ pub(super) fn search_subcommand() -> Command<'static> {
.help("The text with which to search the index")
.required(true),
)
.next_help_heading("DIAGNOSTICS")
.arg(
Arg::with_name("timing")
.short('t')
.long("timing")
.help("Displays the duration of the search operation"),
)
.next_help_heading("DISPLAY")
.arg(
Arg::with_name("format")
.long("format")
.display_order(100)
.takes_value(true)
.value_name("FORMAT")
.possible_values(["json", "pretty", "none"])
.default_value("pretty")
.help("The output format for the returned search results"),
)
.arg(
Arg::with_name("number_of_excerpts")
.long("number-of-excerpts")
.takes_value(true)
.value_name("NUMBER_OF_EXCERPTS")
.default_value(&DEFAULT_NUMBER_OF_EXCERPTS)
.help("The maximum number of excerpts to return for each result."),
)
.arg(
Arg::with_name("number_of_results")
.long("number-of-results")
.takes_value(true)
.value_name("NUMBER_OF_RESULTS")
.default_value(&DEFAULT_NUMBER_OF_RESULTS)
.help("The maximum number of documents to return in the search output."),
)
.arg(
Arg::with_name("excerpt_length")
.long("excerpt-length")
.takes_value(true)
.value_name("EXCERPT_LENGTH")
.default_value(&DEFAULT_EXCERPT_LENGTH)
.help("The length, in characters, of each text excerpt returned in the search output."),
)
.next_help_heading("DIAGNOSTICS")
.arg(
Arg::with_name("timing")
.short('t')
.long("timing")
.help("Displays the duration of the search operation"),
)
}
21 changes: 20 additions & 1 deletion stork-cli/src/main.rs
Expand Up @@ -11,6 +11,7 @@ mod timings;

use clap::ArgMatches;
use colored::Colorize;
use lib::SearchConfig;
use num_format::{Locale, ToFormattedString};
use std::time::Instant;

Expand Down Expand Up @@ -89,7 +90,25 @@ fn search(submatches: &ArgMatches) -> CommandOutput {

let read_time = Instant::now();

let results = lib::search(&index, query).unwrap();
let search_config = SearchConfig {
excerpt_length: submatches
.value_of("excerpt_length")
.unwrap()
.parse()
.unwrap(),
number_of_results: submatches
.value_of("number_of_results")
.unwrap()
.parse()
.unwrap(),
number_of_excerpts: submatches
.value_of("number_of_excerpts")
.unwrap()
.parse()
.unwrap(),
};

let results = lib::search(&index, query, &search_config).unwrap();

let search_time = Instant::now();

Expand Down
13 changes: 11 additions & 2 deletions stork-lib/Cargo.toml
Expand Up @@ -4,7 +4,16 @@ version = "2.0.0"
edition = "2021"

[features]
build = ['thiserror', 'toml', 'serde_json', 'smart-default', 'srtparse', 'frontmatter', 'kuchiki', 'pulldown-cmark', 'rust-tfidf']
build = [
'thiserror',
'toml',
'serde_json',
'srtparse',
'frontmatter',
'kuchiki',
'pulldown-cmark',
'rust-tfidf'
]
build-remote-fetch = ['build', 'mime', 'reqwest']

# json-search-output = ['serde']
Expand All @@ -17,12 +26,12 @@ wasm-bindgen = "0.2.83"
rust-stemmers = "1.2.0"
itertools = "0.10.3"
unicode-segmentation = "1.8.0"
smart-default = { version = "0.6.0" }
### build deps
thiserror = { version = "1.0.29", optional = true }
toml = { version = "0.5.8", optional = true }
serde = { version = "1.0.147", features = ["derive"], optional = false }
serde_json = { version = "1.0.72", optional = true }
smart-default = { version = "0.6.0", optional = true }
srtparse = { version = "0.2.0", optional = true }
frontmatter = { version = "0.4.0", optional = true }
kuchiki = { version = "0.8.1", optional = true }
Expand Down

0 comments on commit 5084298

Please sign in to comment.