Question regarding Lucene scoring

lucene

I have a question regarding Lucene scoring. I have two documents in the index, one contains "my name" and the other contains "my first name". When I search for the keyword "my name", the second document is listed above the first one. What I want is that if the document contains exact keyword I typed, it should be listed first, then the other. Can anyone help me how to do this. Thanks.

Best Answer

A second attempt at an answer: Lucene's default behavior should be what you ask for. The critical factor here is the lengthNorm() part of the score - which sometimes scores longer documents lower than shorter ones. See Lucene's Similarity API for the context. If, say, the lengthNorm was identical for the two hits, they were sorted arbitrarily.

The explain() function will help you see why the documents were scored the way they were, and not according to the default.

I assume you are using a BooleanQuery. If you post the exact way your query is formulated, I may be able to say more. See also the Query Parser Syntax. I hope this is nearer to the mark.

Related Topic