sanity-plugin-enhanced-search

2.0.0 • Public • Published

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

Package Sidebar

Install

npm i sanity-plugin-enhanced-search

Weekly Downloads

3

Version

2.0.0

License

MIT

Unpacked Size

17.4 kB

Total Files

8

Last publish

Collaborators

  • joltmode