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

Is it possible to query by field? #125

Closed stephengoodwin closed 7 years ago

stephengoodwin commented 9 years ago

Current query:

index.search('austin')

Desired query:

index.search('city:austin')
jbohren commented 9 years ago

Or even:

index.search({'city': 'austin'})
olivernn commented 9 years ago

Its currently not possible to search by field due to the way that the documents are scored and indexed. I'll take it into account as a feature for future releases though.

shaun4477 commented 9 years ago

I've gotten around this by indexing the fields raw and as ":". It only works for single word values but is reasonably effective for my use case.

jbohren commented 9 years ago

I've gotten around this by indexing the fields raw and as ":". It only works for single word values but is reasonably effective for my use case.

Can you elaborate on what you mean?

justinlinz commented 9 years ago

Not sure if this is the solution shaun4477 was referring to, but you can prefix your value with propertyname: when indexing, and get decent results. There are probably some drawbacks with this approach as well, but depending on what you need might get the job done.

var data = [{
            refId:'68977',
            name:'Dilbert',
            title:'Engineer'
        },{
            refId:'45674',
            name:'Wally',
            title:'Engineer',
        },{
            refId:'00014',
            name:'Pointy-Haired Boss',
            title:'Manager'
        }
        ];

        var employees = lunr(function () {
            this.ref('refId');
            this.field('name');
            this.field('title');
        });

        data.forEach(function(r){
            employees.add({
                    refId:'refId:'+ r.refId,
                    name:'name:'+ r.name,
                    title:'title:'+ r.title
                }
            );
        });

        var results = employees.search("name:Dilbert title:Engineer");
tirana commented 8 years ago

+1

olivernn commented 7 years ago

I have published an alpha release of the next version of lunr with support for query by field.

In addition I've put together a basic demo that should allow you to play around with the search by field feature.

olivernn commented 7 years ago

Closing this now as v2 is now available in npm.