0.0.39 • Published 3 years ago

gatsby-cdn-search-plugin v0.0.39

Weekly downloads
-
License
0BSD
Repository
-
Last release
3 years ago

Gatsby cdn search plugin

This plugin is in beta and still in work

Give us any feedback, open issues for any questions or ideas

Mongo query compatible search plugin for gatsby.

It is a no cost way to add search to your site.

Key technology is http2/http3 protocols, CDN, mongo-like query and N-GRAM search.

The plugin supports mongo-like query syntax with custom n-gram search.

Idea of this plugin is simple.

  • calculated indices in build phase of gatsby apps. (n-gram, text-lex, simple)
  • split the indices by chunk
  • create range diapason indices as "table of contents" the chunk
  • save all chunk and "table of contents" on CDN as assets
  • in runtime plugin restore indices and efficiently on-demand loaded chunk over http2 protocol
  • http2 multiplexing multiple requests over a single TCP connection.

The plugin has native support React via Hook "useCdnCursorQuery".

Also, you can trace your request with log-level.

import { useCdnCursorQuery, log } from 'gatsby-cdn-search-plugin'
log.enableAll(); // full logging 

Live demo

Kaggle dataset "Used Car Auction Prices" 500 000 row

live demo

source code on github

source of data

Plugins config

    plugins = [
    'gatsby-plugin-offline',
    {
      resolve: require.resolve("./cdn-indice-plugin"),
      options: {
        id: 'cars',
        chunkSize: 6000,
        dataChunkSize: 60,
        indices: [  
          {
              id: 'model',
              column: 'model',
              type: 'simple', 
          },
          { id: 'make', column: 'make' },
          { id: 'year', column: 'year' },
          { id: 'state', column: 'state' },
          { 
              id: 'id-state',
              column: 'state',
              algoritm: 'english',
              type: 'text-lex' 
          },
          { 
              id: 'ngram',
              type: "n-gram",
              actuationLimit: 1,
              actuationLimitAuto: false, 
              gramLen: 3, 
              toLowcase: true, 
              algoritm: 'english',
              stopWords: ["and"],
              columns: ['model', 'make', 'color'] 
          }
        ],
        idAttr: 'id',
        normalizer: ({ data }) => { 
          return data.recentCars
            .map(( {id, ...node} ) => ({ id: id.replace('Car__',''), ...node }));
        },
        graphQL: `query MyQuery { 
          recentCars(cursor: 0, limit: 500000){
              id
              color
              make
              mmr
              model
              seller
              sellingprice
              state
              transmission
              trim
              vin
              year
        }
      }`
      }
    }
    ]

Usage stateful React hook

import { useCdnCursorStatelessQuery, log } from 'gatsby-cdn-search-plugin'
log.enableAll(); // full logging 


const makeQuery = (search) => { // Different query strategy. It is depends of length search word
  if (search.length >= 4) {
    return { $ngram: search }; // n-gram 
  } else if (!!search.length) {
    return {
      $or: [
        { model: { $regex: new RegExp(`^${search}`, 'i'), }, }, // regexp by two columns
        { make: { $regex: new RegExp(`^${search}`, 'i'), }, }
      ],
    };
  } else {
    return undefined;
  }
}
const [state, dispatch] = useReducer(reducer, initialState);

const query = useMemo(() => makeQuery(state.search), [state.search]);

const {hasNext, next, fetching, all, page} = useCdnCursorStatefulQuery('cars', query, {year: 1}, 0, 30); // hook return cursor of data

const load = useMemo(() => {
    if (hasNext && !fetching) {
          next(); // load next slice of data 
    }}, [hasNext, fetching, next]);

Usage stateless React hook (more complicated)

import { useCdnCursorStatelessQuery, log } from 'gatsby-cdn-search-plugin'
log.enableAll(); // full logging 

const initialState = {
    loading: false,
    search: '',
    list: [],
    page: 0,
};

function reducer(state, action) {
    switch (action.type) {
        case 'pageUp':
            return { ...state, page: state.page + 1 }
        case 'type':
            return { ...state, search: action.value }
        case 'loading':
            return { ...state, loading: true }
        case 'load':
            return { ...state, loading: false, list: action.list, page: 0 };
        case 'indice':
            return { ...state, indice: action.value }
        case 'loadMore':
            return { ...state, loading: false, list: [...state.list, ...action.list] };
        default:
            throw new Error();
    }
}

const makeQuery = (search) => { // Different query strategy. It is depends of length search word
  if (search.length >= 4) {
    return { $ngram: search, year: { $lte: 2014 } }; // n-gram and  year <= 2014
  } else if (!!search.length) {
    return {
      $or: [
        { model: { $regex: new RegExp(`^${search}`, 'i'), }, }, // regexp by two columns
        { make: { $regex: new RegExp(`^${search}`, 'i'), }, }
      ],
    };
  } else {
    return { year: { $lte: 2014 } }; // only date predicate
  }
}
const [state, dispatch] = useReducer(reducer, initialState);

const query = useMemo(() => makeQuery(state.search), [state.search]);

const cursor = useCdnCursorQuery('cars', query, {year: 1}, 0, 30); // hook return cursor of data

  useEffect(() => {
    (async () => {
        let list = await cursor.next(); // load first slice of data
        dispatch({ type: 'load', list });
    })();
  }, [state.search, cursor])

  useEffect(() => {
    (async () => {
      if (await cursor.hasNext()) {
        let list = await cursor.next(); // load next slice of data 
        dispatch({ type: 'loadMore', list })
      }
    })()
  }, [state.page]);

