All Projects → AlbertLucianto → Vuex Search

AlbertLucianto / Vuex Search

Licence: mit
Vuex binding for client-side search with indexers and Web Workers 📗🔍

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Vuex Search

SlideBar
SlideBar for Android 一个很好用的联系人字母快速索引
Stars: ✭ 47 (-68.03%)
Mutual labels:  search, index
Defiant.js
http://defiantjs.com
Stars: ✭ 907 (+517.01%)
Mutual labels:  search, web-worker
Scout Extended
Scout Extended: The Full Power of Algolia in Laravel
Stars: ✭ 330 (+124.49%)
Mutual labels:  search, index
Elastix
A simple Elasticsearch REST client written in Elixir.
Stars: ✭ 231 (+57.14%)
Mutual labels:  search, index
Node Sonic Channel
🦉 Sonic Channel integration for Node. Used in pair with Sonic, the fast, lightweight and schema-less search backend.
Stars: ✭ 101 (-31.29%)
Mutual labels:  search, index
Cassandra Lucene Index
Lucene based secondary indexes for Cassandra
Stars: ✭ 584 (+297.28%)
Mutual labels:  search, index
Riot
Go Open Source, Distributed, Simple and efficient Search Engine; Warning: This is V1 and beta version, because of big memory consume, and the V2 will be rewrite all code.
Stars: ✭ 6,025 (+3998.64%)
Mutual labels:  search, index
Blast
Blast is a full text search and indexing server, written in Go, built on top of Bleve.
Stars: ✭ 934 (+535.37%)
Mutual labels:  search, index
Torrenter
Simple nodejs package to download torrents using torrent-indexer and webtorrent, especially movie and series.
Stars: ✭ 42 (-71.43%)
Mutual labels:  search, index
Flexsearch
Next-Generation full text search library for Browser and Node.js
Stars: ✭ 8,108 (+5415.65%)
Mutual labels:  search, web-worker
Lucenenet
Apache Lucene.NET
Stars: ✭ 1,704 (+1059.18%)
Mutual labels:  search, index
Dfi
Peer-to-peer torrent indexing
Stars: ✭ 118 (-19.73%)
Mutual labels:  search, index
Sonic
🦔 Fast, lightweight & schema-less search backend. An alternative to Elasticsearch that runs on a few MBs of RAM.
Stars: ✭ 12,347 (+8299.32%)
Mutual labels:  search, index
Js Search
JS Search is an efficient, client-side search library for JavaScript and JSON objects
Stars: ✭ 1,920 (+1206.12%)
Mutual labels:  search
Docsearch
📘 The easiest way to add search to your documentation.
Stars: ✭ 2,266 (+1441.5%)
Mutual labels:  search
Alfred Vscode
Alfred 3 workflow that allows you to browse and open Visual Studio Code projects or simply open specified folders/files
Stars: ✭ 141 (-4.08%)
Mutual labels:  search
Vue Cart
💵 A shop cart made with vue
Stars: ✭ 140 (-4.76%)
Mutual labels:  vuex
Vue Cli Multi Page
基于vue-cli模板的多页面多路由项目,一个PC端页面入口,一个移动端页面入口,且有各自的路由, vue+webpack+vue-router+vuex+mock+axios
Stars: ✭ 145 (-1.36%)
Mutual labels:  vuex
Rummage phoenix
Full Phoenix Support for Rummage. It can be used for searching, sorting and paginating collections in phoenix.
Stars: ✭ 144 (-2.04%)
Mutual labels:  search
Awesome Deep Learning Papers For Search Recommendation Advertising
Awesome Deep Learning papers for industrial Search, Recommendation and Advertising. They focus on Embedding, Matching, Ranking (CTR prediction, CVR prediction), Post Ranking, Transfer, Reinforcement Learning, Self-supervised Learning and so on.
Stars: ✭ 136 (-7.48%)
Mutual labels:  search

Vuex Search

Coverage Status Build Status Downloads Downloads Version License

Vuex Search is a plugin for searching collections of objects. Search algorithms powered by js-worker-search.

Vuex Search

See working example here.

Installation:

npm install --save vuex-search
# or
yarn add vuex-search

Overview

vuex-search searches collections of documents and returns results as an Array of document ids. It is important to note that the documents themselves aren't returned. This is because the actual search is performed in a web-worker thread for performance reasons. In order to avoid serializing the documents and passing them back and forth, vuex-search simply passes their ids.

Because of this, each document must contain an id attribute.

Please note that vuex-search depends on regenerator runtime, you need to either include transform-runtime plugin in your babel config,

{
  "plugins": [
    "transform-runtime"
  ]
}

or add babel-polyfill in your entries (assuming you are using webpack). For example

module.export = {
  entries: ['babel-polyfill', './src']
}

Examples

// store/state.js

export default {
  myResources: {
    contacts: [
      {
        // id is required for each record
        id: '1',
        address: '1 Hacker Way, Menlo Park',
        name: 'Dr. Katrina Stehr',
      },
      {
        id: '2',
        address: '06176 Georgiana Points',
        name: 'Edyth Grimes',
      },
    ],
  },
}

Vuex Search plugin

searchPlugin(options)

  • options: List of options for defining the plugin. Available options are:

    • resources: { [resourceName]: IndexOptions }

      Dictionary of resourceName and their index options. See IndexOptions.

    • [searchApi]: SearchApi

      If provided, it will be used as default searchApi across resources. See customizing search index. Default: new SearchApi()

// store/index.js

