olivernn / lunr.js

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

Add the search term to the output results #310

Closed LorenzoSerino closed 6 years ago

LorenzoSerino commented 6 years ago

Hi Oliver, I'm trying to add the search term on the results to analyze the result of the output and reject the little significants. For example, I would like to look for the documents containing two clauses:

Now I want to look for all the documents that contain:

On the query, i use:

    var results=idx.query(function (query){
        query.term("topolino", {fields: ["docs"], boost: 100, usePipeline: true })                              
        query.term("pa", {fields: ["docs"], boost: 80, usePipeline: true, wildcard: lunr.Query.wildcard.TRAILING})
    });

Then i modified "lunr" lunr.Index.prototype.query = function (fn) { .. .. .. for (var l = 0; l < matchingDocumentRefs.length; l++) { /*

N.B.: the good work is made by "combine".

Now in the metadata item there are the clauses so i can reject the little significants ([po*]) with a loop.

Is there a better method without modifying lunr?

Regards lorenzo

olivernn commented 6 years ago

So, it looks like you want to do a query for documents that contain terms matching "topolino" and optionally "po*".

Unfortunately this isn't something that is supported yet in Lunr. It is a much requested feature, and is what I'm currently working on. I've discussed how I think this should look, but I would phrase that query as:

documents must contain "topolino" and optionally contain "po", or with the proposed additions to the search syntax: `+topolino po` or, updating the query you mention in your description:

var results=idx.query(function (query){
  query.term("topolino", { fields: ["docs"], boost: 100, presence: lunr.Query.presence.REQUIRED })
  query.term("pa", { fields: ["docs"], boost: 80, wildcard: lunr.Query.wildcard.TRAILING })
});

Note that the above syntax might change slightly, but that is what I'm currently aiming for. Would that allow you to write the queries you want?

LorenzoSerino commented 6 years ago

Hi Oliver, thanks for the quick reply

I read the old discussed and Yes, your proposal for me is correct.

if i can i want to help you and i suggest you: 1) lunr.Query.presence.REQUIRED ok 2) lunr.Query.presence.ONLYONE

for example: two documents:

if i try to find "pa*": now the second document has more score then the first because the second has two word matched.

with ONLYONE: if i try to find "pa*" the first and second have same score.

So if i try to find "to pa" i'll find the first document because has two matched.

then, more immediate and simple solution i would add the clause in the items result o a custom function (output pipeline...) for whatever you want.

Thanks in advance Lorenzo

olivernn commented 6 years ago

I'm going to close this as I think it is mostly covered by the 2.2.x release.