|
| 1 | +# Lucene Query String Builder |
| 2 | + |
| 3 | +[](https://www.npmjs.com/package/lucene-query-string-builder) |
| 4 | +[](https://www.npmjs.com/package/lucene-query-string-builder) |
| 5 | +[](https://libraries.io/npm/lucene-query-string-builder) |
| 6 | +[](https://standardjs.com) |
| 7 | + |
| 8 | +## Notice |
| 9 | + |
| 10 | +*Lucene Query String Builder* is looking for a developer to help with the API. |
| 11 | +I'll continue performing the maintenance tasks. |
| 12 | + |
| 13 | +Easily build your lucene string queries using small and pure functions. |
| 14 | + |
| 15 | +Imagine having an API that leverages lucene for performing queries on the |
| 16 | +(indexed) database. In that case you might want to generate lucene query strings on |
| 17 | +the client/front end. |
| 18 | + |
| 19 | +The usage section shows how you can leverage this lib for your purposes. |
| 20 | + |
| 21 | +## Setup |
| 22 | + |
| 23 | +```bash |
| 24 | +npm install lucene-query-string-builder --save |
| 25 | +``` |
| 26 | + |
| 27 | +## Features |
| 28 | + |
| 29 | +- escapes lucene special chars when creating a term string |
| 30 | +- contains all the operators lucene uses |
| 31 | +- simple lucene.builder function for defining a lucene query builder |
| 32 | + |
| 33 | +## Usage |
| 34 | + |
| 35 | +Let's see how you can use lucene query string builder to define lucene query |
| 36 | +strings with simple JavaScript functions. |
| 37 | + |
| 38 | +Assuming that the lucene global variable contains the lucene functions. This |
| 39 | +would be the default when loaded into a browser. |
| 40 | + |
| 41 | +```JavaScript |
| 42 | + |
| 43 | +var findUserLuceneQueryString = lucene.builder(function(data){ |
| 44 | + |
| 45 | + // just to make the example more readable; |
| 46 | + var _ = lucene; |
| 47 | + |
| 48 | + return _.group(_.and( |
| 49 | + _.field('eye-color', _.term(data.eye.color)), |
| 50 | + _.field('age', _.range(data.age.min, data.age.max)) |
| 51 | + )); |
| 52 | + |
| 53 | +}); |
| 54 | + |
| 55 | +var luceneQueryString = findUserLuceneQueryString({ |
| 56 | + eye: { color: 'brown'}, |
| 57 | + age: { |
| 58 | + min: 10, |
| 59 | + max: 20 |
| 60 | + } |
| 61 | +}); |
| 62 | + |
| 63 | +luceneQueryString === '( eye-color: "brown" AND age:{ 10 TO 20 } )' // => true |
| 64 | + |
| 65 | +``` |
| 66 | +The functions are based on the lucene specifications found here: |
| 67 | +https://lucene.apache.org/core/2_9_4/queryparsersyntax.html#Terms |
| 68 | + |
| 69 | +```JavaScript |
| 70 | + |
| 71 | + var _ = lucene; |
| 72 | + |
| 73 | + /*** |
| 74 | + * terms or term |
| 75 | + */ |
| 76 | + |
| 77 | + _.term('hello'); // => '"hello"' |
| 78 | + |
| 79 | + _.terms('hello world'); // => '"hello world"' |
| 80 | + |
| 81 | + |
| 82 | + /*** |
| 83 | + * field |
| 84 | + */ |
| 85 | + |
| 86 | + _.field('hello', _.term('world')); // => 'hello: "world"' |
| 87 | + |
| 88 | + |
| 89 | + /*** |
| 90 | + * or/and/not |
| 91 | + * |
| 92 | + * These functions are variadic and all work the same way. This example only |
| 93 | + shows the or but ot works similar with and and not |
| 94 | + */ |
| 95 | + |
| 96 | + _.or(_.term('hello'), _.term('world')); // => '"hello" OR "world"' |
| 97 | + |
| 98 | + _.or(_.term('hello'), _.term('you'), _.term('world')); // => '"hello" OR "you" OR "world"' |
| 99 | + |
| 100 | + |
| 101 | + /*** |
| 102 | + * group |
| 103 | + * |
| 104 | + * Is a variadic function too |
| 105 | + */ |
| 106 | + |
| 107 | + _.group(_.term('hello'), _.term('you'), _.term('world')); // => '( "hello" "you" "world" )' |
| 108 | + |
| 109 | + |
| 110 | + /*** |
| 111 | + * range |
| 112 | + * |
| 113 | + * Takes two strings and 2 booleans. |
| 114 | + */ |
| 115 | + |
| 116 | + /* combined with the field function to query for ages between 10 and 20 */ |
| 117 | + _.field('age', _.range(10, 20)); // => 'age: { 10 TO 20 }' |
| 118 | + |
| 119 | + |
| 120 | + /*** |
| 121 | + * fuzzy |
| 122 | + */ |
| 123 | + |
| 124 | + _.fuzzy(_.term('hello'), 0.2); // => '"hello"~0.2' |
| 125 | + |
| 126 | + |
| 127 | + /*** |
| 128 | + * proximity |
| 129 | + */ |
| 130 | + |
| 131 | + _.proximity("a", "c", 2); // => '"a b"'~2 |
| 132 | + |
| 133 | + |
| 134 | + /*** |
| 135 | + * required |
| 136 | + */ |
| 137 | + |
| 138 | + _.required(_.term('required')); // => '+"required"' |
| 139 | + |
| 140 | +``` |
| 141 | + |
| 142 | +## Tests |
| 143 | + |
| 144 | +```bash bash |
| 145 | +set -eo pipefail |
| 146 | + |
| 147 | +{ |
| 148 | + npm i |
| 149 | + npm prune |
| 150 | +} > /dev/null |
| 151 | + |
| 152 | +npx standard --fix |
| 153 | +npx nyc npm t |
| 154 | +``` |
| 155 | + |
| 156 | +## Contributing |
| 157 | + |
| 158 | +I have not gotten the chance to use this lib in my own projects. Please share |
| 159 | +your thoughts, issues and improvements. |
| 160 | + |
| 161 | +- Make sure your dependencies are installed by running: `npm run-script setup` |
| 162 | +- Then start editing the index.js |
| 163 | +- You should add and/or edit the tests in test/index.js |
| 164 | +- Run your tests and see what happens |
| 165 | + |
| 166 | +When performing pull request make sure to not add the **dist** files. This is left |
| 167 | +to the maintainers(s) of the library. They are responsible to version and avoid |
| 168 | +code breakages. |
| 169 | + |
| 170 | +You can perform your own build with `npm run-script` build to make a *lucine.js* and |
| 171 | +a *lucine.min.js* |
| 172 | + |
| 173 | +**notice** |
| 174 | + |
| 175 | +I am currently not using this repository in any of my projects. Therefore I am looking |
| 176 | +for people that are able to make LQSB more useful for them and others. |
| 177 | + |
| 178 | +## Road map |
| 179 | + |
| 180 | +- split all functions into separate files |
| 181 | +- tasks for running tests on dist/lucene.js and dist/lucene.min.js |
| 182 | + |
| 183 | +## License |
| 184 | + |
| 185 | +The MIT License (MIT) |
0 commit comments