Giter Site home page Giter Site logo

Comments (4)

pdhoward avatar pdhoward commented on May 4, 2024 1

Thanks much for the response ... I corrected the method as you noted above it worked beautifully!
.add(doc); // pass in the object since I used the doc descriptor

Very excited about this search capability!

from flexsearch.

ts-thomas avatar ts-thomas commented on May 4, 2024

Hello sorry for the late answer. I do not fully understand your intend, but I can provide you a running version:

const FlexSearch = require("flexsearch");

// provide a document descriptor for each index
// the field "id" and at least one "field" is mandatory.

const settings = {
    "bookstore": {
        preset: "score",
        doc: {
            id: "id",
            field: ["intent", "text"]
        }
    },
    "pizzashop": {
        encode: "extra",
        tokenize: "strict",
        depth: 5,
        threshold: 5,
        doc: {
            id: "id",
            field: ["intent", "text"]
        }
    },
    "votingbooth": {
        encode: "advanced",
        tokenize: "forward",
        threshold: 5,
        doc: {
            id: "id",
            field: ["intent", "text"]
        }
    }
};

const index = {};

const add = (cat, doc) => {
    (index[cat] || (
        index[cat] = new FlexSearch(settings[cat])
    )).add(doc);
};

const search = (cat, query) => {
    return index[cat] ? index[cat].search(query) : [];
};

// provide documents which have the same structure as defined in the document descriptor above

const bookstore = [{
    id: 0,
    intent: "intent",
    text: "text"
},{
    id: 1,
    intent: "intent",
    text: "i am searching for a book"
}];

const pizzashop = [{
    id: 0,
    intent: "intent",
    text: "text"
},{
    id: 1,
    intent: "intent",
    text: "howdy"
}];

const votingbooth = [{
    id: 0,
    intent: "intent",
    text: "text"
},{
    id: 1,
    intent: "intent",
    text: "i need directions"
}];

// add a full document or an array of documents to the index

add("bookstore", bookstore);
add("pizzashop", pizzashop);
add("votingbooth", votingbooth);

// search

var result1 = search("bookstore", "i am searching for a book"); // --> [1]
var result2 = search("pizzashop", "howdy"); // --> [1]
var result3 = search("votingbooth", "i need directions"); // --> [1]

console.log(`========== FAST SEARCH TEST ==========`);
console.log(result1);
console.log(result2);
console.log(result3);

I hope it helps you.

from flexsearch.

ts-thomas avatar ts-thomas commented on May 4, 2024

You can also skip the doc descriptor and just pass one string value with index[cat].add(id, string), but this will return just ids as a result. If that fits your needs then this is noticeably faster.

from flexsearch.

ts-thomas avatar ts-thomas commented on May 4, 2024

One last thing :) the next version will provide you a "where" field in the custom search, this will replace the by-hand-category-splitting completely

from flexsearch.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.