Skip to content

Commit

Permalink
Add sisi search --max flag
Browse files Browse the repository at this point in the history
  • Loading branch information
zcbenz committed Sep 15, 2024
1 parent 26d36e7 commit bfa3165
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ CLI:
sisi remove-index <target>
Remove index for all items under target directory.

sisi search [--in #0] <query>
sisi search [--in #0] [--max #0] [--print] <query>
Search the query string from indexed images.
```

Expand Down
14 changes: 10 additions & 4 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,14 @@ export class SearchCommand extends Command {

query = Option.String();
target = Option.String('--in', {description: 'The directory where images are searched.'});
max = Option.String('--max', 20, {description: 'The maximum number of results to return.'});
print = Option.Boolean('--print', {description: 'Print the results to stdout.'});

async execute() {
const results = await search(this.query, this.target);
const results = await search(this.query, {
maxResults: parseInt(this.max),
targetDir: this.target,
});
if (!results) {
const target = this.target ?? '<target>'
console.error(`No images in index, please run "sisi index ${target}" first.`);
Expand All @@ -64,10 +68,12 @@ export class SearchCommand extends Command {
console.error('There is no matching images');
return;
}
if (this.print)
if (this.print) {
console.log(results.map(r => `${shortPath(r.filePath)}\n${r.score.toFixed(2)}`).join('\n'));
else
presentResults(this.query, results);
return;
}
console.log('Showing results in your browser...');
presentResults(this.query, results);
}
}

Expand Down
7 changes: 5 additions & 2 deletions src/sisi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,11 @@ export async function index(targetDir: string) {
/**
* Search the query string from index.
*/
export async function search(query: string, targetDir?: string): Promise<SearchResult[] | undefined> {
interface SearchOptions {
maxResults: number;
targetDir?: string;
}
export async function search(query: string, {maxResults, targetDir}: SearchOptions): Promise<SearchResult[] | undefined> {
if (targetDir)
targetDir = path.resolve(targetDir);
// List all the embeddings of images under targetDir from the index.
Expand Down Expand Up @@ -118,7 +122,6 @@ export async function search(query: string, targetDir?: string): Promise<SearchR
// Get the indices sorted by higher scores.
const topIndices = mx.argsort(scores).index(mx.Slice(null, null, -1));
// Settings for the results, should probably be made options.
const maxResults = 20;
const goodScore = isTextQuery ? 0.2 : 0.75;
let bottomLineScore = isTextQuery ? 0.16 : 0.6;
// Prepare the results.
Expand Down

0 comments on commit bfa3165

Please sign in to comment.