2
votes

What is your recipe for faceted search in Node.js?

In my previous, Drupal-based site all data was stored in Mysql database. During cron calls all new/modified/deleted nodes were synchronized with Solr which provided nice faceted search in quick manner.

How to do the same in Node.js? By "the same" I mean nice and fast faceted search.

Initially I was thinking about MongoDB with manual synchronization with Elasticsearch. Personally I would prefer to avoid Sphinx (it's not a definitive decision).

2

2 Answers

0
votes

You can use in-memory-faceted-search npm package to scan an array of objects and generate a faceted search index with all possible levels and combinations.

Example:

const data = require("./sample.json")
const facetedSearch = require("in-memory-faceted-search")
 
const facets = [
  { name: "Colors", field: "color" },
  { name: "Sizes", field: "size" },
  { name: "Status", field: "active", map: (item) => (item.active ? "Active" : "Disabled") },
  { name: "Ages", field: "age", map: (item) => (item.age > 25 ? "Over 25" : "Under 25") }
]
 
const tree = facetedSearch({ data, facets })
console.log(JSON.stringify(tree, null, 4))

Output:

{
    "count": 100,
    "Colors": {
        "red": {
            "count": 27
        },
        "blue": {
            "count": 25
        },
        "green": {
            "count": 23
        },
        "brown": {
            "count": 25
        }
    },
    "Sizes": {
        "big": {
            "count": 29
        },
        "small": {
            "count": 34
        },
        "medium": {
            "count": 37
        }
    },
    "Status": {
        "Disabled": {
            "count": 56
        },
        "Active": {
            "count": 44
        }
    },
    "Ages": {
        "Over 25": {
            "count": 72
        },
        "Under 25": {
            "count": 28
        }
    }
}

Check the README file to get more examples and deeper levels

-1
votes

Node.js is completely irrelevant here. What you need is a datastore and/or indexing technology that can do faceted search. Hooking any number of those up with Node.js is just plumbing. Why not just go with MySQL if that works for you?