Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is it possible to query by field? #125

Closed
stephengoodwin opened this issue Dec 18, 2014 · 8 comments
Closed

Is it possible to query by field? #125

stephengoodwin opened this issue Dec 18, 2014 · 8 comments

Comments

@stephengoodwin
Copy link

Current query:

index.search('austin')

Desired query:

index.search('city:austin')
@jbohren
Copy link

jbohren commented Dec 19, 2014

Or even:

index.search({'city': 'austin'})

@olivernn
Copy link
Owner

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
Copy link

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
Copy link

jbohren commented Dec 21, 2014

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
Copy link

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
Copy link

tirana commented Nov 20, 2015

+1

@olivernn
Copy link
Owner

olivernn commented Jan 4, 2017

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
Copy link
Owner

Closing this now as v2 is now available in npm.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants