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