Usage find api exactly

      import { restoreDb } from 'gatsby-cdn-search-plugin'

      const db = await restoreDb('cars');
      let result;
      if (search.length >= 4) {
        result = await db.find({ $ngram: search, year: { $gte: 2014 } }, undefined, 0, offset);
      } else if (!!search.length) {
        result = await db.find({ model: { $regex: new RegExp(`^${search}`, 'i'), }, year: { $gte: 2014 } }, undefined, 0, offset);
      } else {
        result = await db.find({ year: { $gte: 2014 } }, undefined, 0, offset);
      }

Usage cursor api exactly

      import { restoreDb } from 'gatsby-cdn-search-plugin'

      let cursor;
      const searchFetch = async (search, skip = 0, limit = 30) => {
        const db = await restoreDb('cars');
        if(cursor){
          cursor.finish();
        }
        if (search.length >= 4) {
          cursor =  db.cursor({ $ngram: search, year: { $gte: 2014 } }, undefined, skip, limit);
        } else if (!!search.length) {
          cursor =  db.cursor({
            $or: [
              { model: { $regex: new RegExp(`^${search}`, 'i'), }, },
              { make: { $regex: new RegExp(`^${search}`, 'i'), }, }
            ],
          }, undefined, skip, limit);
        } else {
          cursor = db.cursor({ year: { $gte: 2014 } }, undefined, skip, limit);
        }
        return await cursor.next();
      }

Plugin options

Options nameTypeRequiredDefault valueDescription
idStringTrueNoneUnique id database collection. The first parameter in React Hook useCdnCursorQuery.
chunkSizeNumberFalse500Indices chunk size. This affects the number of indices files. You should select this parameters depends of size of your collection
dataChunkSizeNumberFalse25Data chunk size. This affects the number of data files.
idAttrStringTrueNonePrimary id attribute name.
normalizerFunction({data: any}): Row[]TrueNoneIt is callback for handle data from graphQl
graphQLgraphQL stringTrueNoneGraphql query for fetching data
indicesArray<Union<NgramIndicesOption ,TextLexIndicesOption ,SimpleIndicesOption>>TrueNoneSecondary indices. Add column available to search.

NgramIndicesOption

Options nameTypeRequiredDefault valueDescription
type"n-gram"TruesimpleIndices using the N-Gram algorithm for search with typos. Also, support "The Porter Stemming Algorithm" for zipping indices. You can search by this column with specific not Mongo predicate. {$ngram: "search"}
idStringTrueNoneUnique id of indices. Uses as operator name in the search. Query example for id "ngram" is {$ngram: "search"}
actuationLimitNumberTrueNoneMinimum match n-gram in search. color -> col, olo, lor
actuationLimitAutoBooleanFalseFalseIf option equal true option actuationLimit doesn't work. Actuation limit n-gram calculates auto by the size of the search word.
gramLen: 3NumberFalse3Size of. Example if n-gram equal 3 "color" was split to "col", "olo", "lor"
toLowcaseBooleanFalseFalseCase sensitive search
stemStringFalseNonePreprocess indices with "The Porter Stemming Algorithm". Available values 'english', 'russian', ...
columnsString[]TrueNoneIndexing columns
stopWordsString[]FalseNoneThe words exclude for search

SimpleIndicesOption

Options nameTypeRequiredDefault valueDescription
type"text-lex"TruesimpleThe Porter Stemming Algorithm. You can search by this column with specific not Mongo predicate. {$lex: "search"}
idStringTrueNoneUnique id of indices. Uses as operator name in the search. Query example for id "ngram" is {$lex: "search"}
algoritmStringFalseNonePreprocess indices with "The Porter Stemming Algorithm". Available values 'english', 'russian', ...
columnStringTrueNoneIndexing column

TextLexIndicesOption

Options nameTypeRequiredDefault valueDescription
type"simple"True"simple"Default value simple indices by one column only. You can search by this column with regular Mongo predicate lt, gt, eq, ... etc. Also, support regexp by "start with regexp"
idStringTrueNoneUnique id of indices. Uses as operator name in the search. Query example for id "ngram" is {$lex: "search"}
columnStringTrueNoneIndexing column
0.0.39

3 years ago

0.0.39-rc3

3 years ago

0.0.39-rc2

3 years ago

0.0.39-rc1

3 years ago

0.0.39-rc7

3 years ago

0.0.39-rc6

3 years ago

0.0.39-rc4

3 years ago

0.0.36-rc1

3 years ago

0.0.36-rc4

3 years ago

0.0.36-rc3

3 years ago

0.0.36-rc5

3 years ago

0.0.38

3 years ago

0.0.37

3 years ago

0.0.35

3 years ago

0.0.35-rc0

3 years ago

0.0.34

3 years ago

0.0.34-rc6

3 years ago

0.0.34-rc5

3 years ago

0.0.34-rc7

3 years ago

0.0.34-rc2

3 years ago

0.0.34-rc1

3 years ago

0.0.34-rc4

3 years ago

0.0.34-rc3

3 years ago

0.0.33

3 years ago

0.0.30

3 years ago

0.0.32

3 years ago

0.0.26

3 years ago

0.0.27

3 years ago

0.0.28

3 years ago

0.0.29

3 years ago

0.0.25

3 years ago

0.0.24

3 years ago

0.0.22

3 years ago

0.0.20

3 years ago

0.0.21

3 years ago

0.0.19

3 years ago

0.0.17

3 years ago

0.0.18

3 years ago

0.0.16

3 years ago

0.0.13

3 years ago

0.0.14

3 years ago

0.0.15

3 years ago

0.0.12

3 years ago

0.0.11

3 years ago

0.0.9

3 years ago

0.0.8

3 years ago

0.0.7

3 years ago

0.0.6

3 years ago

0.0.5

3 years ago

0.0.4

3 years ago

0.0.3

3 years ago

0.0.2

3 years ago