olivernn / lunr.js

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

Question about field #468

Closed SukkaW closed 4 years ago

SukkaW commented 4 years ago

I'm building a search engine for a JAMStack website using lunr.js and here is the case:

const res = lunr(function() {
  this.ref('url');
  this.field('title');
  this.field('content');
  this.field('tags');
});

As code shows above, I add 3 fields, title, content and tags. However, I want field tags only be searched when specified. I mean:

How should I build my index to achieve this?

faizanu94 commented 4 years ago

I think what @SukkaW is saying can be achieved by fields feature, however @olivernn can suggest better

olivernn commented 4 years ago

How should I build my index to achieve this?

I think you've already built you search index in the correct way to do this. If, when searching, you only want to search specific fields, you can specify those when calling either search or query

I want res.search('foo') only search through field title & content

You will have to specify the fields you want to search unless you want to search all of them. Using your example, and the search method this would become:

res.search("title:foo content:foo")