olivernn / lunr.js

A bit like Solr, but much smaller and not as bright
http://lunrjs.com
MIT License
8.96k stars 548 forks source link

range queries on numbers #237

Open mbriot opened 8 years ago

mbriot commented 8 years ago

I know it's out of the scope but would be really happy to have it in lunr :)

var index = lunr(function(){
    this.field('price');
    this.ref('id');
});

index.add({id: "1",price: "2"});
index.add({id: "2",price: "11"});
index.add({id: "3",price: "27"});

var results = index.numericRangeSearch("[10:30]");
// results = {id : 2 , id : 3}

Lucene used to transform numbers to sortable strings to achieve such kinds of queries : https://github.com/apache/lucene-solr/blob/master/lucene/core/src/java/org/apache/lucene/util/NumericUtils.java

And some times before, the workaround was to use padding : Before

2
11
27

After

002
011
027

If you don't want to include this feature in lunr, any help would be really appreciated.

Thanks a lot for this wonderful project anyway.

Manu

olivernn commented 8 years ago

So, first of all, this is almost definitely not going to make it into the current version of lunr, it would involve too much change, specifically around understanding the search term, but probably in other places too.

The next version of lunr already has some smarts around parsing search queries, so there is already the pieces in place for understanding [10:30] as meaning any document with a number between 10 and 30.

I need to do a bit more background reading, as well as consider how text ranges such as [aa:ab] would work, but it might be something that could end up making it into lunr.

I'll keep this issue open for now, just to remind me to think about it. I can't promise that I'll end up with a solution any time soon though.

mbriot commented 8 years ago

wonderful ! So the QueryParser is already in place to achieve range queries but you need to work on the index to make it possible ?

On which part of the code can I try to help ?

olivernn commented 8 years ago

Well, there is a query parser, it doesn't yet understand range queries but that can be done. The larger piece of work will be finding ranges in the index, but again, this shouldn't be impossible.

Hold off on making a start on this for now, that branch I linked you to is still a work in progress, and things may change, I don't want you to waste any effort. I'm hoping to publish an alpha soon, and then start finalising the interfaces etc, at that point we can start thinking about how to implement.

fauno commented 6 years ago

hi, now lunr is 2.3, is there a way to do this? :)

olivernn commented 6 years ago

@fauno not yet, mostly because I haven't prioritised it.

murraybauer commented 1 year ago

Most applications which are searching purely text/documents typically require the ability to also add search constraints for Date ranges e.g.

All of which would solved via a simple numeric range capablity.