bripkens / lucene

Node.js lib to transform: lucene query → syntax tree → lucene query
MIT License
72 stars 33 forks source link

Falsy numbers as term values lead to invalid queries in toString() #48

Open saeub opened 2 years ago

saeub commented 2 years ago

When a Node has term: 0, the query returned by toString() will lack a value:

> const lucene = require("lucene");
> lucene.toString({
... "left": {
..... "field": "field",
..... "fieldLocation": {
....... "start": {
......... "offset": 0,
......... "line": 1,
......... "column": 1
......... },
....... "end": {
......... "offset": 5,
......... "line": 1,
......... "column": 6
......... }
....... },
..... "term": 0,  // <-----
..... "quoted": false,
..... "regex": false,
..... "termLocation": {
....... "start": {
......... "offset": 6,
......... "line": 1,
......... "column": 7
......... },
....... "end": {
......... "offset": 7,
......... "line": 1,
......... "column": 8
......... }
....... },
..... "similarity": null,
..... "boost": null,
..... "prefix": null
..... }
... });
'field:'

Changing "term": 0 to "term": "0" fixes the problem, returning 'field:0'.

I think this is due to checking falsy values in these places:

https://github.com/bripkens/lucene/blob/961ecf24d2dc3bd18d700d4c8a5cb1cfbac13106/lib/toString.js#L52 https://github.com/bripkens/lucene/blob/961ecf24d2dc3bd18d700d4c8a5cb1cfbac13106/lib/toString.js#L77

Of course, a workaround is to always use strings as term values, but allowing numbers and returning an invalid query like this is very confusing.