Home

Awesome

![](http://i.imgur.com/RIvu9.png | width=50)

<img src="http://lucene.apache.org/solr/assets/identity/Solr_Logo_on_white.png"width="100"/> sails-solr

Provides easy access to solr from Sails.js & Waterline.

This module is a Sails/Waterline adapter, an early implementation of a rapidly-developing, tool-agnostic data standard. Its goal is to provide a set of declarative interfaces, conventions, and best-practices for integrating with all sorts of data sources.

The main goal is a simple usage and integration of a full managed Solr.

Build Status Coverage Status Dependency Status

NPM

Features

Installation

To install this adapter, run:

$ npm install sails-solr

Getting started with sails-solr

To install/start solr if you not have one running

make kickstart

Note: not recommended for production systems! Solr installation Tomcat for more

Configuring Connections

Add the solr configuration to the config/connections.js file. The basic options are as follows:

module.exports.connections = {
  solrConnectionOne: {
    module : 'sails-solr',
    host: 'localhost',
    port: 8983,
    core: 'schemaless',
    schema: true,
    migrate: 'drop'
  }
};

Note: you can define multiple solr connections/cores. By default sails-solr will run multiple models inside one core manageCores. Connection Options

Configuring Models

And then change default model configuration to the config/models.js:

module.exports.models = {
  connection: 'solrConnectionOne',
  attributes: {
    name:'string'
    ...
  }
};

Note: you can add more model based configuartion Model Options / Connection Options

Usage

create a user:

  User.create({name:'foo'},console)

find a user:

  User.find({name:'foo'},console);
  User.findOne({name:'foo'},console);
  User.findByName('foo',console);

Note: See Waterline Documentation Query Language and Query Methods

Special Adapter Interfaces

Autocompleter

search suggestion and spellchecked phrase. Known as "Did You Mean: foo?"

  // as sails request  see hooks and blueprint
  // http://localhost:1337/user/suggest/foa

  // in node
  User.suggest('foa', console);

  //response
  {
  "responseHeader": {
    "status": 0,
    "QTime": 1
  },
  "spellcheck": {
    "suggestions": [
      "foa",
      {
        "numFound": 1,
        "startOffset": 0,
        "endOffset": 9,
        "origFreq": 0,
        "suggestion": [
          {
            "word": "foo",
            "freq": 1
          }
        ]
      }
    ],
    "correctlySpelled": false,
    "collations": [
      "collation",
      "foo"
    ]
  },
  "suggest": {
    "suggest": {
      "foa": {
        "numFound": 2,
        "suggestions": [
          {
            "term": "foo",
            "weight": 0,
            "payload": ""
          },{
            "term": "foo bar",
            "weight": 0,
            "payload": ""
          }
        ]
      }
    }
  }
}

Layerd Navigation

Well known as filter. facet for strings and min,max,avg for ìnteger to build Options and Range Slider Elemets. Query Methods

  // as sails request  see hooks and blueprint
  // http://localhost:1337/user/catalog/?name="*"&limit=3&sort=age asc&skip=0

  // in node
  User.catalog({name:'foo'},console);

  //response
  {
  "responseHeader":{
    "status":0,
    "QTime":1,
    "params":{
      "q":"*:*",
      "indent":"true",
      "stats":"true",
      "sort":"percent asc",
      "rows":"100",
      "wt":"json",
      "stats.field":["age",
        "percent"]}},
  "response":{"numFound":13,"start":0,"docs":[
      {
        "name":"foo",
        "age":10,
        "color":"blue",
        "createdAt":"2015-12-30T23:32:24.755Z",
        "updatedAt":"2015-12-30T23:32:24.755Z",
        "id":"612bb75f-be0f-496b-ba51-8e79ee786c50"},
      {
        "name":"bar",
        "age":20,
        "color":"yellow",
        "createdAt":"2015-12-30T23:15:09.859Z",
        "updatedAt":"2015-12-30T23:15:09.859Z",
        "id":"517a4917-b3b8-4ea0-a3fd-acd41497b6e0"},
      {
        "name":"john",
        "age":30,
        "color":"black",
        "createdAt":"2015-12-30T23:15:10.859Z",
        "updatedAt":"2015-12-30T23:15:10.859Z",
        "id":"515a4917-b3b8-4ea0-a3fd-acd4149432fd"},
  },
  "facet_counts": {
    "facet_queries": {},
    "facet_fields": {
        "name": [
        {
          "blue":1},
        {
          "yellow":1},
        {
          "black":1},
      ]
    },
    "facet_dates": {},
    "facet_ranges": {},
    "facet_intervals": {},
    "facet_heatmaps": {}
  },
  "stats":{
    "stats_fields":{
      "age":{
        "min":10.0,
        "max":30.0,
        "count":3,
        "missing":0,
        "sum":60.0,
        "sumOfSquares":100,
        "mean":20,
        "stddev":10}}}}

Supported Waterline Interfaces

TypeMethodsBuild
Semanticcreate, createEach, find, count, update, destroyBuild Status
Migratabledefine, describe, drop, alter, addAttributes, remove, attributes, addIndex, removeIndexBuild Status
Queryablewhere, limit, sort, skip, selectBuild Status

Note: See Waterline Documentation

Special Adapter Interfaces

TypeMethodsBuild
Suggestsuggest. Return on Object with suggestions and spellecked the requestet term or phraseBuild Status
Catalogcatalog. Return an Object with matching results and Layered Navigation as facet and statsBuild Status

Advanced Configuration

Connection Options

ParamsDefaultDescription
host'localhost'
port'8983'
core'schemaless'
solrconfigobjectset properties, add and update searchcomponent and requesthandler. Config API
manageCorestruecreate cores if not exists CoreAdmin
schematrueallow migrate drop, alter schema manage schema
singlefalseforce manageCores to create a core for each model
fieldTypeMapfieldTypesField Type Map
useFqParamtrueforce query mapping as fq=name:foo param
schemaDefaultFieldTypes{}
debugAdapterfalse
debugCollectionfalse
debugQueryfalse
debugSolrfalse

Model Options

{
  attributes: {
    first_name: {
      type:'string'
      // Overwrite per Field
      schemaDefaultFieldAttributes: {
        indexed: true,
        type: 'text_de'
      }
    }
  }
  // Overwrite per Model
  schemaDefaultFieldAttributes: {
    indexed: false
  }
}

Field Type Map

The following table represents mappings between Sails/Waterline model data types and Solr field types:

Sails/Waterline TypeSolr Type
stringtext_general
texttext_general
binarytext_general
integerint
floatfloat
datedate
timedate
datetimedate
booleanboolean
binarytext_general
arraytext_general
jsontext_general
pointpoint

Note: You can even define your custom mapping as fieldTypeMap: inside connection settings and as model option. If you want a field type explicit mapping use fieldType as additional fieldTypeMapattribute

Solr default field attributes

The following table represents Solr field attributes:

Solr Field AttributesDefault
namenewField
typetext_general
indexedtrue
storedtrue
docValuesfalse
sortMissingFirstfalse
sortMissingLastfalse
multiValuedfalse
omitNormstrue
omitTermFreqAndPositionsfalse
omitPositionsfalse
termVectorstrue
termPositionsfalse
termOffsetsfalse
termPayloadsfalse
requiredfalse
dynamicFieldfalse
jsontext_general

Note: You can even define your custom field attribute default as schemaDefaultFieldAttributes: inside connection settings and as model option. If you want a field attribute explicit you can add this attribute as an additional option inside the Model attribute settings

Running the tests

$ npm test

TODO:

More Resources

License

MIT