|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const values = require('object-values'); |
| 4 | +const existy = require('existy'); |
| 5 | +const complement = require('complement'); |
| 6 | + |
| 7 | +/** |
| 8 | + * Lucene Query Builder |
| 9 | + * |
| 10 | + * Lucene query syntax docs (specs) |
| 11 | + * https://lucene.apache.org/core/2_9_4/queryparsersyntax.html#Terms |
| 12 | + */ |
| 13 | + |
| 14 | +const NAMESPACE = 'lucene'; |
| 15 | + |
| 16 | +let lucene = {}; |
| 17 | + |
| 18 | +lucene.terms = terms; |
| 19 | +lucene.term = terms; //alias |
| 20 | + |
| 21 | +lucene.field = field; |
| 22 | + |
| 23 | +lucene.or = surrounded('OR'); |
| 24 | +lucene.and = surrounded('AND'); |
| 25 | +lucene.not = surrounded('NOT'); |
| 26 | + |
| 27 | +lucene.group = surrounder('(', ')'); |
| 28 | +lucene.range = range; |
| 29 | + |
| 30 | +lucene.fuzzy = fuzzy; |
| 31 | +lucene.proximity = proximity; |
| 32 | +lucene.required = required; |
| 33 | + |
| 34 | +lucene.builder = builder; |
| 35 | + |
| 36 | +if (typeof module.exports === 'undefined') |
| 37 | + window[NAMESPACE] = lucene; |
| 38 | +else |
| 39 | + module.exports = lucene; |
| 40 | + |
| 41 | +/** |
| 42 | + * Used for defining a query builder |
| 43 | + * |
| 44 | + * @param {function} fn |
| 45 | + * @returns {function} |
| 46 | + * |
| 47 | + * @example |
| 48 | + * |
| 49 | + * userLuceneQuery = lucene.builder((data) => { |
| 50 | + * return lucene.field( |
| 51 | + * lucene.term('user-name', data.user.name) |
| 52 | + * ); |
| 53 | + * }); |
| 54 | + * |
| 55 | + * userLuceneQuery({user: {name: 'Dolly'}}) // => 'user-name: 'Dolly'' |
| 56 | + */ |
| 57 | +function builder(fn) { |
| 58 | + return (data) => fn(data); |
| 59 | +} |
| 60 | + |
| 61 | +/** |
| 62 | + * Basicly add double quotes around the string |
| 63 | + * |
| 64 | + * @param {string} words |
| 65 | + * |
| 66 | + * @returns {string} |
| 67 | + * |
| 68 | + * @todo make sure lucene query characters are escaped with a \ |
| 69 | + */ |
| 70 | +function terms(words) { |
| 71 | + return `"${words}"`; |
| 72 | +} |
| 73 | + |
| 74 | +/** |
| 75 | + * @param {string} field |
| 76 | + * @param {string} query |
| 77 | + * |
| 78 | + * @returns {string} the format is <field>: <term(s)> |
| 79 | + * |
| 80 | + * @example |
| 81 | + * lucene.field('prop-name', lucene.term('value')) // => 'prop-name: "value"' |
| 82 | + */ |
| 83 | +function field(field, query) { |
| 84 | + return `${field}: ${query}`; |
| 85 | +} |
| 86 | + |
| 87 | +/** |
| 88 | + * The value is between 0 and 1, with a value closer to 1 only terms with a |
| 89 | + * higher similarity will be matched. |
| 90 | + * |
| 91 | + * Fuzzy search can only be applied to a single term |
| 92 | + * |
| 93 | + * @param {string} str |
| 94 | + * @param {number} [similarity=undefined] |
| 95 | + * |
| 96 | + */ |
| 97 | +function fuzzy(termStr, similarity) { |
| 98 | + |
| 99 | + if ((complement(existy)(similarity))) |
| 100 | + return `${termStr}~`; |
| 101 | + |
| 102 | + let isValidSimilarity = within(0,1); |
| 103 | + |
| 104 | + if (!isValidSimilarity(similarity)) |
| 105 | + throw new RangeError('Similarity must be between 0 and 1. It was ' + similarity); |
| 106 | + |
| 107 | + return `${termStr}~${similarity}`; |
| 108 | +} |
| 109 | + |
| 110 | +/** |
| 111 | + * Lucene supports finding words are a within a specific distance away. |
| 112 | + * |
| 113 | + * @param {string} first |
| 114 | + * @param {string} second |
| 115 | + * |
| 116 | + * @param {number} distance |
| 117 | + * |
| 118 | + * @returns {string} |
| 119 | + */ |
| 120 | +function proximity(first, second, distance) { |
| 121 | + return `"${first} ${second}"~${distance}`; |
| 122 | +} |
| 123 | + |
| 124 | +/** |
| 125 | + * Range Queries allow one to match documents whose field(s) values are |
| 126 | + * between the lower and upper bound specified by the Range Query. Range |
| 127 | + * Queries can be inclusive or exclusive of the upper and lower bounds. |
| 128 | + * |
| 129 | + * @param {string} |
| 130 | + * @param {string} |
| 131 | + * @param {boolean} [includeLeft=true] |
| 132 | + * @param {boolean} [includeRight=true] |
| 133 | + * |
| 134 | + * @returns {string} |
| 135 | + */ |
| 136 | +function range(from, to, includeLeft, includeRight) { |
| 137 | + return surrounder( |
| 138 | + includeLeft ? '[' : '{', |
| 139 | + includeRight ? ']' : '}' |
| 140 | + )(`${from} TO ${to}`); |
| 141 | +} |
| 142 | + |
| 143 | +//banana banana banana |
| 144 | +function required(termStr) { |
| 145 | + return `+${termStr}`; |
| 146 | +} |
| 147 | + |
| 148 | +/** |
| 149 | + * Higher order function for building strings that always have the same string |
| 150 | + * between two other strings |
| 151 | + * |
| 152 | + * @param {string} patty |
| 153 | + * |
| 154 | + * @returns {string} |
| 155 | + * |
| 156 | + * @example |
| 157 | + * surrounded('OR')('hello', 'world') // -> "hello OR world" |
| 158 | + */ |
| 159 | +function surrounded(middle) { |
| 160 | + return function(){ |
| 161 | + return values(arguments).join(` ${middle} `); |
| 162 | + }; |
| 163 | +} |
| 164 | + |
| 165 | +/** |
| 166 | + * Higher order function for building strings that are surrounderd on both |
| 167 | + * sides of a string |
| 168 | + * |
| 169 | + * @param {string} open |
| 170 | + * @param {string} close |
| 171 | + * |
| 172 | + * @returns {function} |
| 173 | + */ |
| 174 | +function surrounder(open, close){ |
| 175 | + return function(){ |
| 176 | + let middle = values(arguments).join(' '); |
| 177 | + return `${open} ${middle} ${close}`; |
| 178 | + }; |
| 179 | +} |
| 180 | + |
| 181 | +/** |
| 182 | + * @param {number} left |
| 183 | + * @param {number} right |
| 184 | + * |
| 185 | + * @returns {function} |
| 186 | + * |
| 187 | + * @example |
| 188 | + * |
| 189 | + * within(0,1)(0.5) // => true |
| 190 | + * |
| 191 | + * within(100, 300)(40) // => false |
| 192 | + */ |
| 193 | +function within(left, right) { |
| 194 | + return function within(value) { |
| 195 | + return ((value >= left) && (value <= right)); |
| 196 | + }; |
| 197 | +} |
0 commit comments