npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

sanity-plugin-enhanced-search

v2.0.0

Published

Configurable, hidden field to add (by default) unsupported field type lookup to search.

Downloads

6

Readme

Enhanced Search Sanity Plugin

This plugin enables better search primarily in Studio (ie. search for numbers, search in array values, accented character insensitive search) but can also be used with API.

It works by overriding default Publish action to set a hidden text field and populate it with all the extra data (as strings) you need for your search.

Since the resulting field is of string type, it will be added to Studio's search query by default.

Usage

sanity install sanity-plugin-enhanced-search

Add the field definition to schema

  1. In your schema.js, import the plugins schema: import ess from "part:sanity-plugin-enhanced-search/schema"
  2. Add the plugins field to your schema:
    export default createSchema({
      name: "default",
      types: schemaTypes.concat([
        ess,
        /* your schema */
      ]),
    });
  3. Add the field to your document:
    {
      type: "document",
      name: "document",
      title: "Document",
      fields: [
        {
          type: "string",
          name: "string",
          title: "String"
        },
        {
          type: "number", // By default numbers won't be searched for
          name: "number",
          title: "Number"
        },
        {
         type: "object",
         name: "object",
         title: "Object",
         fields: [
           {
             type: "string",
             name: "string",
             title: "String",
           },
           {
             type: "number", // Neither will nested numbers
             name: "number",
             title: "Number",
           },
         ],
       },
       {
         type: "array",
         name: "array",
         title: "Array",
         of: [
           {
             type: "string", // Array strings are also left out of Studio search
             name: "string",
             title: "String",
           },
           {
             type: "number",
             name: "number",
             title: "Number",
           },
         ],
       },
       {
         type: "enhanced_search_source",
         name: "enhanced_search",
         title: "Enhanced Search Source", // Not really used, but Studio warns about incomplete schema
         options: {
           // Define what fields you want to include as part of enhanced search
           // This uses JSONPath syntax relative to document root
           fields: [
             // Reference `document.number`
             "number",
             // Reference `document.object.number`
             "object.number",
             // Reference all values in `document.array`
             { path: "array[*]", format: (value) => value.join(" ") }, // You can also provide formatter
             // Reference `document.string`
             { path: "string", format: transliterate }, // Use transliteration library to turn UTF8 into ASCII
           ],
           // Optionally define how to join the strings together (defaults to \n)
           joinedBy: "\n",
           // For when you need a completely custom setup, you can use the provider option to implement your own value provider
           provider (
             // complete document
             document,
             // the fields option or an empty array
             fields
           ) {
             /* transform the document however you want to */
             return the_generated_string
           }
         },
       }
      ]
    }

By default all the values are mapped to strings via toString() and concatenated with \n.

Enjoy enhanced search capabilities in studio

Now try searching for a number (or any other of your enhanced fields) and see how it can now find documents with the given data.

You can also use this field by its name in __experimental_search to define search weights.

Usage from API

Best to use with Full-Text Search operators*:

*[_type == "document" && enhanced_search match "*1337*"]

If you use same field name for all of your documents and you want to resolve documents of any type given they match the given query, you can omit the _type clause:

*[enhanced_search match "*1337*"]

In this case, though, it makes sense to rename the field from enhanced_search to simply search and enjoy this lovely syntax:

*[search match "*1337*"]

Caveats

As with all Studio based plugins, if you were to mutate data outside the Studio, you have to set the enhanced_search field on your own.


TODO

  • [ ] Easier to follow README
  • [ ] Add example how this can be used to provide a nearly feature complete Full-Text Search implementation in Sanity
  • [ ] Add a complementary API for use with mutations made outside Studio