import Vue from 'vue';
import Vuex from 'vuex';
import searchPlugin from 'vuex-search';
import state from './state';

Vue.use(Vuex);

const store = new Vuex.Store({
  state,
  plugins: [
    searchPlugin({
      resources: {
        contacts: {
          // what fields to index
          index: ['address', 'name'],
          // access the state to be watched by Vuex Search
          getter: state => state.myResources.contacts,
          // how resource should be watched
          watch: { delay: 500 },
        },
        // otherResource: { index, getter, watch, searchApi },
      },
    }),
  ],
});

IndexOptions

  • index: Array<string>

    List of fields to be indexed.

  • getter: (state) => Array<object>|object

    Getter function to access the resource from root state and to watch.

  • [watch]: boolean|WatchOptions

    Whether needs to or delay reindex if resource changes. This option is useful to avoid reindex overhead when the resource frequently changes. Reindexing can be done by mapping action reindex.

    WatchOptions

    • [delay]: number

      If provided, reindex will be debounced with specified delay.

    Default: true

  • [searchApi]: SearchApi

    Custom search index. If defined, it is used instead of the shared searchApi instance.

Binding with Vue Component

import {
  mapActions as mapSearchActions,
  mapGetters as mapSearchGetters,
  getterTypes,
  actionTypes,
} from 'vuex-search';
// SomeComponent.vue

data() {
  return { text: '' },
},

computed: {
  ...mapSearchGetters('contacts', {
    resultIds: getterTypes.result,
    isLoading: getterTypes.isSearching,
  }),
},

methods: {
  ...mapSearchActions('contacts', {
    searchContacts: actionTypes.search,
  }),
  doSearch() {
    this.searchContacts(this.text);
  },
},

mapGetters(resourceName, getterMap)

Similar to Vuex helper for mapping attributes, getterMap can be either an object or an array.

mapActions(resourceName, actionMap)

Similar to Vuex helper for mapping attributes, actionMap can be either an object or an array.

getterTypes

  • result

    Mapped state is an array of ids.

  • isSearching

    Mapped state indicates whether searchApi has resolved its promise of search result.

  • resourceIndex

    Full state of resource index: result, isSearching, and current search text.

actionTypes

  • search

    Mapped action's function signature: (query: string) => void.

  • reindex

    Mapped action's function signature: () => void. To be used when option watch is false. This action will reindex the resource and automatically re-search current text.

  • registerResource

    Mapped action's function signature: (options: IndexOptions) => void. This action will dynamically add resourceName with options provided. See IndexOptions.

    More about Dynamic Index Registration.

  • unregisterResource

    Mapped action's function signature: () => void. This action will unwatch and remove resourceName index.

Customizing Search Index

By default, vuex-search builds an index to match all substrings. You can override this behavior by providing your own, pre-configured searchApi param to the plugin like so:

import searchPlugin, { SearchApi, INDEX_MODES } from 'vuex-search';

// all-substrings match by default; same as current
// eg 'c', 'ca', 'a', 'at', 'cat' match 'cat'
const allSubstringsSearchApi = new SearchApi();

// prefix matching (eg 'c', 'ca', 'cat' match 'cat')
const prefixSearchApi = new SearchApi({
  indexMode: INDEX_MODES.PREFIXES,
});

// exact words matching (eg only 'cat' matches 'cat')
const exactWordsSearchApi = new SearchApi({
  indexMode: INDEX_MODES.EXACT_WORDS,
});

const store = new Vuex.Store({
  state,
  plugins: [
    searchPlugin({
      resources: {
        contacts: {
          index: ['address', 'name'],
          getter: state => state.myResources.contacts,
        },
      },
      searchApi: exactWordsSearchApi, // or allSubstringSearchApi; or prefixSearchApi
    }),
  ],
});

Custom word boundaries (tokenization) and case-sensitivity

You can also pass parameters to the SearchApi constructor that customize the way the search splits up the text into words (tokenizes) and change the search from the default case-insensitive to case-sensitive:

import searchPlugin, { SearchApi } from 'vuex-search';

const store = new Vuex.Store({
  state,
  plugins: [
    searchPlugin({
      resources: {
        contacts: {
          index: ['address', 'name'],
          getter: state => state.myResources.contacts,
        },
      },
      searchApi: new SearchApi({
        // split on all non-alphanumeric characters,
        // so this/that gets split to ['this','that'], for example
        tokenizePattern: /[^a-z0-9]+/,
        // make the search case-sensitive
        caseSensitive: true,
      }),
    }),
  ],
});

Dynamic Index Registration

When a module needs to be loaded or registered dynamically, statically defined plugin can be a problem. The solution is to use vuex-search dynamic index registration.

VuexSearch instance can be accessed through search attribute of store. Thus, in a Vue instance it is accessed through this.$store.search. Available methods are:

registerResource(resourceName, options: IndexOptions)

Note that this method is slightly different from registerResource from mapActions. Calling this method needs to provide resourceName. Whereas, method from mapActions has already injected resourceName as its first argument.

unregisterResource(resourceName)

Remove outdated resource indexes, and unwatch/unsubscribe any watchers/subscriptions related to resourceName.

Changing Base

By default, vuex-search will register its module in 'vuexSearch/' from root state. To avoid possible clash naming, you can change its base name before defining the plugin in the store through

import { VuexSearch } from 'vuex-search';

VuexSearch.base = 'vuexSearchNew';

const store = new Vuex.Store({
  // ... store options
});

Changelog

Changes are tracked in the changelog.

License

vuex-search is available under the MIT License.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].