Skip to content
Advertisement

Add weights to documents Lucene 8

I am currently working on a small search engine for college using Lucene 8. I already built it before, but without applying any weights to documents.

I am now required to add the PageRanks of documents as a weight for each document, and I already computed the PageRank values. How can I add a weight to a Document object (not query terms) in Lucene 8? I looked up many solutions online, but they only work for older versions of Lucene. Example source

Here is my (updated) code that generates a Document object from a File object:

JavaScript

The issue with my current approach is that I would need a CustomScoreQuery object to search the documents, but this is not available in Lucene 8. Also, I don’t want to downgrade now to Lucene 7 after all the code I wrote in Lucene 8.


Edit:

After some (lengthy) research, I added a DoubleDocValuesField to each document holding the boost (see updated code above), and used a FunctionScoreQuery for searching as advised by @EricLavault. However, now all my documents have a score of exactly their boost, regardless of the query! How do I fix that? Here is my searching function:

JavaScript

Advertisement

Answer

Regarding my edited problem (boost value completely replacing search score instead of boosting it), here is what the documentation says about FunctionScoreQuery (emphasis mine):

A query that wraps another query, and uses a DoubleValuesSource to replace or modify the wrapped query’s score.

So, when does it replace, and when does it modify?

Turns out, the code I was using is for entirely replacing the score by the boost value:

JavaScript

What I needed to do instead was using the function boostByValue, that modifies the searching score (by multiplying the score by the boost value):

JavaScript

And now it works! Thanks @EricLavault for the help!

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement